[SOLVED] C++ reads in ASCII 255 at the end of files?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am basically just trying to read in a file and output it to the terminal in C++, but I have noticed a strange character at the end of the output every time. I am using Slackware 14.1, gcc v4.8.2, and this occurs on files with both unix and dos line endings.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
ifstream thefile;
char fileName[50], ch;
cout << endl << "Enter filename: ";
cin.getline(fileName, 49);
thefile.open(fileName);
if (thefile.fail())
{
cout << "file not opened" << endl;
return 0;
}
cout << endl;
while (! thefile.eof())
{
ch = thefile.get();
cout << ch;
if (ch == -1) // ASCII 255 or it wraps around to -1
cout << endl << "EOF";
}
return 0;
}
So, I'm trying to figure out why it is outputting "ÿ", ASCII 255 at the end of the file. I have done some research, but I feel like I just don't know what's going on.
ascii 255 is the non breaking space, the html , does your file have an empty ending line or some other character before eof (first tought is about a copy-paste from a web page)?
Last edited by gengisdave; 03-19-2014 at 06:16 PM.
Reason: typo
while (! thefile.eof())
{
ch = thefile.get();
cout << ch;
if (ch == -1) // ASCII 255 or it wraps around to -1
cout << endl << "EOF";
}
Lets look at the loop. If the file pointer is at the last character in the file the while is still true, get() will read the next character which is EOF so cout<<ch will print the character representation which is that strange thing you see. (ch==-1) evaluates true so EOF is printed.
EOF does not get set when you read the last character in the file. It is the next read that sets EOF, but you are printing the returned value (-1) from that unsuccessful read.
Why does EOF work that way? Because it is entirely possible for the file to grow between the time you read the last character and your next read.
Thanks for the replies! So would a do-while loop work better in my case do you think?
Edit: I went ahead and tried it, but it didn't seem to help. I don't recall ever having this problem before when trying to read in files... I'm not sure what I am doing wrong.
Last edited by casualfred; 03-19-2014 at 07:01 PM.
Reason: I tried out what I asked.
Alright, thanks rknichols. So, I could just filter the output like that. I think this is an answer to my question, so I'll mark it as solved.
But, let me be sure I understand what is happening here. There is an EOF flag C++ associates with an opened file. C++ sets that flag when it attempts a read, but finds there is nothing else to read. The failed read will always return this strange character.
Thanks for the great information and for taking the time to explain this stuff!
(It's quite common mistake, almost everyone has it once... getchar/getc/fgetc/... return unsigned char or -1, so the value should be stored in 'int' not 'char')
while not ( eof ) {
get()
if not ( eof ) {
... do something ...
end-if
end-while
Or this way:
Code:
get()
while not ( eof ) {
... do something ...
get()
end-while
Usually, eof indications are produced by attempts to "get" something. They don't look-ahead to see if eof is about to be reached. (The only language that I recall which attempted to do otherwise was Standard Pascal, and it was ugly.)
Your code is basically producing garbage at the last because it has just read past the end of the file. Although the eof indicator will have been tripped thereby, you're not checking for that after the get which ran-dry.
You can always evaluate the file stream object after you have performed a call to get(). C++ ios stream objects (including fstream) implement an operator bool() method that returns the state of the stream.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.