A silly question about CFile
-
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.
-
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.
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 flagsCFile::modeCreate
andCFile::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. -
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 flagsCFile::modeCreate
andCFile::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. -
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 flagsCFile::modeCreate
andCFile::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.