首页上一页 1 下一页尾页 3 条记录 1/1页
关于《Visual C++文件处理的问题》
发表在C语言图书答疑
2012-10-06
是否精华
是
否
版块置顶:
是
否
[strong][font color=#0000FF]以下一段代码是《视频学Viual C++》5.5章里的一个Exam。原程序是没有加名字空间的。但是我发现当我加了名字空间以后,程序执行到一半就结束了。本人就一菜鸟,求解惑![/font]
[font color=#FF0000]文件处理代码(原程序不带名字空间):[/font]
#include "iostream.h"
#include "fstream.h"
#include "iomanip"
int main()
{
fstream infile;
fstream outfile;
char name[20];
char str[30];
cout<<"请输入文件:"<<"\n";
cin>>name;
outfile.open(name,ios::out | ios::trunc);
if(!outfile)
{
cout<<"写文件打开失败!";
exit(1);
}
cout<<"请输入要写入文件的数据:"<<"\n";
cin>>str;
outfile<<str;
outfile.close();
infile.open(name,ios::in);
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
char c;
cout<<"输出文件中数据:"<<"\n";
while(infile.get(c))
{
cout<<c;
}
cout<<"\n";
infile.close();
outfile.open(name,ios::out | ios::app);
if(!outfile)
{
cout<<"写文件打开失败!";
exit(1);
}
cout<<"输入要在文件末尾添加数据:"<<"\n";
cin>>str;
outfile<<str;
outfile.close();
infile.open(name,ios::in);
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
cout<<"输出文件中数据:"<<"\n";
while(infile.get(c))
{
cout<<c;
}
cout<<"\n";
infile.close();
return 0;
}
[font color=#FF0000]文件处理代码(本人写的带名字空间):[/font]
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void OperateFile()
{
fstream outfile;
fstream infile;
char str[30];
char name[20];
cout << "请输入要打开的文件名," << endl;
cin >> name;
outfile.open(name,ios::out | ios::trunc);
if(!outfile)
{
cout << "写文件打开失败!" << endl;
exit(1);
}
cout << "请输入要写放文件的数据" <<endl;
cin >> str;
outfile << str;
outfile.close();
infile.open(name,ios::in);
if(!infile)
{
cout << "读文件打开失败!" <<endl;
exit(1);
}
char c;
while(infile.get(c))
{
cout << c;
}
cout << endl;
infile.close();
outfile.open(name,ios::out | ios::app);
if(!outfile)
{
cout << "写文件打开失败!" << endl;
exit(1);
}
cout << "请输入要在文件末尾添加的数据:" << endl;
cin >> str;
outfile << str;
outfile.close();
cout << "重新读取文件" <<endl;
infile.open(name,ios::in); //执行至此处失败;
if(!infile)
{
cout << "读文件打开失败!" << endl;
exit(1);
}
while(infile.get(c))
{
cout << c;
}
cout << endl;
infile.close();
}
int main()
{
OperateFile();
return 0;
}
[/strong]
[font color=#FF0000]文件处理代码(原程序不带名字空间):[/font]
#include "iostream.h"
#include "fstream.h"
#include "iomanip"
int main()
{
fstream infile;
fstream outfile;
char name[20];
char str[30];
cout<<"请输入文件:"<<"\n";
cin>>name;
outfile.open(name,ios::out | ios::trunc);
if(!outfile)
{
cout<<"写文件打开失败!";
exit(1);
}
cout<<"请输入要写入文件的数据:"<<"\n";
cin>>str;
outfile<<str;
outfile.close();
infile.open(name,ios::in);
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
char c;
cout<<"输出文件中数据:"<<"\n";
while(infile.get(c))
{
cout<<c;
}
cout<<"\n";
infile.close();
outfile.open(name,ios::out | ios::app);
if(!outfile)
{
cout<<"写文件打开失败!";
exit(1);
}
cout<<"输入要在文件末尾添加数据:"<<"\n";
cin>>str;
outfile<<str;
outfile.close();
infile.open(name,ios::in);
if(!infile)
{
cout<<"读文件打开失败!";
exit(1);
}
cout<<"输出文件中数据:"<<"\n";
while(infile.get(c))
{
cout<<c;
}
cout<<"\n";
infile.close();
return 0;
}
[font color=#FF0000]文件处理代码(本人写的带名字空间):[/font]
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void OperateFile()
{
fstream outfile;
fstream infile;
char str[30];
char name[20];
cout << "请输入要打开的文件名," << endl;
cin >> name;
outfile.open(name,ios::out | ios::trunc);
if(!outfile)
{
cout << "写文件打开失败!" << endl;
exit(1);
}
cout << "请输入要写放文件的数据" <<endl;
cin >> str;
outfile << str;
outfile.close();
infile.open(name,ios::in);
if(!infile)
{
cout << "读文件打开失败!" <<endl;
exit(1);
}
char c;
while(infile.get(c))
{
cout << c;
}
cout << endl;
infile.close();
outfile.open(name,ios::out | ios::app);
if(!outfile)
{
cout << "写文件打开失败!" << endl;
exit(1);
}
cout << "请输入要在文件末尾添加的数据:" << endl;
cin >> str;
outfile << str;
outfile.close();
cout << "重新读取文件" <<endl;
infile.open(name,ios::in); //执行至此处失败;
if(!infile)
{
cout << "读文件打开失败!" << endl;
exit(1);
}
while(infile.get(c))
{
cout << c;
}
cout << endl;
infile.close();
}
int main()
{
OperateFile();
return 0;
}
[/strong]