Re: how to count rows and columns of integers/doubles in a file?
- From: "Martin Joergensen" <unoder.spam@xxxxxxxxxxxx>
- Date: Thu, 16 Mar 2006 17:55:07 +0100
"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,-snip-
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.
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
.
- Follow-Ups:
- Re: how to count rows and columns of integers/doubles in a file?
- From: Martin Jørgensen
- Re: how to count rows and columns of integers/doubles in a file?
- References:
- how to count rows and columns of integers/doubles in a file?
- From: Martin Joergensen
- Re: how to count rows and columns of integers/doubles in a file?
- From: Keith Thompson
- how to count rows and columns of integers/doubles in a file?
- Prev by Date: Re: question about clock()
- Next by Date: Re: how to understand yytable yydgoto yylhs.... format?
- Previous by thread: Re: how to count rows and columns of integers/doubles in a file?
- Next by thread: Re: how to count rows and columns of integers/doubles in a file?
- Index(es):
Relevant Pages
|