problem with fgetc

P

pavan734

Hi I have got a file "file_data.in" that contains the following data:
abcdef
I have a code that reads character by character & display it. But to
my surprise it is printing one character more at the end. I dont want
the last character to be printed. Here is the code I have written:
//code
FILE* input_file ;
input_file = fopen("file_data.in", "r");
char file_data;
cout << "Printing input data\n" ;
do
{
file_data = fgetc(input_file);
if(file_data == EOF)
{
cout << "break\n" ;
break;
}
cout << "File data = " << file_data << endl ;
}while(file_data !=EOF);
The output I got is :

Printing input data
File data = a
File data = b
File data = c
File data = d
File data = e
File data = f
File data =

break
 
I

Ian Collins

Hi I have got a file "file_data.in" that contains the following data:
abcdef
I have a code that reads character by character & display it. But to
my surprise it is printing one character more at the end. I dont want
the last character to be printed. Here is the code I have written:

Is there a '\n' at the end of your input?
//code
FILE* input_file ;
input_file = fopen("file_data.in", "r");

Any good reason for using C style I/O?
 
P

pavan734

Is there a '\n' at the end of your input?


Any good reason for using C style I/O?

No, there is no \n. Please try running the code. I dont have option to
attach the file. Otherwise I would have done it.
 
K

Kai-Uwe Bux

No, there is no \n.

Try opening the file in binary mode. It is conceivable that your OS is
adding \n when it thinks you are finished reading the last line of a text
file.
Please try running the code. I dont have option to
attach the file. Otherwise I would have done it.


Best

Kai-Uwe Bux
 
I

Ian Collins

*Please* don't quote signatures!
No, there is no \n. Please try running the code. I dont have option to
attach the file. Otherwise I would have done it.
I bet there is, change the output line to

cout << "File data = " << (int)file_data << endl;

and see what you get.
 
P

pavan734

*Please* don't quote signatures!


I bet there is, change the output line to

cout << "File data = " << (int)file_data << endl;

and see what you get.

Yes you are true. It is also reading \n, but I didnt do "Enter" . Tell
me how to avoid this
 
P

pavan734

Try opening the file in binary mode. It is conceivable that your OS is
adding \n when it thinks you are finished reading the last line of a text
file.


Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -

Even if I open in binary mode, Iam getting the same problem
 
P

P.J. Plauger

Hi I have got a file "file_data.in" that contains the following data:
abcdef
I have a code that reads character by character & display it. But to
my surprise it is printing one character more at the end. I dont want
the last character to be printed. Here is the code I have written:
//code
FILE* input_file ;
input_file = fopen("file_data.in", "r");
char file_data;

Here's one pending problem:

EOF is an int value, which you're later storing in this
char object. If char has a signed representation, you'll
luck out (at the cost of mistaking 0xff for EOF).
cout << "Printing input data\n" ;
do
{
file_data = fgetc(input_file);
if(file_data == EOF)
{
cout << "break\n" ;
break;
}
cout << "File data = " << file_data << endl ;
}while(file_data !=EOF);
The output I got is :

Printing input data
File data = a
File data = b
File data = c
File data = d
File data = e
File data = f
File data =

break

A C text file is supposed to end on a newline, and it looks
like that's what you're reading and printing out. If the
file *doesn't* containe a newline, the C runtime may well
be supplying one, or doing something else funny, right at
the end of the file.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
J

James Kanze

I bet there is, change the output line to
cout << "File data = " << (int)file_data << endl;
and see what you get.
[/QUOTE]
Yes you are true. It is also reading \n, but I didnt do "Enter" . Tell
me how to avoid this

Well, the new line is in the file, so the problem is with
whatever program generates the file. Good text editors
generally have an option to force a newline at the end of all
files, and some text editors never do otherwise, at least when
editing text. A number of programs have, historically, had
problems when text files didn't end with a new line. Note that
in text mode, if the file doesn't end with a new line, the
behavior may vary---you may see a new line anyway, you may see
exactly what is there, or you may not see anything behind the
last new line.
 

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
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top