troubleshooting Question

fstream file read write not working in vs2005

Avatar of cjm20
cjm20Flag for United States of America asked on
Programming Languages-OtherC++
5 Comments1 Solution706 ViewsLast Modified:
I have code in a VC6 application to read and write to a text file.  The code works fine.  However, this same code in a VS2005 application does not work.  It compiles without error, and the reading works properly (as I can see by stepping thru the code in the debugger).  However, the file writing does NOT work.  Instead, the file is not getting written to, even though in the debugger the code to write to the file IS getting executed.

Here's a code sample:

fstream f;
f.open(strMyFileName, ios::in | ios::out);

CString strStuffToWrite = "Some Cool Stuff To Write to the File";
char* charForWritingToFile = NULL;
char charForReadingBlankLine[2];
f.read(charForReadingBlankLine,  2);//advance past the first blank line in the file

charForWritingToFile = (char*)malloc(strlen(strStuffToWrite) + 1);
strcpy_s(myChar, strlen(strStuffToWrite) + 1, strStuffToWrite);       //.net
f.write(myChar, strStuffToWrite.GetLength());  

So again, this code runs, but when I open the file after it has run, the file is unchanged, i.e., the stuff I wanted to write to the file has NOT replaced the stuff that was ORIGINALLY in the file.  (Yet in VC6 this code worked perfectly)

Any help would be greatly appreciated.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros