输入流/输出流

输入流/输出流

  • 输入流操作

  • cin>>

接受一个字符串,遇“空格”、“TAB”、“回车”都结束

  • cin.get()

用法1单个接收 cin.get(字符变量名)可以用来接收字符

#include 
using namespace std;
main ()
{
char ch;
ch=cin.get();   //或者cin.get(ch);
cout<

用法2:cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格

#include 
using namespace std;
main ()
{
char a[20];
cin.get(a,20);
cout<

用法3:cin.get(无参数)没有参数主要是用于舍弃输入流中的不需要的字符,或者舍弃回车,弥补cin.get(字符数组名,接收字符数目)的不足.

  • cin.getline()

接受一个字符串,可以接收空格并输出

#include 
using namespace std;
main ()
{
char m[20];
cin.getline(m,5);
cout<
  • getline()

接受一个字符串,可以接收空格并输出

#include
#include
using namespace std;
main ()
{
string str;
getline(cin,str);
cout<

cin.getline()属于istream流,而getline()属于string流

  • gets()

接受一个字符串,可以接收空格并输出

#include
#include
using namespace std;
main ()
{
   char m[20];
   gets(m);     //不能写成m=gets();
   cout<

以上内容参考到的链接

  • 输出流操作

  • 流错误状态

  • 文件处理

  • 打开文件

fstream txt;
txt.open("a.txt,ios::in");
string str;
while(!txt.eof()){
   getline(txt,str);
}
  • 关闭文件

txt.close()

你可能感兴趣的:(c++)