美文网首页
c++ cin,scanf,cin.getline读取字符串

c++ cin,scanf,cin.getline读取字符串

作者: TFprime | 来源:发表于2019-05-01 14:56 被阅读0次
#include<iostream>
#include<cstring>
using namespace std; 

int main(){
    
    // 1. 使用cin读入字符串
    //    cin只能读入一个空格之前的字符,之后的字符会在下一次读入时读取 
    char c1[100];
    cin >> c1;
    cout << "c1: " << c1 << endl;
    
    // 2. 使用scanf读入字符串
    //    scanf只能读入一个空格之前的字符,之后的字符会在下一次读入时读取  
    char c2[100];
    scanf("%s", c2); // c2是一个地址,所以不需要加&
    printf("c2: %s\n", c2);
    
    // 3. 使用cin.getline读入一整行字符串,包括空格回车等符号 
    //    cin.getline(char buf[], int bufSize);
    char c3[100];
    cin.getline(c3, sizeof(c3));
    cout << "c3: " << c3 << endl;
    
    return 0;
}

相关文章

网友评论

      本文标题:c++ cin,scanf,cin.getline读取字符串

      本文链接:https://www.haomeiwen.com/subject/dhxwnqtx.html