Re: how to count rows and columns of integers/doubles in a file?



"Keith Thompson" <kst-u@xxxxxxx> skrev i en meddelelse news:lny7zbfiip.fsf@xxxxxxxxxxxxxxxxxx
"Martin Joergensen" <unoder.spam@xxxxxxxxxxxx> writes:
-snip-
A much better approach is to use fgets() to read a line at a time,
then use sscanf() to scan the resulting line. sscanf(), unlike
fscanf(), doesn't consume its input; it's still there in the string,
and if the sscanf() call fails (check its returned value), you can try
again.
-snip-

What do you think about the following, based on Michael Mair's proposal?

Just copy/paste - compiles well...
- - - - - -
#include <stdlib.h> /* system("PAUSE") */
#include <stdio.h>
#include <stddef.h>
#include <ctype.h> /* for isspace */

#define number_of_files 3

/* prototypes */
void getdata(char *filename, unsigned *pRows, unsigned *pCols); /* this is how the args should be */
int countColsInNextLine (FILE *pFile, unsigned *pCols);


int main()
{
int i;
unsigned n_x[number_of_files], n_y[number_of_files]; /* one space for each data file */

getdata("unknowns.dat", &n_x[0], &n_y[0] );
getdata("BC_types.dat", &n_x[1], &n_y[1] );
// getdata("BC_values.dat", &n_x[2], &n_y[2] );

for(i=0; i<number_of_files; i++)
printf("Data file number %i has properties: x = %u, y= %u\n", i, n_x[i], n_y[i]);

return 0;
}

//fgets( stringbuffer, 20, fp);
//sscanf( stringbuffer, "%X", &var1);

void getdata(char *filename, unsigned *pRows, unsigned *pCols) /* this is how the args should be */
{
FILE *pFile;

unsigned rows, cols, oldcols;

if ( (pFile = fopen(filename, "r") ) == NULL)
{
printf("Cannot open file %s.\n", filename);
system("PAUSE"); /* give the user at chance to see this error before the windows shuts down */
exit(1);
}

*pRows = 0;
*pCols = 0;

/* get nx and ny */
cols = 0;
rows = 0;
if (countColsInNextLine(pFile, &oldcols) != EOF) { /* get oldcols for first row */
rows++;
while (countColsInNextLine(pFile, &cols) != EOF) {
rows++;
if (cols != oldcols) { /* verify that number of cols didn't change */
printf("ERROR: Number of columns is not a constant in file: %s!\n", filename);
exit(1);
}
}
}

*pRows = rows; /* update results */
*pCols = oldcols;

printf("\nFinished reading from file %s.\n", filename);
fclose(pFile); /* close input file, finished reading values in */
}

int countColsInNextLine (FILE *pFile, unsigned *pCols)
{
int c;
unsigned cols = 0;

do {
c = getc(pFile); /* get first character to start the loop */
if (c != EOF && !isspace(c)) { /* first time non-space encountered, update cols */
cols++;
do { /* skip digits and commas */
c = getc(pFile);
} while (c != EOF && !isspace(c) && c != ',' && c != '.'); /* comma should also be valid input in number */
}
while (c != EOF && c != '\n' && isspace(c)) { /* skip through spaces, but not '\n' ! */
c = getc(pFile);
}
} while (c != EOF && c != '\n');

*pCols = cols;
return c;
}
- - - -
It seems like there is small error in counting the rows..... I couldn't find the error before and I'm not sure I understood all the while loops completely - to help myself I added some comments.

So there's just a little error left (that's the second time in this thread I write that - This time I hope it's true!) :-)


Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

.



Relevant Pages

  • Re: how to count rows and columns of integers/doubles in a file?
    ... void getdata(char *filename, unsigned *pCols, unsigned *pRows, int *count); ... void testwronginput(int c, unsigned cols, unsigned rows); ... unsigned rows, cols, oldcols; ...
    (comp.lang.c)
  • Re: text reading
    ... int values_read; ... Read the documentation for sscanf. ... Fix any errors you see. ... Keith Thompson kst-u@xxxxxxx ...
    (comp.lang.c)
  • Re: sscanf not working , why?
    ... >> int main ... Now sscanf will find 'a' to be the first character. ... >> 2) second test program code ...
    (comp.lang.c)
  • Re: text reading
    ... int values_read; ... Read the documentation for sscanf. ... introduced a few deliberate errors to provoke responses. ... then take the program you posted and fix it. ...
    (comp.lang.c)
  • Re: how to count rows and columns of integers/doubles in a file?
    ... void getdata(char *filename, unsigned *pCols, unsigned *pRows, int *count); ... void testwronginput(int c, unsigned cols, unsigned rows); ... unsigned rows, cols, oldcols; ... I just saw in the debugger that I think scanf left a 10 in the buffer and if I didn't put this here, it would have taken that as a control character and stop the program. ...
    (comp.lang.c)