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 and Writing to a file at the same time

Reading and Writing to a file at the same time

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
15 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.
  • N Naveen

    if ur using CFile in MFC open the file as follows.. CFile File; File.Open( _T("C:\\out.txt"),CFile::modeReadWrite ); Now u can use both Read and Write function CFile.. nave

    W Offline
    W Offline
    waxie
    wrote on last edited by
    #6

    I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy

    K N D 3 Replies Last reply
    0
    • W waxie

      I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy

      K Offline
      K Offline
      kakan
      wrote on last edited by
      #7

      First: Beacuse the file pointer is placed on the character after "String" in the file, so that's where you will be writing "Overwrite String" You will have to position the file pointer to the beginning of "String" before writing to the file. Second: Since "Overwrite String" is longer than "String", if the file pointer was placed at the beginning of "String", the "String " would be overwritten, *but* also the 10 characters after "String" in the file. Pls read my post.

      W 1 Reply Last reply
      0
      • W waxie

        I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #8

        Can u give more details.. What happened? did the execution comes to oFile.WriteString(_T("Overwrite String"));.. more over.. When u read usinf ReadString, the filepointer advance to the next line..So before writting u musk seek back to the staring of the line that u have to replace.. nave

        1 Reply Last reply
        0
        • K kakan

          First: Beacuse the file pointer is placed on the character after "String" in the file, so that's where you will be writing "Overwrite String" You will have to position the file pointer to the beginning of "String" before writing to the file. Second: Since "Overwrite String" is longer than "String", if the file pointer was placed at the beginning of "String", the "String " would be overwritten, *but* also the 10 characters after "String" in the file. Pls read my post.

          W Offline
          W Offline
          waxie
          wrote on last edited by
          #9

          Yeah, OK, say the pointer is really not in the right position. But why doesn't my 'overwriting' string appear in any parts of the text file? Am I correct to assume that it should appear? Say, on the next line? Thanks so much for all your replies.

          K 1 Reply Last reply
          0
          • W waxie

            Yeah, OK, say the pointer is really not in the right position. But why doesn't my 'overwriting' string appear in any parts of the text file? Am I correct to assume that it should appear? Say, on the next line? Thanks so much for all your replies.

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #10

            If you did open the file for reading and writing (see Naveen's post), then it should be writtened to the file, immediately after "String". -- modified at 4:51 Tuesday 16th May, 2006 That is, provided the expression if(csCurrLine==_T("String")) evaluates to a value != 0

            W 1 Reply Last reply
            0
            • K kakan

              If you did open the file for reading and writing (see Naveen's post), then it should be writtened to the file, immediately after "String". -- modified at 4:51 Tuesday 16th May, 2006 That is, provided the expression if(csCurrLine==_T("String")) evaluates to a value != 0

              W Offline
              W Offline
              waxie
              wrote on last edited by
              #11

              Honestly, it really does not work. My new string does not appear anywhere in the file - or as expected, after the old string.

              K 1 Reply Last reply
              0
              • W waxie

                Honestly, it really does not work. My new string does not appear anywhere in the file - or as expected, after the old string.

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #12

                OK, I beleive you! :) I can think of two things: First: Does the expression if(csCurrLine==_T("String")) evaluate to a value != 0 That is, does your program find "String" in your present file? Second: Do you check the return code of the write-statement?

                W 1 Reply Last reply
                0
                • K kakan

                  OK, I beleive you! :) I can think of two things: First: Does the expression if(csCurrLine==_T("String")) evaluate to a value != 0 That is, does your program find "String" in your present file? Second: Do you check the return code of the write-statement?

                  W Offline
                  W Offline
                  waxie
                  wrote on last edited by
                  #13

                  Yes, the program finds the evaluated string. I'm sure with that. And yes, it can write coz i tried writing to a file without reading and it does write. WriteString doesn't have a return value - it returns void.

                  K 1 Reply Last reply
                  0
                  • W waxie

                    Yes, the program finds the evaluated string. I'm sure with that. And yes, it can write coz i tried writing to a file without reading and it does write. WriteString doesn't have a return value - it returns void.

                    K Offline
                    K Offline
                    kakan
                    wrote on last edited by
                    #14

                    OK, it doesn't have a return value, but it does throw an exception for error conditions? Does it? There simply must be a reason why the file won't change after Write.

                    1 Reply Last reply
                    0
                    • W waxie

                      I wonder... why doesn't this code work? CString csCurrLine; while(oFile.ReadString(csCurrLine)) { if(csCurrLine==_T("String")) { oFile.WriteString(_T("Overwrite String")); break; } } oItemListFile.Close(); TheBuggy

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

                      Why does WriteString() fail?

                      TRY
                      {
                      oFile.WriteString(_T("Overwrite String"));
                      }
                      CATCH(CFileException, pEx)
                      {
                      AfxMessageBox(_T("Exception encountered."));
                      }
                      END_CATCH


                      "The largest fire starts but with the smallest spark." - David Crow

                      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