|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目: 建立两个磁盘文件f1.dat和f2.dat,编程序实现以下工作:
① 从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);
② 从fl.dat中读入10个数,然后存放到f2.dat文件原有数据的后面;
错误 LNK2005 _main 已经在 test 8.1源.obj 中定义
错误 LNK1169 找到一个或多个多重定义的符号
想问问各位大佬,为什么会出现这种问题?还有,怎么建立磁盘文件?
感谢解答!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- using namespace std;
- //fun1函数从键盘输入20个整数,分别存放在两个磁盘文件中
- void fun1()
- {
- int a[10];
- ofstream outfile1("f1.dat"), outfile2("f2.dat"); //分别建立两个文件流对象
- if (!outfile1)
- {
- cerr << "open f1.dat error!" << endl;
- exit(1);
- }
- if (!outfile2)
- {
- cerr << "open f2.dat error!" << endl;
- exit(1);
- }
- cout << "enter 10 integer numbers:" << endl;
- for (int i = 0; i < 10; i++) //输入10个数存放在f1.dat文件中
- {
- cin >> a[i];
- outfile1 << a[i] << " ";
- }
- cout << "enter 10 integer numbers:" << endl;
- for (int i = 0; i < 10; i++) //输入10个数存放在f2.dat文件中
- {
- cin >> a[i];
- outfile2 << a[i] << " ";
- }
- outfile1.close();
- outfile2.close();
- }
- //从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面
- void fun2()
- {
- ifstream infile("f1.dat"); //f1.dat作为输入文件
- if (!infile)
- {
- cerr << "open f1.dat error!" << endl;
- exit(1);
- }
- ofstream outfile("f2.dat", ios::app); //f2.dat作为输出文件,文件指针指向文件尾,向它写入的数据放在原来数据的后面
- if (!outfile)
- {
- cerr << "open f2.dat error!" << endl;
- exit(1);
- }
- int a;
- for (int i = 0; i < 10; i++)
- {
- infile >> a; //从磁盘文件f2.dat读入一个整数
- outfile << a << " "; //将该读数存放到f2.dat中
- }
- infile.close();
- outfile.close();
- }
- int main()
- {
- fun1();
- fun2();
- cout << "Hello world!" << endl;
- return 0;
- }
复制代码 |
|