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. A silly question about CFile

A silly question about CFile

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionhelp
4 Posts 3 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.
  • B Offline
    B Offline
    bin8922
    wrote on last edited by
    #1

    I'd like to know how to append a file with a string. For example, I have a CStdio file or CArchive file. How to add a string at the end? I tried the following code: StdioFile file(fileName, CFile::modeCreate|CFile::modeReadWrite); file.SeekToEnd(); file.WriteString("New String"); file.Flush(); but it will wipe the old content away. The only way I can think of is to store the content first and then write stored strings and new string in. But that is too stupid. Is there anything easy to solve this problem. Thanks a lot in advance.

    A 1 Reply Last reply
    0
    • B bin8922

      I'd like to know how to append a file with a string. For example, I have a CStdio file or CArchive file. How to add a string at the end? I tried the following code: StdioFile file(fileName, CFile::modeCreate|CFile::modeReadWrite); file.SeekToEnd(); file.WriteString("New String"); file.Flush(); but it will wipe the old content away. The only way I can think of is to store the content first and then write stored strings and new string in. But that is too stupid. Is there anything easy to solve this problem. Thanks a lot in advance.

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      The reason for the failure is not in the way you write to the file; it is in the way you open it. The CFile::modeCreate flag instructs the constructor to create a new file. If there is an existing file with the same name, it is truncated to zero length, thus wiping it clean. In order to accomplish the desired result, you must open the file with a combination (OR) of flags CFile::modeCreate and CFile::modeNoTruncate. This combination will, if the file exists, open the file normally, and if it doesn't, it will create a new file. Removing both flags causes the function to fail if the file doesn't exist. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

      A B 2 Replies Last reply
      0
      • A Antti Keskinen

        The reason for the failure is not in the way you write to the file; it is in the way you open it. The CFile::modeCreate flag instructs the constructor to create a new file. If there is an existing file with the same name, it is truncated to zero length, thus wiping it clean. In order to accomplish the desired result, you must open the file with a combination (OR) of flags CFile::modeCreate and CFile::modeNoTruncate. This combination will, if the file exists, open the file normally, and if it doesn't, it will create a new file. Removing both flags causes the function to fail if the file doesn't exist. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

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

        What he said.

        1 Reply Last reply
        0
        • A Antti Keskinen

          The reason for the failure is not in the way you write to the file; it is in the way you open it. The CFile::modeCreate flag instructs the constructor to create a new file. If there is an existing file with the same name, it is truncated to zero length, thus wiping it clean. In order to accomplish the desired result, you must open the file with a combination (OR) of flags CFile::modeCreate and CFile::modeNoTruncate. This combination will, if the file exists, open the file normally, and if it doesn't, it will create a new file. Removing both flags causes the function to fail if the file doesn't exist. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

          B Offline
          B Offline
          bin8922
          wrote on last edited by
          #4

          Thank you very much. Best wishes.

          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