Working with Files
-
Hi, In my MFC app I need to read and write to a file. Write now I am working on the writing part of it all. I can write to a file that doesn't exist, but when the file does exists it always overwrites the contents of the file. I am using the following code:
CStdioFile f; f.Open("test.txt",CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite, NULL); f.WriteString(temp); f.Close();
temp is just some string that the user enters. Can anyone tell me what I am doing wrong, and tell me how would i get it to append to the end when the file exists already? Is there a better way to do this?
-
Hi, In my MFC app I need to read and write to a file. Write now I am working on the writing part of it all. I can write to a file that doesn't exist, but when the file does exists it always overwrites the contents of the file. I am using the following code:
CStdioFile f; f.Open("test.txt",CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite, NULL); f.WriteString(temp); f.Close();
temp is just some string that the user enters. Can anyone tell me what I am doing wrong, and tell me how would i get it to append to the end when the file exists already? Is there a better way to do this?
Put this in the pot and see if it boils:
f.Open("test.txt",CFile::modeCreate | **CFile::modeNoTruncate** | CFile::shareDenyWrite, NULL);
Also, are you sure you need "modeReadWrite"? I doubt you'll ever need that for CStdioFile. "When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein