program for reading a file

W

WANG Cong

On Sat, 15 Mar 2008 03:38:57 -0700,broli wrote:
I use TC 2.01 on win xp and to be honest it has given me many problems.
I want to use a different and easier complier, I have looked around but
cannot find one.

TC on win sucks a lot. Don't use it. Try to use a standard-friendly
compiler, e.g. gcc, icc.
 
S

santosh

Bartc said:
gcc -Wall (mingw version) doesn't give a warning about char c=fgetc().

lcc-win32 -A does give a warning.

Pelles C (or any other running under IDE) I wouldn't have a clue how
to set warning level.

A run through lint spots the problem immediately.
 
S

santosh

Bartc said:
gcc -Wall (mingw version) doesn't give a warning about char c=fgetc().

lcc-win32 -A does give a warning.

Pelles C (or any other running under IDE) I wouldn't have a clue how
to set warning level.

BTW the Intel C compiler does produce a warning even at standard
diagnostic levels.

$ icc -Wall -ansi -o 0 0.c
0.c(7): remark #810: conversion from "int" to "char" may lose
significant bits
c = fgetc(stdin);
^
$
 
B

Barry Schwarz

After changing to int, Im still getting an infinite loop -

#include<stdio.h>

int main(void)
{

FILE *fp;
int c;

fp = fopen("sample.dat", "r");
if(fp == NULL)
{

printf("Cannot open the file\n");
return 0;

}

while(1)
{

c = fgetc(fp);
if(c == EOF)

How are signalling EOF from your keyboard? This is system dependent.
Popular choices are ctrl-D for Unix and ctrl-Z for Windows.
{
break;
}
else
printf("%c",c);

Please learn to indent consistently and visibly. It will be a
lifesaver when you start writing larger programs. (And for stuff
posted to Usenet, use spaces, not tabs.)
}

return 0;

}


Remove del for email
 
W

William Pursell

fp = fopen("sample.dat", "r");
if(fp == NULL)
{

printf("Cannot open the file\n");
return 0;


3 comments: "Cannot open the file" is NOT a
useful error message, returning with success
seems wrong in this case, and it is odd to
write error messages to the output stream.
Please consider:

if( fp == NULL ) {
fprintf( stderr, "Cannot open %s: %s\n",
filename, strerror( errno ));
return EXIT_FAILURE;
}
 
K

Keith Thompson

broli said:
I use TC 2.01 on win xp and to be honest it has given me many
problems. I want to use a different and easier complier, I have looked
around but cannot find one.

I see nothing wrong with the program you posted upthread (the second
version, with "int c;"). There may be good reasons to use something
other than TC 2.01 on win xp, but I seriously doubt that it's so
flawed that it would cause the program you posted to go into an
infinite loop.

Compilers do have bugs, but for the elementary kind of stuff you're
currently doing, a compiler bug shouldn't be the first thing you think
of. Make sure that the code you posted is exactly the same as the
code you're compiling, that you've recompiled it, that you're running
it properly, etc.
 
K

Keith Thompson

santosh said:
Here is your first mistake. Fgetc returns an int value, not a char. This
is because the out-of-band value EOF is an int. You need to store
fgetc's return value in an int, test it if it is EOF and assign to a
char only if it is an ordianary character.
Right.


This check will fail because of what I said above.

It won't *necessarily* fail, at least not visibly. I just tried the
program with "char c;", and it "worked". (It fails, by terminating
early, if I insert a 0xff character into the input file.)

It would most likely fail (with an infinite loop) on a system where
plain char is unsigned (it's signed on mine).
 
C

CBFalconer

broli said:
I cannot understand why the program goes into infinite loop after
the file is printed on the screen.

#include<stdio.h>

int main(void)
{

FILE *fp;
char c;

int c;

should do it. Look up the return value from fgetc().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top