Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString and binary file

CString and binary file

Scheduled Pinned Locked Moved C / C++ / MFC
iosregexquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pnpfriend
    wrote on last edited by
    #1

    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.

    M B D 3 Replies Last reply
    0
    • P pnpfriend

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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

      P 2 Replies Last reply
      0
      • P pnpfriend

        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.

        B Offline
        B Offline
        Bram van Kampen
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • P pnpfriend

          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.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          In addition to the other suggestions, why exactly are you mixing MFC (CString) with STL (string)? There's no reason to use a CString 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

          P 1 Reply Last reply
          0
          • M Mark Salsbery

            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

            P Offline
            P Offline
            pnpfriend
            wrote on last edited by
            #5

            The file contains eastern chars in the file. In order to read the eastern chars I think I do need to open it in binary mode, dont' I? thanks.

            1 Reply Last reply
            0
            • D David Crow

              In addition to the other suggestions, why exactly are you mixing MFC (CString) with STL (string)? There's no reason to use a CString 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

              P Offline
              P Offline
              pnpfriend
              wrote on last edited by
              #6

              I tried to use MFC CString becasue there is CompareNoCase().

              D 1 Reply Last reply
              0
              • M Mark Salsbery

                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

                P Offline
                P Offline
                pnpfriend
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • P pnpfriend

                  I tried to use MFC CString becasue there is CompareNoCase().

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • P pnpfriend

                    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.

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups