Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Problem solved! .........................
Posted
Updated 17-Nov-14 6:14am
v4
Comments
PIEBALDconsult 17-Nov-14 10:42am    
== 1) // read one integer

Doesn't that mean "one character"?

OK, that part of my C-Fu is quite rusty, forget I said it. Carry on, nothing to see here.
PIEBALDconsult 17-Nov-14 10:49am    
If it's a one-digit number that might be OK. I'd remove the test or make it >0.
den2k88 17-Nov-14 10:51am    
Nope, it means one *match*.

fscanf("%d %d") for example returns 2 if it sees 2 integers, 1 if there is only one, 0 if none. It is not tied to the number of characters!
Richard MacCutchan 17-Nov-14 12:09pm    
You open the file, read the count value, close the file. You then open the file again and count the number of integers, so it will include the count on the first line, and always be wrong. And even if it is correct, you then try to continue reading to get the values a second time. But at this point your file is at EOF so that will also fail.

The thing I see is that you're not closing the file after having it scanned the first time. So what happens?

The first time you cycle through the file you go right until the END OF FILE, and the internal read pointer of the FILE structure points there. Then you try to read other t_size number AFTER the end of file -> that doesn't work. If you step with a debugger you will probably see the second loop (the one with the counter) never being executed.

So what can you do?
Either
1) close the file after the first read-through, re-open it and skip the first integer

OR

2) After reading the first integer you immediately allocate the array and start pouring data in there, making the checks right before assigning the read integer to its place in the array.

Hope to be of help,
Denis
 
Share this answer
 
C#
while ( fscanf ( fp, "%d", &value) == 1) // read one integer
                {
                o++;                // this loop will continue until EOF or non-integer input
                }


now you're at EOF

C#
        if ( o > t_size)    // 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 ( o < t_size)    // 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
        }


        //else if everything work..

printf("Create a size of %d array\n", t_size);

int* my_array = NULL;
my_array = malloc(t_size*sizeof(*my_array));

if (my_array==NULL) {
printf("Error allocating memory!\n");
return 1;
getchar();
}


These are simple static checks, they work just fine.

The subsequent fscanf where does it pick the data from? AFTER EOF. Bad.

C#
        int i =0;
        for ( i = 0; i < t_size; i++ )
        {
        fscanf(fp, "%d",&my_array[i]);
        }
}



If you add the sequent lines before the above cycle

C#
fclose(fp);
fp = fopen ("xxx.txt", "r");
fscanf(fp, "%*d");  // ditch the next integer

it should work. That is the first way.

Also note that making two trivial integer checks in a loop is not slower than going TWICE through the file.
 
Share this answer
 
Comments
[no name] 17-Nov-14 12:14pm    
Don't worry I made it!

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