Re: how to count rows and columns of integers/doubles in a file?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 15 Mar 2006 22:30:22 GMT
"Martin Joergensen" <unoder.spam@xxxxxxxxxxxx> writes:
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
What do you mean by "columns"? Does "0 0 0 0 0 0" have 6 columns
(numbers) or 11 (characters)?
[snip]
returnvalue = fscanf(fp, "%i", &int_readvalue); /* all input
must be valid */
fscanf with a "%i" format reads an integer value after skipping any
leading whitespace -- including new-lines. It gives no indication of
what it skipped. If new-lines are significant, that's the wrong
approach.
It also accepts either decimal, octal, or hexadecimal input. Decide
whether you want to allow numbers like "0xff", and whether you want
"0123" to be interpreted in octal (yielding 83) or in decimal
(yielding 123). Since you mentioned you'll eventually want the
program to work with doubles, you probably don't want to accept octal
or hexadecimal; you probably want "%d" rather than "%i". ("%d" and
"%i" behave identically for the *printf functions, but differently for
the *scanf functions).
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.
fgets() has the disadvantage that it can only read a line up to a
specified length. If the input line is longer than what you
specified, fgets() will read only part of it; you can detect this by
whether the line you just read ends in a '\n'. If you only want to
allow input lines up to some maximum length, this isn't much of a
problem -- but you should still decide what to do if an input line
exceeds your maximum. If you want to handle arbitrarily long input
lines, you can use CBFalconer's "ggets" (Google it).
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: 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?
- 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: problem with memcpy and pointers/arrays confusion - again
- Next by Date: Re: is there any instruction or operator to convert a Byte from 10110001 to 10001101?
- 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
|