几个输入输出函数

Posted mayfly-nymph

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了几个输入输出函数相关的知识,希望对你有一定的参考价值。

1.流成员函数put输出单个字符

cout.put(a);//输出字符a

cout.put(65+32)//输出ASCII码a

cout.put(71).put(79).put(79).put(68).put(\n);//输出GOOD

 

倒序输出一个字符串(利用指针从最后一个字符输出)

 1 #include <iostream>
 2 using namespace std;
 3 int main( )
 4 {
 5    char *a="BASIC";//字符指针指向‘B‘
 6    for(int i=4;i>=0;i--)
 7       cout.put(*(a+i));                  //从最后一个字符开始输出
 8    cout.put(\n);

这里面的cout.put()也可以换成putchar();

 

2.cin输入流

IBM PC及兼容机Ctrl+Z表示文件结束符

 

在UNIX和Macintosh系统中,以 Ctrl + D表示文件结束符

 

#include <iostream>

using namespace std;

int main()
{
        float a;

        while(cin >> a){

        if(a<105)

        cout <<"lalala" << endl;

        else

        cout<< "biubiubiu" << endl;
}

cout << "end od file" << endl;

return 0;

}

若输入,99/2,处理方式是先处理99,遇到/,无效字符,cin返回0,循环结束,输出“end of file"

 

2.get()函数读入一个字符(无参,有一个参,有三个参)

不带参数的get函数

cin.get()指定输入流中提取一个字符(包括空白字符),函数提取字符返回字符,遇到文件结束符返回EOF(-1)

#include <iostream>
using namespace std;
int main( )
{
   int c;
   cout<<"enter a sentence:"<<endl;
   while((c=cin.get())!=EOF)
      cout.put(c);
   return 0;
}

getchar()与cin.get()功能类似

有一个参数的get函数

cin.get(ch)

#include <iostream>
using namespace std;
int main( )
{
   char c;
   cout<<"enter a sentence:"<<endl;
   while(cin.get(c))  //读取一个字符赋给字符变量c,如果读取成功,cin.get(c)为真
   {cout.put(c);}
   cout<<"end"<<endl;
   return 0;
}

 

有三个参数的get函数

cin.get(字符数组,字符个数n,终止字符) 或者 cin.get(字符指针,字符个数n,终止字符)

读入n-1个字符,最后是一个字符串结束标志。

 

cin.get(ch,10,‘\\n‘);指定换行符为终止符

 

如不足9个字符,例如:读取4个字符,则在a[4]及以后添加‘\0‘,若多出,例如输入12个字符,则读取9个,ch[9]添加‘\0‘

 

第三个参数不写,默认为‘\n‘

 

3.getline()函数读入一行字符

cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符)

#include <iostream>
using namespace std;
int main( )
{
   char ch[20];
   cout<<"enter a sentence:"<<endl;
   cin>>ch;
   cout<<"The string read with cin is:"<<ch<<endl;
   cin.getline(ch,20,/);  //读个字符或遇‘/‘结束
   cout<<"The second part is:"<<ch<<endl;
   cin.getline(ch,20);  //读个字符或遇‘/n‘结束
   cout<<"The third part is:"<<ch<<endl;
   return 0;
}

enter a sentence: I like C++./I study C++./I am happy.↙
The string read with cin is:I
The second part is: like C++.
The third part is:I study C++./I am h

如果在用cin.getline(ch, 20, ‘/‘)从输入流读取数据时,遇到回车键("\n"),是否 结束读取?结论是此时"\n"不是结束标志"\n"被作为一个字符被读入。

 

在输入流中有一个字符指针,指向当前应访问的字符。在开始时,指针指向第一个字符,在读入第一个字符‘I‘后,指针就移到下一个字符(‘I‘后面的空格),所以

getline函数从空格读起,遇到就停止,把字符串" like c++."存放到ch[0]开始的10个数组元素中,然后用"cout<<ch"输出这10个字符。注意:遇终止标志字符"/"时

停止读取并不放到数组中。

若把cin.getline(ch,20); 改成cin.getline(ch,20,‘/‘);

则运行结果为:
enter a sentence: I like C++./I study C++./I am happy.↙
The string read with cin is: I
The second part is: like C++.
The third part is:    (没有从输人流中读取有效字符)

 

因为,第一个getline输入完毕,指针停留在‘\‘,下一次读取还在从这里,因为换过的代码,遇到‘\‘停止读取。

cin和getline

用“cin<<”读数据时以空白字符(包括空格、tab键、回车键)作为终止标志

用“cin <<”可以读取C++的标准类型的各类型数据(如果经过重载,还可以用于输入自定义类型的数据)

 

cin.getline()读数据时连续读取一系列字符,可以包括空格

用cin.getline()只用于输入字符型数据

以上是关于几个输入输出函数的主要内容,如果未能解决你的问题,请参考以下文章

几个输入输出函数

几个关于js数组方法reduce的经典片段

几个输入输出函数的总结

创建自己的代码片段(CodeSnippet)

关于c语言的几个输入输出函数的问题

几个关于js数组方法reduce的经典片段