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. File I/O w/ Unicode Dialog [modified]

File I/O w/ Unicode Dialog [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 2 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.
  • A Offline
    A Offline
    aquawicket
    wrote on last edited by
    #1

    I've been racking my brain all day with this... Just can't get things going.. I need to be able to read and write variables to a file.. I've got one example to work.. But the reading I get back are box characters... This prolly has to do with the fact that my progam is in UNICODE mode. I'm just trying to read and write in a CSV file. (comma seperated values). But anything that is capable of reading and writing variables to a file would be great.. Example: // Open the text file we want CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead); CArchive ar (&cfFile, CArchive::load); // Load its contents into a CArchive // Initialise the variable which holds each line's contents CString strLine = L""; if(!ar.ReadString(strLine)) // Read the first line of the CArchive into the variable return; // Failed, so quit out do // Repeat while there are lines in the file left to process { if(strLine.GetLength() == 0) // If the line is empty, skip it continue; CString strText = strLine; // A line of the file // Initialise the variables that will hold the values CString strItemName = L""; CString strPicPath = L""; CString strSoundPath = L""; // Extract the first value, and place it in the strItemName variable AfxExtractSubString(strItemName, strText, 0, ','); // Extract the second value, and place it in the strPicPath variable AfxExtractSubString(strPicPath, strText, 1, ','); // Extract the third value, and place it in the strSoundPath variable AfxExtractSubString(strSoundPath, strText, 2, ','); // Do something with these values in the variables }while(ar.ReadString(strLine)); If I display strItemName, strPicPath or strSoundPath after reading the file... I get blocks.. any ideas? -- modified at 22:34 Tuesday 31st October, 2006

    M 1 Reply Last reply
    0
    • A aquawicket

      I've been racking my brain all day with this... Just can't get things going.. I need to be able to read and write variables to a file.. I've got one example to work.. But the reading I get back are box characters... This prolly has to do with the fact that my progam is in UNICODE mode. I'm just trying to read and write in a CSV file. (comma seperated values). But anything that is capable of reading and writing variables to a file would be great.. Example: // Open the text file we want CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead); CArchive ar (&cfFile, CArchive::load); // Load its contents into a CArchive // Initialise the variable which holds each line's contents CString strLine = L""; if(!ar.ReadString(strLine)) // Read the first line of the CArchive into the variable return; // Failed, so quit out do // Repeat while there are lines in the file left to process { if(strLine.GetLength() == 0) // If the line is empty, skip it continue; CString strText = strLine; // A line of the file // Initialise the variables that will hold the values CString strItemName = L""; CString strPicPath = L""; CString strSoundPath = L""; // Extract the first value, and place it in the strItemName variable AfxExtractSubString(strItemName, strText, 0, ','); // Extract the second value, and place it in the strPicPath variable AfxExtractSubString(strPicPath, strText, 1, ','); // Extract the third value, and place it in the strSoundPath variable AfxExtractSubString(strSoundPath, strText, 2, ','); // Do something with these values in the variables }while(ar.ReadString(strLine)); If I display strItemName, strPicPath or strSoundPath after reading the file... I get blocks.. any ideas? -- modified at 22:34 Tuesday 31st October, 2006

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

      After this line CString strText = strLine; // A line of the file does strText have a line of comma separated strings? Don't know if it's necessary but try making your comma constants L',' AfxExtractSubString(strItemName, strText, 0, L',');

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        After this line CString strText = strLine; // A line of the file does strText have a line of comma separated strings? Don't know if it's necessary but try making your comma constants L',' AfxExtractSubString(strItemName, strText, 0, L',');

        A Offline
        A Offline
        aquawicket
        wrote on last edited by
        #3

        hmmmm.. still dosn't display the character correctly.. Their must be a simple way to read and write int variables to a file.. I don't even really need strings.

        M 1 Reply Last reply
        0
        • A aquawicket

          hmmmm.. still dosn't display the character correctly.. Their must be a simple way to read and write int variables to a file.. I don't even really need strings.

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

          Did you write the ints as ints or as string representations of the ints?? If you don't need to read the file in notepad or the file is not imported to other applications then you can write binary ints instead of comma-separated strings.

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            Did you write the ints as ints or as string representations of the ints?? If you don't need to read the file in notepad or the file is not imported to other applications then you can write binary ints instead of comma-separated strings.

            A Offline
            A Offline
            aquawicket
            wrote on last edited by
            #5

            ok... sounds good.. Just as long as the program can write to the file and read it back to itself. What should I look up for binary reading and writing?

            M 1 Reply Last reply
            0
            • A aquawicket

              ok... sounds good.. Just as long as the program can write to the file and read it back to itself. What should I look up for binary reading and writing?

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

              For int values use CArchive::Read() and CArchive::Write() or the overloaded extraction/insertion operators, something like // to write an int int IntValue = ...; ar.Write(&IntValue, sizeof(int)); // or "ar << IntValue;" // to read an int int IntValue; ar.Read(&IntValue, sizeof(int)); // or "ar >> IntValue;"

              A 1 Reply Last reply
              0
              • M Mark Salsbery

                For int values use CArchive::Read() and CArchive::Write() or the overloaded extraction/insertion operators, something like // to write an int int IntValue = ...; ar.Write(&IntValue, sizeof(int)); // or "ar << IntValue;" // to read an int int IntValue; ar.Read(&IntValue, sizeof(int)); // or "ar >> IntValue;"

                A Offline
                A Offline
                aquawicket
                wrote on last edited by
                #7

                Ok... I know i'm real close.. lol CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeReadWrite ); char buf[512]; int variable = 12; int i; CArchive ar( &cfFile, CArchive::store, 512, buf ); ar.Write(&variable, sizeof(int)); ar.Close(); CArchive ar2( &cfFile, CArchive::load, 512, buf ); i = ar2.Read(&variable, sizeof(int)); ar2.Close(); temp.Format((L"%d"), i); AfxMessageBox(temp); //dispalys 0... need 12

                A 1 Reply Last reply
                0
                • A aquawicket

                  Ok... I know i'm real close.. lol CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeReadWrite ); char buf[512]; int variable = 12; int i; CArchive ar( &cfFile, CArchive::store, 512, buf ); ar.Write(&variable, sizeof(int)); ar.Close(); CArchive ar2( &cfFile, CArchive::load, 512, buf ); i = ar2.Read(&variable, sizeof(int)); ar2.Close(); temp.Format((L"%d"), i); AfxMessageBox(temp); //dispalys 0... need 12

                  A Offline
                  A Offline
                  aquawicket
                  wrote on last edited by
                  #8

                  Nevermind... I got it :) Thank you very very much Mark... very much appreciated :) CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeReadWrite ); char buf[512]; Learning = 12; cfFile.SeekToBegin(); CArchive ar( &cfFile, CArchive::store, 512, buf ); ar.Write(&Learning, sizeof(int)); ar.Close(); cfFile.SeekToBegin(); CArchive ar2( &cfFile, CArchive::load, 512, buf ); ar2.Read(&i, sizeof(int)); ar2.Close(); temp.Format((L"%d"), i); AfxMessageBox(temp);

                  M 1 Reply Last reply
                  0
                  • A aquawicket

                    Nevermind... I got it :) Thank you very very much Mark... very much appreciated :) CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeReadWrite ); char buf[512]; Learning = 12; cfFile.SeekToBegin(); CArchive ar( &cfFile, CArchive::store, 512, buf ); ar.Write(&Learning, sizeof(int)); ar.Close(); cfFile.SeekToBegin(); CArchive ar2( &cfFile, CArchive::load, 512, buf ); ar2.Read(&i, sizeof(int)); ar2.Close(); temp.Format((L"%d"), i); AfxMessageBox(temp);

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

                    The file pointer, yes :) Take care, Mark

                    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