【问题标题】:Use fscanf to read two lines of integers使用 fscanf 读取两行整数
【发布时间】:2014-11-29 21:30:38
【问题描述】:

我想问一些我用 C 写的东西。

我使用fopen() 命令打开并读取一个仅包含两行的文本文件。在 第一行是N个整数,第二行是第一行所说的N个整数。

例如。

-------------- nubmers.txt --------------
8                                       <-- we want 8 numbers for the 2nd line
16 8 96 46 8 213 5 16                   <-- and we have 8 numbers! :)

但我想在文件打开时采取限制。

数字N 应该在1 ≤ Ν ≤ 1.000.000 之间。如果没有,则显示错误消息。如果文件正常,则程序继续使用另一个代码运行。

这是我到目前为止所做的:

int num;

....

   fscanf(fp,"%d",&num);                                                    // here goes the fscanf() command
            if(num<1 || num>1000000)                                            // set restrictions to integer 
            {
        printf("The number must be 1<= N <= 1.000.000",strerror(errno));        // error with the integer number
        getchar();                                                              // wait the user press a key
        return 0;                                                               // returning an int of 0, exit the program
            }
            else                                                                // if everything works.....
            {
        printf("work until now");                                               // Everything works until now! :)
        getchar();                                                              // wait the user press a key
        return 0;                                                               // returning an int of 0, exit the program
            }

但问题是限制只检查第一行号,虽然它是正确的,但不要读取第二行中的数字。

我的意思是: 假设我在第一行有号码10。 代码将分析数字,检查限制并进入“其他”部分

else                                                                // if everything works.....
                {
            printf("work until now");                                               // Everything works until now! :)
            getchar();                                                              // wait the user press a key
            return 0;                                                               // returning an int of 0, exit the program
                }

..它会说一切正常。 但是如果我在第二行有20 数字怎么办? - 当我只需要10

例如。

-------------- nubmers.txt --------------
10
16 8 96 46 8 213 5 16 8 9 21 5 69 64 58 10 1 7 3 6

所以我希望尽可能地被清除。我的问题是我需要程序中的代码,除了第一个限制之外,在第一个限制下还有另一个限制,它将读取带有数字的 txt 文件的第二行并检查是否有与第一个一样多的数字行说!

我该怎么做? 如果你们想要任何其他声明,请随时询问! 希望我清楚我的问题:)

【问题讨论】:

    标签: c gcc scanf djgpp


    【解决方案1】:

    这样做:

        fscanf(fp,"%d",&num);
    
        // next lines of code (restrictions). Then place the below code before getchar in the else
    
        int temp[num+1];// space to store num integers to temp and 1 space to check for extra number
    
        for(i=0;i<num;i++)
        {
            if(fscanf(fp,"%d",&temp[i]) != 1)// fscanf will automatically read 2nd line and store them in temp array
            //error ! Less numbers in file !
        }
    
        if(fscanf(fp,"%d",&temp[num]==1) //if still numbers can be scanned
        //Extra numbers found in line 2
    

    【讨论】:

    • 我还想检查更少的数字!
    • @GeorgeGkas ,现在它也会检查更少的数字!
    【解决方案2】:

    这将检查整数的数量并报告太多或不足。除了每一个被读入value 之外,整数不会被保存。是否要存储每个整数?

    fscanf(fp,"%d",&num); // here goes the fscanf() command                                                                   
    if(num<1 || num>1000000) // set restrictions to integer                                                                   
    {                         
        printf("The number must be 1<= N <= 1.000.000",strerror(errno)); // error with the integer number                     
        getchar(); // wait the user press a key                                                                               
        return 0; // returning an int of 0, exit the program                                                                  
    }                         
    else // if everything works.....                                                                                          
    {                         
        int i = 0;            
        int value = 0;        
        while ( fscanf ( fp, "%d", &value) == 1) { // read one integer                                                        
            i++; // this loop will continue until EOF or non-integer input                                                    
        }                     
        if ( i > num) {                     
            printf ( "too many integers\n");
        }                     
        if ( i < num) {                       
            printf ( "not enough integers\n");
        }                     
        getchar(); // wait the user press a key                                                                               
        return 0;  // returning an int of 0, exit the program                                                                 
    }                     
    

    【讨论】:

    • 很好!!!哦,热代码! ;) 工作正常。如果你想发布另一个答案,如果我想存储每个整数!!!
    • 是的,但我也会保留您的第一个答案!请再发一个
    • 我还想添加一个else命令来继续代码!我在哪里添加它?
    【解决方案3】:

    使用循环获取第一个 num 并检查 is 是下一行中的整数个数:

    int z = num;

    while(z--){
        if (getchar() == EOF)
            printf("err")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多