c:
fopen,fclose
c++:
输入文件:
fstream myfile;
myfile.open("test.txt",ios::out|ios::trunc);
cin>>myfile;
myfile.close;
输出文件:
char data[8];
ifstream myfile;
myfile.open("test.txt");
myfile>>data;
以上都没判断文件是否存在
例如,打开D盘下的a.txt文件:
FILE *fp = fopen("d:\\a.txt", "w");//打开文件
……
fclose(fp);//关闭文件
#include
#include
using namespace std;
int main()
{
string str;
int i;
i=0;
ofstream outfile("test.data",ios::out);
if(!outfile)
{
cerr<<"open error!"<
outfile<<"this is test content!";
}
outfile.close();
ifstream ifile("test.data",ios::in);
if(!ifile)
{
cerr<<"read error!"<
//cout<<"Yes"<
}
ifile.close();
cout<