|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
问题如下,写在代码里了,路过的鱼油看看,本咸鱼就先睡了
- #include<fstream>
- #include<iostream>
- using namespace std;
- int main (void)
- {
- fstream fp("hellow.txt",ios::in | ios::app);
-
- if(!fp)
- {
- cout<<"文件打开失败!"<<endl;
- return 0;
- }
-
- /*for(int i=0;i<10;i++) //若不屏蔽掉,后面的输出就执行不了!
- {
- char j;
- cin>>j;
- fp<<j;
- }*/
-
- cout<<"正在打印!"<<endl;
-
- char x;
- while(fp>>x)
- {
- cout<<x;
- }
- fp<<endl;
- fp.close();
-
- return 0;
-
- }
复制代码
你这边输入完成后,没把文件指针设到开头
所以会失败
这边输入完后,将文件指针设到开头,开始读取,以下是代码:在你23行的位置加了一行代码,将文件指针设到开头即可
- #include<fstream>
- #include<iostream>
- using namespace std;
- int main(void)
- {
- fstream fp("hellow.txt", ios::in | ios::app);
- if (!fp)
- {
- cout << "文件打开失败!" << endl;
- return 0;
- }
- for(int i=0;i<10;i++) //若不屏蔽掉,后面的输出就执行不了!
- {
- char j;
- cin>>j;
- fp<<j;
- }
- fp.seekg(ios::beg);
- cout << "正在打印!" << endl;
- char x;
- while (fp >> x)
- {
- cout << x;
- }
- fp << endl;
- fp.close();
- return 0;
- }
复制代码
|
|