使用fscanf读取两行整数

时间:2014-10-05 11:57:47

标签: c gcc scanf djgpp

我想问一些我用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文件的第二行并检查是否有与第一行一样多的数字线说!

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

3 个答案:

答案 0 :(得分:0)

使用一个获取第一个num的循环,并且check是下一行中的整数数:

int z = num;

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

答案 1 :(得分:0)

这样做:

    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

答案 2 :(得分:0)

这将检查整数的数量并报告太多或不够。除了将每个整数读入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                                                                 
}                     
相关问题