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

    waxie wrote:

    we can only read to a file first, close it, then write to it,

    Who said this ? we can both, read and write to a file at the same ..For this u must open the file in read-write mode.. nave

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

    Really? can you give me a snippet of the working code please? Thanks! TheBuggy

    N 1 Reply Last reply
    0
    • W waxie

      Really? can you give me a snippet of the working code please? Thanks! TheBuggy

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

      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 1 Reply Last reply
      0
      • W waxie

        Hi guys, I have been teaching myself c++/mfc, and I was wondering if there is a way to read and write to a file at the same time? Like read the file first and if it encounters this text, replace that text with a new text (so write to the file)? I observed that we can only read to a file first, close it, then write to it, or vice versa. But is there a way to do both? I did not want to use fseek() since my number of bytes per line are not fixed. I'd appreciate all the help I can get. And if I got a concept wrong, please do correct me. Thanks a lot!:-D :rose:The Buggy

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

        Hello. You can see a file as a number of bytes. There are no intervening spaces or "records" in it. You can open a file for both reading and writing, but then you (usually) has to fseek between read and writes. You can overwrite parts of a file, but you can't insert (or delete) new bytes in the beginning or middle of an existing file. (Acually, you can, but that means you have to write the "move" code yourself, and it won't be very effective). The only way you can change an existing file is at the end. You can append to the file or remove (by setting the file length to a shorter value than before. The easiest way to change the contents of a file (other than overwriting a fix number of bytes) is to create a temp file, and then rename the temp file to the name of the original file.

        1 Reply Last reply
        0
        • 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