读/写文本文件,奇怪的问题让我疯了

时间:2011-04-14 14:49:11

标签: text

好的,这就是问题所在,如果我写这样的文本文件并将其读取,它就可以了,没问题:

fstream ff,ff2;
ff.open("simtestagain.txt",ios::out);
CString mycstring = _T("Name with spaces");
char mycharbuffer[255];         //destination buffer
size_t convertedChars = 0;      //number of characters converted
wcstombs_s( &convertedChars, mycharbuffer, mycstring.GetLength()+1, mycstring.GetBuffer(), _TRUNCATE);
ff << mycharbuffer; 

ff.close();

ff2.open("simtestagain.txt",ios::in);

ff2.getline(mycharbuffer,255);
mycstring = mycharbuffer;

ff2.close();
AfxMessageBox(mycstring);

现在我还需要在这个文件中写数字,所以我这样做:

fstream ff,ff2;
int a,b;
ff.open("simtestagain.txt",ios::out);
CString mycstring = _T("Name with spaces");
char mycharbuffer[255];         //destination buffer
size_t convertedChars = 0;      //number of characters converted
wcstombs_s( &convertedChars, mycharbuffer, mycstring.GetLength()+1, mycstring.GetBuffer(), _TRUNCATE);
ff << 1 << endl;
ff << mycharbuffer << endl; 
ff << 2 << endl;

ff.close();

ff2.open("simtestagain.txt",ios::in);

//EDIT: copy/paste error, not in code //ff2 >> mycharbuffer;
ff2 >> a;
ff2.getline(mycharbuffer,255);
mycstring = mycharbuffer;
ff >> b;

ff2.close();
AfxMessageBox(mycstring);

现在cstring不起作用,我不明白为什么...... :(

1 个答案:

答案 0 :(得分:0)

摆脱ff2 >> mycharbuffer

在检索第一个号码之前使用ff2 >> mycharbuffer。因此,当您尝试输入它看到一个巨大的长字符串的数字时,您将位置指针移到该行之外,我确信它会给您带来错误。