c++ 1. 有一个整型数组,含10个元素,从键盘输入10个整数给数组,将此数组送到磁盘文件“D:\file1.txt”中存放。
时间: 2024-03-05 19:47:26 浏览: 48
好的,下面是C++代码实现:
```c++
#include <iostream>
#include <fstream> // 文件操作头文件
using namespace std;
int main()
{
int nums[10];
cout << "请输入10个整数:" << endl;
for(int i = 0; i < 10; i++)
{
cin >> nums[i];
}
ofstream outfile("D:\\file1.txt"); // 创建输出文件流对象
if(outfile) // 判断文件是否打开成功
{
for(int i = 0; i < 10; i++)
{
outfile << nums[i] << " "; // 写入数据到文件中
}
outfile.close(); // 关闭文件
cout << "数据已经成功写入文件!" << endl;
}
else
{
cout << "文件打开失败!" << endl;
}
return 0;
}
```
运行程序后,会提示输入10个整数,输入完后,程序会将这10个整数写入到文件“D:\file1.txt”中。如果文件打开成功,程序会输出“数据已经成功写入文件!”的提示信息。