Search code examples
cgccscanfdjgpp

Use fscanf to read two lines of integers


I want to ask something that I write in C.

I use the fopen() command to open and read a text file that contains only two lines. in first line is an integer N number, and in the second line is the N integer numbers that the first line says.

Eg.

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

but I want to take restrictions when the file openend.

the number N should be between 1 ≤ Ν ≤ 1.000.000. If not then show an error message. If the file is ok then the programm continue to run with another code.

Here is what I done until now:

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
            }

But the problem is that the restriction checks only for the first line number , it's correct though, but don't read the numbers in the second line.

What I mean is that : Lets say that I have the number 10 in the first line. The code will analyze the number, will check for restrictions and will proceed to the 'else' part

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
                }

..and it will said that everything is working. But what if I have 20 numbers in the second line? -when I need only 10

Eg.

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

So I hoped be as cleared as I could. My question is that I need a code in the program, besides the 1st restriction, that have also another one restriction under the first that will read the second line of the txt file with the numbers and check if there are as many numbers as the first line says!

How do I do that? If you guys want any other declarations feel free to ask! Hope I was clear with my problem :)


Solution

  • This will check the number of integers and report too many or not enough. The integers are not saved except for each one being read into the value. Do you want to store each integer?

    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                                                                 
    }