使用fscanf:在输入

时间:2016-09-13 17:16:14

标签: c loops if-statement while-loop scanf

在为即将到来的作业做一些练习时,我遇到了使用fscanf的问题并尝试在将不正确的格式输入程序时发出通知。我很确定我必须使用fscanf的返回,但无法弄清楚如何在我的while循环中使用它。目前程序在输入诸如“a3”之类的值时会连续循环,但“3e”几乎可以正常工作,除非它输出数字3两次。

工作扫描码:

int intGet( int min, int max ) {

int input = 0;
char temp = ' ';
printf("Enter a number in between [%d-%d]: ", min, max);

if (scanf("%d%c", &input, &temp) != 2){
    errorNo = 1;
    return EXIT_FAILURE;
}
if (temp != '\n'){
    errorNo = 2;
    return EXIT_FAILURE;
}
if (input < min || input > max){
    errorNo = 3;
    return EXIT_FAILURE;
}
else {
    printf("Read %d\n", input);
}
return EXIT_SUCCESS;

fscanf代码沿着相同的想法,它不断循环:

int intGet( int min, int max ) {
int counterValid  = 0;
int counterInvalid = 0;
int entry = 0;
int total = 0;
int input = 0;
int check = 0;
char temp = ' ';
FILE *fp;

fp = fopen("02_num.txt", "r");
if (!fp)
{
    perror("fopen()");
    exit(EXIT_FAILURE);
}
printf("----------------------------------------------------------\n");
printf("Entry\tInvalid\tValid\tNumber\n");
printf("----------------------------------------------------------\n");

fscanf(fp, "%*[^\n]\n"); //skips first line (min and max)


  while (check != EOF){

check = fscanf(fp, "%d%c", &input, &temp);

    if (check == EOF){
        exit(EXIT_SUCCESS);
    }
    if (check != 2){
        counterInvalid += 1;
        entry += 1;
        printf("%d\t*\t\t%d\tIncorrect Format\n", entry, input);
    }
    if (temp != '\n'){
        counterInvalid += 1;
        entry += 1;
        printf("%d\t*\t\t%d\tExtra Characters\n", entry, input);
    }
    if (input < min || input > max){
        counterInvalid += 1;
        entry += 1;
        printf("%d\t*\t\t%d\tInput Number out of Range\n", entry, input);
    }
    else{
        counterValid += 1;
        entry +=1;
        total += input;
        printf("%d\t\t*\t%d\n", entry, input);
    }
}
     printf("----------------------------------------------------------\n");
entry +=1;
printf("%d\t%d\t%d\t%d\n", entry, counterInvalid, counterValid, total);
return EXIT_SUCCESS;
}

然后,此代码将输出一个如下所示的表:

*****************************************************
       Entry    Invalid    Valid    Number
         1         *                  32
*****************************************************
// adds invalid and valid entry totals, displays then here, adds totals of valid numbers and also outputs them here.

此代码中使用的02_num.txt文件,前两个数字是最小值和最大值,在另一个函数中读取。

1 15 //min and max
23 
45 
67 
8990  
3e 
12 
a3  

我尝试使用if / while循环的东西:

定义一个变量来记录fscanf(当前最接近的)的返回值:

while (check != EOF){

check = fscanf(fp, "%d%c", &input, &temp);
    if (check != 2){
        counterInvalid += 1;
        entry += 1;
        printf("%d\t*\t\t%d\tIncorrect Format\n", entry, input);
    }

同样检查if语句中的返回值(扫描文件两次但输出不正确):

while (fscanf(fp, "%d", &input) != EOF){

    if (fscanf(fp, "%d%c", &input, &temp) != 2){
        counterInvalid += 1;
        entry += 1;
        printf("%d\t*\t\t%d\tIncorrect Format\n", entry, input);
    }

如果有任何我缺少的信息,请告诉我,我只是有点卡在这里..已经过去几天了。可能只是一个我错过的愚蠢错误,但提前感谢!

1 个答案:

答案 0 :(得分:3)

循环问题是fscanf返回零时的操作。除此之外,它意味着如果再次使用相同的格式字符串调用fscanf,结果将为零,因为输入中导致您首先获得零的问题尚未得到解决

当您从fscanf返回零时,您需要添加用于跳过该行的代码 - 例如,由Jonathan Lefflerchux修改的fscanf(fp, "%*[^\n]"); fscanf(fp, "%*1[\n]");将会工作