Re: frustrated by fscanf and sscanf



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.
.



Relevant Pages

  • Re: fread()
    ... > int i,istat,iaccum,intype; ... reading binary data from standard input? ... Use sizeof (float), or even better, sizeof *floatarray, instead. ... FLOAT, and set istat accordingly? ...
    (comp.lang.c)
  • Re: Opening a PPM file Image
    ... Just create a CFile object and open it in binary mode for reading, ... int height; ... int rowsize = width; ...
    (microsoft.public.vc.mfc)
  • Problem with shared library generated on Solaris 8 X86
    ... trondn@solsrv2 cctest> uname -a ... Reading testprog ... extern int func; ...
    (comp.unix.solaris)
  • Re: Two threads reading from same file?
    ... > proper part of the disk file, I filled the disk file with 4-byte ... When reading from disk index 999 ... int loop = 1000; ... void CreateTestFile() ...
    (microsoft.public.win32.programmer.kernel)
  • Re: integers and arrays in Java - how?
    ... But "int" not only does not work, it also prevents reading X and Y coordinates of the mouse. ... Get X and Y mouse coordinates into a variable that i can do real math on. ... If i try "int" in that math, the values are then zero for everything - even those where i do no calculation. ... And i would have no clue as if the code was compilable or not, and i do not think i care, since the jave code is a part of an HTML web page. ...
    (comp.lang.java)