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. Reading delimited files...

Reading delimited files...

Scheduled Pinned Locked Moved C / C++ / MFC
c++javahelp
5 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.
  • J Offline
    J Offline
    Joshua Guy
    wrote on last edited by
    #1

    As you probably noticed from this post I am a very new newbie to VC++. Here is my problem. I need to read in a test file that might have a format like this... string, int, int. I know in java you can use a string tokenizer to read in each value separatly and by line. I can't seem to figure this out in C++. This is what I have been doing. CFile myFile("test.txt", CFile::modeWrite); BYTE buffer[4096]; CString input; input = myFile.Read(buffer, 4096); Once I get it into a string object I can use the method SpanExcluding to get a string from the beginning to the first character I specify not to read. I can't seem to get past it thought. I know my approach is probably all wrong so I don't care if you tear it to shreads. I frequently visit this site and know you guys know what you;re doing so thanks for any advice in advance. Joshua

    M R 2 Replies Last reply
    0
    • J Joshua Guy

      As you probably noticed from this post I am a very new newbie to VC++. Here is my problem. I need to read in a test file that might have a format like this... string, int, int. I know in java you can use a string tokenizer to read in each value separatly and by line. I can't seem to figure this out in C++. This is what I have been doing. CFile myFile("test.txt", CFile::modeWrite); BYTE buffer[4096]; CString input; input = myFile.Read(buffer, 4096); Once I get it into a string object I can use the method SpanExcluding to get a string from the beginning to the first character I specify not to read. I can't seem to get past it thought. I know my approach is probably all wrong so I don't care if you tear it to shreads. I frequently visit this site and know you guys know what you;re doing so thanks for any advice in advance. Joshua

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Joshua, use CStdioFile instead of CFile; it lets you read in a line at a time. For grabbing the tokens, you can use the CRT function strtok(), or the undocumented MFC function AfxExtractSubString(). You use the function thus: // assuming you have a CString sLine that holds the next line... CString sStringToken, sIntToken1, sIntToken2; AfxExtractSubString ( sStringToken, sLine, 0, ',' ); AfxExtractSubString ( sIntToken1, sLine, 1, ',' ); AfxExtractSubString ( sIntToken2, sLine, 2, ',' ); The prototype for AfxExtractSubString() is: BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n'); where rString is where the token is placed, lpszFullString is the string to search, iSubString is the token to pull out (a 0-based count), and chSep is the delimiter character.

      J 1 Reply Last reply
      0
      • J Joshua Guy

        As you probably noticed from this post I am a very new newbie to VC++. Here is my problem. I need to read in a test file that might have a format like this... string, int, int. I know in java you can use a string tokenizer to read in each value separatly and by line. I can't seem to figure this out in C++. This is what I have been doing. CFile myFile("test.txt", CFile::modeWrite); BYTE buffer[4096]; CString input; input = myFile.Read(buffer, 4096); Once I get it into a string object I can use the method SpanExcluding to get a string from the beginning to the first character I specify not to read. I can't seem to get past it thought. I know my approach is probably all wrong so I don't care if you tear it to shreads. I frequently visit this site and know you guys know what you;re doing so thanks for any advice in advance. Joshua

        R Offline
        R Offline
        Roger Scudder
        wrote on last edited by
        #3

        To add to what Michael has given you... Text file are usually much easier to handle if you use CStdioFile instead of CFile. CStdioFile allows you to easly read each line in a loop. For example... CStdioFile f; // construct a CStdioFile object char buffer[256]; // define a temp buffer while (f.ReadString(szBuffer, 255)) // tokenize string and process Roger C.

        J 1 Reply Last reply
        0
        • M Michael Dunn

          Joshua, use CStdioFile instead of CFile; it lets you read in a line at a time. For grabbing the tokens, you can use the CRT function strtok(), or the undocumented MFC function AfxExtractSubString(). You use the function thus: // assuming you have a CString sLine that holds the next line... CString sStringToken, sIntToken1, sIntToken2; AfxExtractSubString ( sStringToken, sLine, 0, ',' ); AfxExtractSubString ( sIntToken1, sLine, 1, ',' ); AfxExtractSubString ( sIntToken2, sLine, 2, ',' ); The prototype for AfxExtractSubString() is: BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n'); where rString is where the token is placed, lpszFullString is the string to search, iSubString is the token to pull out (a 0-based count), and chSep is the delimiter character.

          J Offline
          J Offline
          Joshua
          wrote on last edited by
          #4

          Thanks for the insight. This is exactly what I was looking for, I just didn't know where to go. Thanks again for your time. Joshua

          1 Reply Last reply
          0
          • R Roger Scudder

            To add to what Michael has given you... Text file are usually much easier to handle if you use CStdioFile instead of CFile. CStdioFile allows you to easly read each line in a loop. For example... CStdioFile f; // construct a CStdioFile object char buffer[256]; // define a temp buffer while (f.ReadString(szBuffer, 255)) // tokenize string and process Roger C.

            J Offline
            J Offline
            Joshua
            wrote on last edited by
            #5

            Thank you also Roger. This is exactly the help I was looking for. I works great. Joshua

            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