在vs2010下,为什么读取文件数据失败!
为什么下列代码在我一个工程里读取数据失败,然而在另外一个工程里能读取?
int n;
TCHAR filter[]=_T("Text File(*.txt)|*.txt|Dat File (*.dat)|*.dat|All Files (*.*)|*.*");
CFileDialog Dlg(true,NULL,NULL,OFN_HIDEREADONLY,filter, NULL);
if(Dlg.DoModal() == IDOK)
{
ifstream fin(Dlg.GetFileName());
while(!fin.eof())
{
fin>>n;
}
fin.close();
}
请高人指教!
[解决办法]
。。。这个工程下有文件没。。。
[解决办法]
while(!fin.eof())
{
//有没有走进这里来?
fin>>n;
}
[解决办法]
Byte and Wide Streams
A byte stream treats a file as a sequence of bytes. Within the program, the stream looks like the same sequence of bytes, except for the possible alterations described above.
By contrast, a wide stream treats a file as a sequence of generalized multibyte characters, which can have a broad range of encoding rules. (Text and binary files are still read and written as described above.) Within the program, the stream looks like the corresponding sequence of wide characters. Conversions between the two representations occur within the Standard C library. The conversion rules can, in principle, be altered by a call to setlocale that changes the category LC_CTYPE. Each wide stream determines its conversion rules at the time it becomes wide-oriented, and retains these rules even if the category LC_CTYPE subsequently changes.
Positioning within a wide stream is subject to the same limitations as for text streams. Moreover, the file-position indicator may well have to deal with a state-dependent encoding. Typically, it includes both a byte offset within the stream and an object of type mbstate_t. Thus, the only reliable way to obtain a file position within a wide stream is by calling fgetpos, and the only reliable way to restore a position obtained this way is by calling fsetpos.
[解决办法]
TCHAR.H routine :_tsetlocale
_UNICODE & _MBCS not defined: setlocale
_MBCS defined: setlocale
_UNICODE defined: _wsetlocale
_tsetlocale( LC_ALL, _T("chs"));
[解决办法]
你都MFC了,还不用CFile?
直接Read早没事情了。