c++ getline
2016-05-05 16:03
267 查看
#include <iostream>
#include <string>
string Myname;
getline(cin, Myname);
cout<<Myname<<endl;
转载:http://blog.csdn.net/slience_perseverance/article/details/19819601
getline(istream &in, string &s)
从输入流读入一行到string s
•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束
[cpp] view plain
copy
print?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string buff;
ifstream infile;
ofstream outfile;
cout<<"Input file name: "<<endl;
cin>>buff;
infile.open(buff.c_str());
if(!infile)
cout<<"error"<<buff<<endl;
cout<<"Input outfile name: "<<endl;
cin>>buff;
outfile.open(buff.c_str());
while(getline(infile, buff))
outfile<<buff<<endl;
infile.close();
outfile.close();
return 0;
}
#include <iostream>
#include <string>
string Myname;
getline(cin, Myname);
cout<<Myname<<endl;
转载:http://blog.csdn.net/slience_perseverance/article/details/19819601
getline(istream &in, string &s)
从输入流读入一行到string s
•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束
[cpp] view plain
copy
print?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string buff;
ifstream infile;
ofstream outfile;
cout<<"Input file name: "<<endl;
cin>>buff;
infile.open(buff.c_str());
if(!infile)
cout<<"error"<<buff<<endl;
cout<<"Input outfile name: "<<endl;
cin>>buff;
outfile.open(buff.c_str());
while(getline(infile, buff))
outfile<<buff<<endl;
infile.close();
outfile.close();
return 0;
}
相关文章推荐
- C++写入txt文件方法
- C++中的随机数函数
- C++文件读写详解(ofstream,ifstream,fstream)
- c++ primer 学习笔记25 面向对象编程
- C/C++获得当前程序路径
- 运算符重载
- [C/C++11]_[初级]_[使用enumerations类型]
- [C/C++11]_[初级]_[使用enumerations类型]
- 如何用Visual Studio 2013 (vs2013)编写C语言程序
- c++第五次作业
- 谈谈程序设计语言、编译器和开发环境之间的关系
- C++泛型编程
- 《c++精英内参-程序员高效指南》-3如何爱上编码
- Floyd判圈算法
- <C++精英内参-程序员高效指南>-4搭配VS的好用工具
- C++设计模式--工厂模式
- windows下的 c++ socket编程
- c++ 模版template —— c++(复习七)
- c++的多态
- C语言基础: 浮点数转化整数(向上取整、向下取整)| 求绝对值函数