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



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
.



Relevant Pages

  • how to count rows and columns of integers/doubles in a file?
    ... I'm making a function for a bigger program, ... void int_getdata(char *filename, unsigned int *x, unsigned int *y); ... warning C4013: 'system' undefined; assuming extern returning int ...
    (comp.lang.c)
  • CE5.00 CEPlayer cannot open .wav file (0x80040216)
    ... I am trying to play a .wav file ... Please verify that the path and filename ... I have added directshow and the various codecs to the platform. ... MSDXM!CdxmPlay::ProcessWindowMessage(HWND__ * 0x7c0145f0, unsigned int ...
    (microsoft.public.windowsce.platbuilder)
  • [git patches 2/2] warnings: use uninitialized_var()
    ... used uninitialized" warning fixes, in misc-2.6.git#gccbug. ... 'uninit-var', you will receive BOTH 'uninit-var' and 'warnings' ... struct cyclades_port *info; ... unsigned int cur_frag, entry; ...
    (Linux-Kernel)
  • Re: shame on MISRA
    ... extern unsigned char variable1; ... extern unsigned int variable2; ... the warning for that in the MISRA rules turned off since I'm using ...
    (comp.arch.embedded)
  • Re: I Hate Warnings! This One Has Me.
    ... typedef unsigned int _w64 size_t; ... which means it will go to 64 bits in 64-bit systems, and so the warning ... > In class TProductData: ... > Where the dickens did "unsigned int" come from, ...
    (microsoft.public.vc.language)