getline from file only works for somet string


I am trying to get string from textfile to be decrypted. The file contain special character after being encrypted. but the problem is, for some kind of string, i manage to get the line from the file, but for some string not eventhough i was using the same code. Here is my code:

string line;
ifstream myfile (filename.c_str());
if (myfile.is_open())
getline (myfile,line);

/*the problem is at this line. some file just return null string eventhough it has content*/
cout<<"line: "<<line.c_str()<<endl;

this is the content from my file that return null string:
þ`‚±íqË FÇÆ4Ò

this is another content from my file that return the string i want:

Is there any character dat forbids me from getting the first string
þ`‚±íqË FÇÆ4Ò ?

As the file contains non-printable characters, you should open it as a binary file, then read it using read() rather than one of the text methods line getline().

On Unix, the binary flag doesn't actually do anything. Some systems treat the end line sequence as more than one character. On Unix, \n is one character, but on some operating systems it is more than one character. For example, DOS and Windows translate it to <carrage return><line feed> (\r\n). Text mode on these systems treat this sequence as a single character. But if you're processing non-printable characters, you need to specify binary mode to avoid this translation.
Last edited on
Topic archived. No new replies allowed.