>t)//以" />
当前位置:文档之家› 文件输入输出流的几种方法

文件输入输出流的几种方法

#include

int main()
{
std::ofstream fout("C:\helloworld\a.txt");
fout << "Hello World!";
fout.close();
return 0;
}using namespace std;
int main() {
float sum = 0, t;
int count = 0;
ifstream in("data1.txt", ios::in);
if (!in) { cout << "不能打开输入文件:\n"; return -1; }
while (in >> t)//以此读一个实数
{
sum += t, count++;
}
cout << "\n实数的平均值=" << sum / count << ",实数的个数=" << count;
in.close();
return 0;
}





#include
#include
using namespace std;
int main()
{
int a[10],b;
ofstream outfile("D:\\1.txt", ios::out);
if(!outfile)
{
cerr << "open error!" << endl;
cin >> b;
exit(1);
}
cout << "enter 10 interger number:" << endl;
for (int i = 0; i < 10; i++)
{
cout << "请输入第" << i + 1 << "个数字:";
cin >> a[i];
outfile << a[i] << " ";
}

outfile.close();
return 0;
}



#include
#include
using namespace std;
int main() {
char ch, f1[256], f2[256], d;
cout << "请输入源文件名?";
cin >> f1;
cout << "请输入目标文件名?";
cin >> f2;
fstream infile(f1, ios::in);
fstream outfile(f2, ios::out);

if (!infile) { cout << "\n不能打开源文件1"; return -1; }
if (!outfile) { cout << "\n不能打开目标文件2"; return -1; }
infile.unsetf(ios::skipws);//line1
while (infile >> ch)
outfile << ch;
infile.close();
outfile.close();
cout << "\n复制完毕!";
return 0;
}

相关主题
文本预览
相关文档 最新文档