Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading a file

0 views
Skip to first unread message

zowtar

unread,
Nov 20, 2009, 10:05:45 PM11/20/09
to
1) Guys, i'm trying to read a file with one integer per line, but i
got a problem when the last line is empty (linux text files)... it
shows the number before the empty line. Any idea?
2) One more thing... when I try to read a line with one character it
prints an empty line. Any idea?

1)
FILE *in = fopen(argv[1], "r");
while(feof(in) == false)
{
int number;
fscanf(in, "%d", &number);
printf("NUMBER: %d\n", number);
}
fclose(in);

2)
char character;
fscanf(in, "%c", &character);
printf("CHAR: %c\n", character);

Keith Thompson

unread,
Nov 20, 2009, 10:17:55 PM11/20/09
to
zowtar <zow...@gmail.com> writes:
> 1) Guys, i'm trying to read a file with one integer per line, but i
> got a problem when the last line is empty (linux text files)... it
> shows the number before the empty line. Any idea?
> 2) One more thing... when I try to read a line with one character it
> prints an empty line. Any idea?
>
> 1)
> FILE *in = fopen(argv[1], "r");

What if fopen() fails?

> while(feof(in) == false)

feof() isn't what you want to use here.  fscanf tells you how many
input items it successfully read; use that to determine when you've
run out of input. See question 12.2 (and probably all of section 12)
of the comp.lang.c FAQ, <http://www.c-faq.com/>.

> {
> int number;
> fscanf(in, "%d", &number);
> printf("NUMBER: %d\n", number);
> }
> fclose(in);
>
> 2)
> char character;
> fscanf(in, "%c", &character);
> printf("CHAR: %c\n", character);

It's hard to tell from your description just what the problem is.
Remember that the new-line that terminates a line is read as a single
character.

Note that getc() or fgetc() is equivalent to fscanf with a "%c"
option.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jonathan Campbell

unread,
Nov 21, 2009, 7:21:08 AM11/21/09
to
zowtar wrote:
> 1) Guys, i'm trying to read a file with one integer per line, but i
> got a problem when the last line is empty (linux text files)... it
> shows the number before the empty line. Any idea?
> 2) One more thing... when I try to read a line with one character it
> prints an empty line. Any idea?
>
> 1)
> FILE *in = fopen(argv[1], "r");

See what Keith says about checking whether the file opened properly.

> while(feof(in) == false)
> {
> int number;
> fscanf(in, "%d", &number);
> printf("NUMBER: %d\n", number);
> }
> fclose(in);

and the FAQ on use / non-use of feof.

I would tend to use fgets (? fgets correct? I've been using C++ for the
past many years) to read each line into a (null terminated) string
(reads up to the next \n, but replaces the \n with \0) and then use
sscanf to read out of the string. Make sure to declare the char[] long
enough.

At least that will allow for easier debugging.

Best regards,

Jon C.

--
Jonathan Campbell www.jgcampbell.com BT48, UK.

Pavel R.

unread,
Nov 21, 2009, 9:54:25 AM11/21/09
to

I maybe completely wrong, but what about while ( in >> number) ?

Ulrich Eckhardt

unread,
Nov 22, 2009, 3:29:16 AM11/22/09
to
Pavel R. wrote:
[some C code]

> I maybe completely wrong, but what about while ( in >> number) ?

That's C++, the OP doesn't mention it, but his code seems to be C.
Otherwise, this while loop would be correct.

Uli

0 new messages