1

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

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

4

3 回答 3

0

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

int z = 数字;

while(z--){
    if (getchar() == EOF)
        printf("err")
}
于 2014-10-05T12:08:33.627 回答
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
于 2014-10-05T12:13:17.153 回答
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                                                                 
}                     
于 2014-10-05T13:42:42.983 回答