CString and binary file
-
Hi All, I have to open a binary file and read the line. then compare the line with some string. My code is:
ifstream aBinaryfile; string sinfo; aBinaryfile.open(fName,ios::in|ios::binary ); //opening the file getline(aBinaryfile,sinfo); //reading first line //info = sinfo.c_str(); CString info(sinfo.c_str()); //content of info is "FirstLine" if(info.CompareNoCase(TEXT("FirstLine"))==0 { AfxMessageBox("Match"); } else { AfxMessageBox("not Match"); }
The result suppose to be display "Match" but right now I'm getting "not Match" eventhough content of info is "FirstLine". Why they both are not equal when both are suppose to be equal? Is it because I was reading the binary file? Thank you. -
Hi All, I have to open a binary file and read the line. then compare the line with some string. My code is:
ifstream aBinaryfile; string sinfo; aBinaryfile.open(fName,ios::in|ios::binary ); //opening the file getline(aBinaryfile,sinfo); //reading first line //info = sinfo.c_str(); CString info(sinfo.c_str()); //content of info is "FirstLine" if(info.CompareNoCase(TEXT("FirstLine"))==0 { AfxMessageBox("Match"); } else { AfxMessageBox("not Match"); }
The result suppose to be display "Match" but right now I'm getting "not Match" eventhough content of info is "FirstLine". Why they both are not equal when both are suppose to be equal? Is it because I was reading the binary file? Thank you.It doesn't make sense to open a file in binary mode and then use getline, which (sorta) by definition, is a text-mode method. I'm pretty sure the string you read has the newline character at the end, which is NOT a match to your compare string. if you remove the ios::binary flag it should work :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hi All, I have to open a binary file and read the line. then compare the line with some string. My code is:
ifstream aBinaryfile; string sinfo; aBinaryfile.open(fName,ios::in|ios::binary ); //opening the file getline(aBinaryfile,sinfo); //reading first line //info = sinfo.c_str(); CString info(sinfo.c_str()); //content of info is "FirstLine" if(info.CompareNoCase(TEXT("FirstLine"))==0 { AfxMessageBox("Match"); } else { AfxMessageBox("not Match"); }
The result suppose to be display "Match" but right now I'm getting "not Match" eventhough content of info is "FirstLine". Why they both are not equal when both are suppose to be equal? Is it because I was reading the binary file? Thank you.Did you consider setting Breakpoints ? Set a Breakpoint, and SingleStep through the Code. Make (Mental)notes at what you expect to see. vs what's showing. All of us have to do this All the Time, It's Called 'Debugging'
LateNightsInNewry
-
Hi All, I have to open a binary file and read the line. then compare the line with some string. My code is:
ifstream aBinaryfile; string sinfo; aBinaryfile.open(fName,ios::in|ios::binary ); //opening the file getline(aBinaryfile,sinfo); //reading first line //info = sinfo.c_str(); CString info(sinfo.c_str()); //content of info is "FirstLine" if(info.CompareNoCase(TEXT("FirstLine"))==0 { AfxMessageBox("Match"); } else { AfxMessageBox("not Match"); }
The result suppose to be display "Match" but right now I'm getting "not Match" eventhough content of info is "FirstLine". Why they both are not equal when both are suppose to be equal? Is it because I was reading the binary file? Thank you.In addition to the other suggestions, why exactly are you mixing MFC (
CString
) with STL (string
)? There's no reason to use aCString
object just for the sake of comparing.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
It doesn't make sense to open a file in binary mode and then use getline, which (sorta) by definition, is a text-mode method. I'm pretty sure the string you read has the newline character at the end, which is NOT a match to your compare string. if you remove the ios::binary flag it should work :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
In addition to the other suggestions, why exactly are you mixing MFC (
CString
) with STL (string
)? There's no reason to use aCString
object just for the sake of comparing.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
It doesn't make sense to open a file in binary mode and then use getline, which (sorta) by definition, is a text-mode method. I'm pretty sure the string you read has the newline character at the end, which is NOT a match to your compare string. if you remove the ios::binary flag it should work :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
The file that I have to read is UTF-8 text file. And it contains file name that is in eastern characters like Chinese, Japanese, Jew, etc. as shown in following; [begin file names] file1 : c:\temp\פסטיבל סגול ה- 11 - למדיטציה ואהבה.txt file2 : c:\temp\sampleDocument.doc file3 : c:\temp\日本側とのアレン.jpg [/begin file names] I need to store those info, file name, in the same way they appear in the text file. then I have to open those files after I got the name of the file with path from the UTF-8 text file. Therefore, I tried to open the file in the binary mode. Right now, 1) I read the first line, "[begin file names]", then 2) I have to compare if the string that i just read is "[begin file names]" using CString::CompareNoCase(strRead,"[begin file names]") 3) read the next line, which is "file1 : c:\temp\פסטיבל סגול ה- 11 - למדיטציה ואהבה.txt", if CompareNoCase return zero. 4) store the file name . . . etc. right now I am getting "file1: ?????? 11 ????.txt. How can I read the UTF-8 file so I can get the information from the file correctly ? I'm using regular C++. Thanks thanks.
-
bool compareNoCase( const char c1, const char c2 )
{
return toupper(c1) == toupper(c2);
}
...
string str,
strComp("FirstLine");if (str.size() == strComp.size() &&
equal(str.begin(), str.end(), strComp.begin(), compareNoCase))
{
cout << "The strings are equal." << endl;
}
else
cout << "The strings are not equal." << endl;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
The file that I have to read is UTF-8 text file. And it contains file name that is in eastern characters like Chinese, Japanese, Jew, etc. as shown in following; [begin file names] file1 : c:\temp\פסטיבל סגול ה- 11 - למדיטציה ואהבה.txt file2 : c:\temp\sampleDocument.doc file3 : c:\temp\日本側とのアレン.jpg [/begin file names] I need to store those info, file name, in the same way they appear in the text file. then I have to open those files after I got the name of the file with path from the UTF-8 text file. Therefore, I tried to open the file in the binary mode. Right now, 1) I read the first line, "[begin file names]", then 2) I have to compare if the string that i just read is "[begin file names]" using CString::CompareNoCase(strRead,"[begin file names]") 3) read the next line, which is "file1 : c:\temp\פסטיבל סגול ה- 11 - למדיטציה ואהבה.txt", if CompareNoCase return zero. 4) store the file name . . . etc. right now I am getting "file1: ?????? 11 ????.txt. How can I read the UTF-8 file so I can get the information from the file correctly ? I'm using regular C++. Thanks thanks.
pnpfriend wrote:
right now I am getting "file1: ?????? 11 ????.txt
You're getting that where? If you look at the string in binary, are all those question marks the same multibyte character? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder