Re: how to count rows and columns of integers/doubles in a file?
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Wed, 15 Mar 2006 22:05:45 -0800
On Wed, 15 Mar 2006 20:32:04 +0100, "Martin Joergensen"
<unoder.spam@xxxxxxxxxxxx> wrote:
Hi,
I have some files which has the following content:
0 0 0 0 0 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0
I'm making a function for a bigger program, that
1) counts number of columns in each row
2) verifies that there are the same number of columns in each row
(if not: error + quit)
3) increments row count after one row is finished
4) stops execution if it encounters anything that is not an
integer (later: should also work for doubles)
My only problem is that I don't know how to switch line (carriage
return) or how to detect it using scanf. Besides that, the
program is almost finished. See below:
- - - - - - - - - - - - - - - -
#include <stdio.h>
void int_getdata(char *filename, unsigned int *x, unsigned int
*y);
int main()
{
unsigned int n_x[3], n_y[3];
int_getdata("unknowns.dat", &n_x[0], &n_y[0] );
int_getdata("BC_types.dat", &n_x[1], &n_y[1] );
// double_getdata("BC_values.dat", &n_x[2], &n_y[2] );
return 0;
}
void int_getdata(char *filename, unsigned int *x, unsigned int
*y)
{
FILE *fp;
unsigned int old_nx;
int int_readvalue, returnvalue;
//double double_readvalue;
old_nx = 0; /* reset */
*x = 0;
*y = 0;
if ( (fp = 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);
}
/* get nx and ny */
do{
returnvalue = fscanf(fp, "%i", &int_readvalue); /* all
input must be valid */
fscanf will eat white space, including the \n that marks the end of
line.
You could use fgets to get a complete line then strtol to process each
value for correctness.
if(returnvalue == 1) (*x)++; /* nx is getting larger */
else{
printf("Error: non-valid input found in %s.\n",
filename);
exit(1);
}
if(int_readvalue == '\n')
{
if(old_nx != 0 && old_nx != *x)
{
printf("ERROR: nx is not a constant in file
%s!\n", filename);
exit(1);
}
else /* we should now be sure that nx is constant and
move on to the next input line */
{
old_nx = *x;
y++; /* ny is getting larger */
*x = 0;
}
}
} while(returnvalue != EOF);
printf("\nFinished reading from file %s: (x,y) = (%i,%i).\n",
filename, *x, *y);
fclose(fp); /* close input file, finished reading values in
*/
}
- - - - - - - - - - - - - - - -
I hope somebody can help me solve this small problem, so I can
finish the program....
I also get these small warnings, but they are not critical:
warning C4013: 'system' undefined; assuming extern returning int
warning C4013: 'exit' undefined; assuming extern returning int
Best regards / Med venlig hilsen
Martin Jørgensen
Remove del for email
.
- References:
- how to count rows and columns of integers/doubles in a file?
- From: Martin Joergensen
- how to count rows and columns of integers/doubles in a file?
- Prev by Date: Re: newbie program, pointers again...
- Next by Date: Re: Trap Representations - c99 [again]
- Previous by thread: Re: how to count rows and columns of integers/doubles in a file?
- Next by thread: Trap Representations - c99 [again]
- Index(es):
Relevant Pages
|