Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I code in C. I recently created a program that read a text file that contains two lines of numbers. I want for the first line to be only one number and for the second line as many as I want. I don't care for the value of the number just to be one integer. If the numbers are greater than 1 in the first line then will print an error and will return 0;

I use the fscanf() library but I can't code it.

eg

text.txt--------
9859                  <-only one number
29 5898 95 9594 


9859 895               <-more than one numbers. Print an error!
29 5898 95 9594


Here is the fscanf code:

C++
fscanf(fp,"%d",&num);													// here goes the fscanf() command  ?Do I need to use fget()?

int readch = 0;
long int filepos = 0L;
filepos = ftell ( fp); 	// get the file position                                                                   
while ( ( readch = fgetc ( fp)) != EOF) { 
// read each character of the file
    if ( isalpha ( readch)) {
        // check each character to see if it is a letter                                    
        printf ( "File contains letters\n");  // print error message
        fclose ( fp);                         // close file
        getchar();                            // wait for the user to press a key
        return 1;                            // exit the program
    }
}


fseek ( fp, filepos, SEEK_SET); 										// move file position back to where it was after reading num 

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;         // The compiler creates the integer "int i",  
    int value = 0;     // with the value of "0".
    
    while ( fscanf ( fp, "%d", &value) == 1)
    { 																                                                        
        i++;   // this loop will continue until EOF or non-integer input                                                    
    }
    if ( i > num)   // If the second line of the file has more integers
    // than the first number in the first line says...
    {
        printf ( "too many integers\n");   // ...then will "print" an error.
        getchar();    // wait the user press a key 
        return 1;     // the code will return 1, exit the program
    }
    if ( i < num)
    {
        // If the second line of the file has less integers
        // than the first number in the first line says...
        printf ( "not enough integers\n");    // ...then will "print" an error.
        getchar();
        // wait the user press a key 
        return 1;   // the code will return 1, exit the program
    }
    
    //!here the code will continue if every restrictions was passed!





Can anyone help me?

Regards,
George
Posted
Updated 7-Oct-14 8:32am
v3
Comments
joshrduncan2012 7-Oct-14 10:11am    
What have you tried? Where are you stuck? Can you please show us your code and where the error is pointing to?
[no name] 7-Oct-14 10:13am    
Because with fscanf you don't know on which line you are. Seems you have to go the hard way and read in the first line as string and check how many numbers you will find
[no name] 7-Oct-14 10:24am    
can I use another way except fscanf?
[no name] 7-Oct-14 10:27am    
How exactly you are using fscanf right now? Some code snippets available?
[no name] 7-Oct-14 10:47am    
I update the code to see it. But I just to use another one restriction. Just in case.

You should read each line completely (something like fread), and then use strtok to split it into its components. You can then count the number of items in each line, and convert them from strings to integers using atoi.
 
Share this answer
 
Comments
[no name] 7-Oct-14 13:41pm    
That is a clear Statement. 5. Regards, Bruno
Richard MacCutchan 7-Oct-14 14:19pm    
Thanks.
[no name] 8-Oct-14 8:00am    
Indded. But I'm getting errors. How exactly code it?
Richard MacCutchan 8-Oct-14 8:44am    
What does "I'm getting errors" mean?
If you want help, then show us the code and the errors, and explain exactly what is happening; we cannot guess. Use the Improve question link above to add the details.
fscanf and scanf aren't very flexible for parsing. It is difficult to do proper validation of data. Lots of reasons for not using (f)scanf.

That said, here's a trick to make it work for you. This will recognize whether the first number is the only number. The restriction is there can be no white space following this number. If you must accept "1234 " as the first line, this won't work.

You should also understand that - because of the way (f)scanf works, leading blank lines will be skipped. (f)scanf skips over all leading white space - including newlines.

C++
char peek = ' ';
if ( fscanf(fp, "%d%c", &value, &peek) == 2 && peek = '\n' )
{
    // parse remaining numbers in a loop.
    while ( fscanf(fp, "%d", &value) == 1) )
    {
        // etc.
    }
}


It should be obvious the code extracts the character following the first number and checks whether this character is a line feed.
 
Share this answer
 
Look at 'c-read-file-line-by-line'. It's a long discussion about the best way to read a text file line by line.
 
Share this answer
 
Comments
[no name] 7-Oct-14 11:02am    
Not helped a lot to say the truth
I found this on the same site.
Google is a programmers best friend :)

C
FILE *file = fopen("Integers.txt", "r");
 int integers[100];

 int i=0;
 int num;
 while(fscanf(file, "%d", &num) > 0) {
     integers[i] = num;
     i++;
 }
 fclose(file);
 
Share this answer
 
Comments
[no name] 7-Oct-14 11:31am    
And how should this help to decide first line and others?
[no name] 7-Oct-14 11:37am    
It can't
[no name] 7-Oct-14 11:40am    
Yep you are right, that was a question to http://www.codeproject.com/script/Membership/View.aspx?mid=1905096 ;)
Sten Hjelmqvist 7-Oct-14 11:50am    
You can lead a horse to water but you cannot force it to drink.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900