满足条件后如何终止函数
How to terminate a function once a condition has been met
描述:
我有一个格式如下的文件:number number number
,这里有几行:
0 00 19
0 900000 949999
0 9500000 9999999
1 00 09
2 00 19
2 200 349
2 35000 39999
2 900000 949999
2 9500000 9999999
3 00 02
3 7000 8499
3 9500000 9999999
4 00 19
4 7000 8499
80 00 19
80 900000 999999
81 00 19
81 900000 999999
82 990000 999999
83 7000 8499
958 95000 99999
959 7000 8499
960 85000 99999
961 00 19
第一列表示area
,第二列代表num1
和num2
。
我已经设法将每一列存储在其各自的变量中,因此:
int isRegistered(FILE* fp, int area)
{
int finished = 1;
int scanned_area;
char num1[7], num2[7];
int rc = 1;
rewind(fp);
while (finished != EOF) {
finished = fscanf(fp,"%d %s %sn", &scanned_area, &num1, &num2);
}
return rc;
}
我知道我"不应该使用fscanf",也不应该使用%s
扫描,但这些都是特定于任务的,我不能更改它们。
现在,我将所有这些数字存储在它们各自的变量中,第一列数字存储在scanned_area
中,第二列存储在num1
中,第三列存储在num2
中。
场景
我的面积值为1。如何对其进行编码,以便在scanned_area==1时,函数停止?
我的面积值为1。如何对其进行编码,以便在scanned_area==1时,函数停止?
您可以使用break
退出循环:
while (condtion)
{
// some code
if (scanned_area == 1)
{
break;
}
}
如果您希望函数停止,那么只需返回一个适当的值,在您的情况下,返回一个整数。一旦函数命中返回语句,就会返回该值,而在执行该值之后不会返回任何其他值。
希望这有帮助:D
相关文章:
- 为什么我的 while 函数没有终止?
- 在字符串函数中抛出'char const*'实例后调用的终止
- 程序终止时调用函数
- 当加入 C++11 函数的线程仍未终止时,是否可以返回?
- 在使用标准向量函数时引发'std::bad_alloc'实例后调用的终止
- 可变参数模板中递归模板函数的终止问题
- 如何提前终止在另一个文件中定义的函数?
- 如何在执行期间终止(或返回)具有结构类型的自动函数
- 我在执行 levelOrder 函数时无法弄清楚异常终止的原因?
- c 在函数的循环终止时
- 为什么我的析构函数中的异常没有触发 std::终止?
- 如何使用unique_ptr声明调用构造函数并处理程序终止信号
- 程序在函数返回后被终止
- 程序是否可以在调用kill函数之前返回/终止
- Windows资源监视器使用哪些Win32函数来检测已终止进程的磁盘和网络活动
- CMake 解析错误函数缺少结尾")"。而是找到带有文本的未终止字符串")
- 如果在可变参数模板解包中达到终止函数,则出现异常
- 程序不断调用意外和终止函数
- 终止函数模板递归
- 满足条件后如何终止函数