C++关于文件流的初级问题解决

今天在调试一个初级的文件流程序时出现了如下的情况。源程序如下:

#include <fstream>
#include <iostream>
void main(){
 using namespace std;
 ifstream in("a.in");
 ofstream out("a.out");
 for (string str;getline(in,str);){
   out<<str<<endl;

 }
}

编译时报错:

--------------------Configuration: new2 - Win32 Debug--------------------
Compiling...
n1.cpp
E:/VC6.0/MSDev98/MyProjects/new2/n1.cpp(8) : error C2065: 'getline' : undeclared identifier
E:/VC6.0/MSDev98/MyProjects/new2/n1.cpp(9) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable
 conversion)
执行 cl.exe 时出错.

n1.obj - 1 error(s), 0 warning(s)
我在网上查询关于这个问题的解决方法时,没有找到解决方法,因为许多人都注意到了环境、头文件的对错问题。我无意间看到了一个人写的错误的程序中有string这个头文件,我就试着加入这个头文件,结果问题解决了。

修改后的代码如下:

#include <fstream>
#include <iostream>
#include <string>
void main(){
 using namespace std;
 ifstream in("a.in");
 ofstream out("a.out");
 for (string str;getline(in,str);){
  out<<str<<endl;
  
 }
}

一切OK。

但是问题在于,为什么其他人编写这个程序时就没有写入string头文件,但是也成功运行了呢?这就不得而知了,继续关注……

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值