c++ I/O

来源:互联网 发布:windows 10安装大小 编辑:程序博客网 时间:2024/06/02 21:09

对于以下输入:

8 23  24

6
7
95




程序A:

#include<iostream>
using namespace std;

int main()
{
 while(fin>>k)
  cout<< k<<endl;
return 0;
}

显示结果是:

8

23 

24

6
7
95


程序B的结果跟A一样,不过是从文件vertor.in取得输入:


#include<iostream>
using namespace std;
#include<fstream>

int main()
{
ifstream fin("vertor.in");
int k;
while(fin>>k)
cout<< k<<endl;
return 0;
}
~     

程序C 每次读一整行:

#include<iostream>
using namespace std;

int main()
{

string s;
 while(getline(cin,s))
  cout<<s<<endl;
return 0;
}
其输入结果是:

8 23  24

6
7
95


程序D:是从vertor.in中取得 输入,效果一样:

#include<iostream>
using namespace std;
#include<fstream>

int main()
{
ifstream fin("vertor.in");

string s;
 while(getline(fin,s))
  cout<<s<<endl;
return 0;
}
~     

原创粉丝点击