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



"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.
.



Relevant Pages

  • Re: text reading
    ... Read lines using fgets() then use sscanf() to read the values. ... This is much easier than using fscanf() on its own. ... simply using fscanf in loop. ...
    (comp.lang.c)
  • Re: text reading
    ... Read lines using fgets() then use sscanf() to read the values. ... This is much easier than using fscanf() on its own. ... Are you saying not to use any of ...
    (comp.lang.c)
  • Re: Reading a file
    ... Normally I'd suggest fgets() followed by sscanf() ... I get the feeling this is ideal territory for fscanf. ... to eat my words when the OP posts the actual data format. ...
    (comp.lang.c)
  • Re: text reading
    ... Read lines using fgets() then use sscanf() to read the values. ... This is much easier than using fscanf() on its own. ... The thing about kandr2 much of the time is that it ...
    (comp.lang.c)
  • Re: Newbie-question: scanf alternatives?
    ... Please engage your brain and explain what makes fscanf() any less ... fundamental than any other standard C library function. ... >EOF is not guaranteed. ... >> Try to implement it without using fgets() at all and compare the ...
    (comp.lang.c)