Re: frustrated by fscanf and sscanf
- From: James Kuyper <jameskuyper@xxxxxxxxxxx>
- Date: Wed, 28 Nov 2007 13:47:18 GMT
a wrote:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below:....
The unusual but deterministic behaviour is that some rows can be read successfully but not the others. In this case, the first matrix is read successfully, but the second one, nothing can be read (I think, printf shows all zeros) until the 5 96 0 83 (the 7th row on 2nd matrix) but then the reading is then on and off again (Frustrated >.< )
I took your example data, removing what I assumed was explanatory text that's not in your actual data, and stored it in a file. I wrapped your code fragment in a complete program, which set up everything appropriately. You used feof() inappropriately, and SIZE violates the usual conventions for naming what must be a variable in this program, but I left those things uncorrected, since they shouldn't affect the results.
// This code was written based upon a message posted by a@xxxxx on the
// usenet newsgroup comp.lang.c
// Message-ID: <fiikts$2kr9$1@xxxxxxxxxxxxxxxxxxxxxxxx>
// Date: Wed, 28 Nov 2007 10:48:27 +0800
// The lines from that message are marked with //a. The rest of this program
// was written by James Kuyper to fill in a suitable context.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int SIZE;
int retval = EXIT_SUCCESS;
const char filename[] = "test.dat";
FILE *file = fopen(filename, "r");
double *r;
if(file == NULL)
{
perror(filename);
return EXIT_FAILURE;
}
if(fscanf(file, "%d ", &SIZE) != 1)
perror("SIZE");
retval = EXIT_FAILURE;
}
else if(SIZE <1 || SIZE_MAX/2/SIZE/SIZE < 1)
{
fprintf(stderr, "Unacceptable value for SIZE:%d\n", SIZE);
retval = EXIT_FAILURE;
}
else if((r=malloc(2*SIZE*SIZE*sizeof(*r)))==NULL)
{
fprintf(stderr, "Insufficient memory");
retval = EXIT_FAILURE;
}
else
{
int i;
printf("Reading 2 %dX%d arrays of double.\n", SIZE, SIZE);
while(!feof(file) && i< SIZE * SIZE + SIZE * SIZE) { //a
int n =
fscanf(file, "%lf", &r[i]); //a
if(n != 1)
{
fprintf(stderr, "fscanf() returned %d\n", n);
break;
}
i++; //a
} //a
if(ferror(file))
{
perror(filename);
retval = EXIT_FAILURE;
}
printf("Elements read:%d\n", i);
free(r);
}
fclose(file);
return retval;
}
I compiled and ran my version of your program, with the following results:
~/testprog(77) make scan_array
cc -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -c -o scan_array.o scan_array.c
cc scan_array.o -o scan_array
~/testprog(78) scan_array
Reading 2 12X12 arrays of double.
Elements read:288
Whatever the problem with your actual program is, it comes from something that's different from what I wrote. Therefore, what you should do is simplify your code as much as possible, while still demonstrating the problem. Then post your ENTIRE program, not just a fragment. Post your actual input file, not one with textual explanations stuck in the middle, and finally include the exact output from your posted program, when run using your posted data. Only then will be able to help you further.
.
- Follow-Ups:
- References:
- frustrated by fscanf and sscanf
- From: a
- frustrated by fscanf and sscanf
- Prev by Date: Re: Why is there 'div()' in the standard lib
- Next by Date: Can A Macro Do This?
- Previous by thread: Re: frustrated by fscanf and sscanf
- Next by thread: Re: frustrated by fscanf and sscanf
- Index(es):
Relevant Pages
|