星期二, 十月 25, 2005

On Oct. 25, some tips sharing

If in VC the ERROR:
fatal error C1083: Cannot open precompiled header file: 'Debug/abc.pch': No such file or directory(abc is the name of your project), you can easily just solve it by Project->Setting->C/C++->Category(Choose Precompiled Headers)->Choose Create PreCompiled Headers (the 3nd choice) and fill in "stdafx.h" (no need to file in the quote).

Read file by istream:
What we often do is:
ifstream in;
in.open("dictory");
in>>abc;

However, we will not know when the open operation fails.
Therefore, the Code is changed like this:
ifstream in("dictory/file.dat");
if(!in) return ERROR;
in>>abc;
We can also use :
ifstream in("dictory/file.dat");
if(!in.is_open()) return ERROR;
in>>abc;

However, ifstream will automatically create a file.dat thus the two methods above cannot work well when there is no such a file. We can solve it by add ios::nocreate like this:
ifstream in("dictory/file.dat",ios::nocreate);
if(!in.is_open()) return ERROR;
in>>abc;
You see, that is part of what I learnt yesterday.

没有评论: