CStdioFile类的有关问题
求助:CStdioFile类的问题
#include <afx.h>
#include <iostream.h>
void main(){
CStdioFile myFile;
if (!myFile.Open( "c:\\1.txt", CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite) )return;
CString str,istr;//栈中分配
if(myFile.GetLength()==0){
str="123456\n4455555\n";
myFile.WriteString(str);
}else{
istr="123456\n4455555\n";
//位置1
while (myFile.ReadString(str))
{
cout<<str<<endl;
}
myFile.WriteString(istr);
}
}
这样先读再写是没有问题的,但是如果把WriteString提到位置1,就会发现读的是一堆乱码,而且会有异常,难道不能先写再读么
------解决方案--------------------
仅供参考
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
#include <afx.h>
#include <iostream.h>
void main(){
CStdioFile myFile;
if (!myFile.Open( "c:\\1.txt", CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite) )return;
CString str,istr;//栈中分配
if(myFile.GetLength()==0){
str="123456\n4455555\n";
myFile.WriteString(str);
}else{
istr="123456\n4455555\n";
//位置1
while (myFile.ReadString(str))
{
cout<<str<<endl;
}
myFile.WriteString(istr);
}
}
这样先读再写是没有问题的,但是如果把WriteString提到位置1,就会发现读的是一堆乱码,而且会有异常,难道不能先写再读么
------解决方案--------------------
仅供参考
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.