Ok, here is the problem, if I write and read something to a text file like this, it works, no problem:
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);
Now I need to also write numbers in this file, so I do:
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);
Now the cstring does not work and I can`t figure out why... :(
Get Rid Of ff2 >> mycharbuffer
You use ff2 >> mycharbuffer
before you retrieve the first number. So you move the position pointer beyond that line, when you try to input the number it sees a huge long string, and I'm sure it gives you errors.
精彩评论