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. CStdIOFile ????

CStdIOFile ????

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • S Offline
    S Offline
    Shay Harel
    wrote on last edited by
    #1

    Hi, I need to open a file and appened to it later, from some reason, it get's overwritten, here is the most simple code.... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::modeCreate |CFile::typeText); log_file.WriteString("123\n"); ///do something....... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::typeText |CFile::modeNoTruncate); log_file.WriteString("456\n"); end result in the file is always "456". Why????

    4 L T 3 Replies Last reply
    0
    • S Shay Harel

      Hi, I need to open a file and appened to it later, from some reason, it get's overwritten, here is the most simple code.... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::modeCreate |CFile::typeText); log_file.WriteString("123\n"); ///do something....... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::typeText |CFile::modeNoTruncate); log_file.WriteString("456\n"); end result in the file is always "456". Why????

      4 Offline
      4 Offline
      4apai
      wrote on last edited by
      #2

      try to use retrived handle in fopen function with a+ flag instead of recalling constructor 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

      S 1 Reply Last reply
      0
      • 4 4apai

        try to use retrived handle in fopen function with a+ flag instead of recalling constructor 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

        S Offline
        S Offline
        Shay Harel
        wrote on last edited by
        #3

        I don't want to use fopen etc.... The reason why I started using CStdioFile is because Visual Studio .NET did not support the anymore

        4 1 Reply Last reply
        0
        • S Shay Harel

          I don't want to use fopen etc.... The reason why I started using CStdioFile is because Visual Studio .NET did not support the anymore

          4 Offline
          4 Offline
          4apai
          wrote on last edited by
          #4

          use CreateFile!? it always help me with) 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

          1 Reply Last reply
          0
          • S Shay Harel

            Hi, I need to open a file and appened to it later, from some reason, it get's overwritten, here is the most simple code.... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::modeCreate |CFile::typeText); log_file.WriteString("123\n"); ///do something....... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::typeText |CFile::modeNoTruncate); log_file.WriteString("456\n"); end result in the file is always "456". Why????

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file Artificial intelligence is no match for natural stupidity.

            S 1 Reply Last reply
            0
            • S Shay Harel

              Hi, I need to open a file and appened to it later, from some reason, it get's overwritten, here is the most simple code.... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::modeCreate |CFile::typeText); log_file.WriteString("123\n"); ///do something....... CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::typeText |CFile::modeNoTruncate); log_file.WriteString("456\n"); end result in the file is always "456". Why????

              T Offline
              T Offline
              Tom Wright
              wrote on last edited by
              #6

              Here's whay I do and it works everytime. CStdioFile myFile; myFile.Open (FileName, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate, NULL); myFile.SeekToEnd(); myFile.WriteString (strLSbuf); myFile.Close (); I think the problem you are running into is that you are not first placing the cursor to the end of the line then writing out your string. So when you do an open on the file, the cursor is placed at the beginning of the line and you overwrite your existing data. Hope this help Tom Wright tawright915@yahoo.com

              S 1 Reply Last reply
              0
              • L Lost User

                CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file Artificial intelligence is no match for natural stupidity.

                S Offline
                S Offline
                Shay Harel
                wrote on last edited by
                #7

                So.... You suggest to change the second time I open the file to: CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::typeText ||CFile::modeCreate |CFile::modeNoTruncate); I already tried that, just like the the microsoft site say, still no change, this is crazy, I know.....

                L 1 Reply Last reply
                0
                • T Tom Wright

                  Here's whay I do and it works everytime. CStdioFile myFile; myFile.Open (FileName, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate, NULL); myFile.SeekToEnd(); myFile.WriteString (strLSbuf); myFile.Close (); I think the problem you are running into is that you are not first placing the cursor to the end of the line then writing out your string. So when you do an open on the file, the cursor is placed at the beginning of the line and you overwrite your existing data. Hope this help Tom Wright tawright915@yahoo.com

                  S Offline
                  S Offline
                  Shay Harel
                  wrote on last edited by
                  #8

                  AAAAAnnnndddddd.... we have a winner !!! Thanks a lot. When I worked with ofstream, and use the ios::app flag it was done by default I guess. Have a great day.

                  1 Reply Last reply
                  0
                  • S Shay Harel

                    So.... You suggest to change the second time I open the file to: CStdioFile log_file (_T("file.txt"),CFile::modeWrite |CFile::typeText ||CFile::modeCreate |CFile::modeNoTruncate); I already tried that, just like the the microsoft site say, still no change, this is crazy, I know.....

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Ah yes... stupid of me... you have to SeekToEnd() before you write again Artificial intelligence is no match for natural stupidity.

                    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