欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

c讀取一行字符串,以及c++讀取一行字符串的實(shí)例

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

一 c讀取一行字符串

1 gets

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 
 int size = 1024; 
 char* buff = (char*)malloc(size); 

 // read lines 
 while(NULL != gets(buff)){ 
 printf("Read line with len: %d\n", strlen(buff)); 
 printf("%s", buff); 
 } 

 // free buff 
 free(buff); 
} 

利用getchar()讀取一個(gè)個(gè)字符來讀取一行

#include <stdio.h> 
#include <stdlib.h> 

int my_getline(char* line, int max_size) 
{ 
 int c; 
 int len = 0; 
 while( (c = getchar()) != EOF && len < max_size ){ 
 line[len++] = c; 
 if('\n' == c) 
  break; 
 } 


 line[len] = '\0'; 
 return len; 
} 

int main() 
{ 
 int max_size = 1024; 
 char* buff = (char*)malloc( sizeof(char) * max_size ); 

 //getline 
 int len; 
 while(0 != (len = my_getline(buff, max_size))){ 
 printf("Read line with len: %d\n", len); 
 printf("%s", buff); 
 } 

 free(buff); 
} 

二 c++讀取一行字符串

cin.get()和cin.getline()
#include<iostream>

using namespace std;

int main()
{

 cout << "----------getline忽略'\\n-----------------" << endl;
 char str0[30], str1[30];
 cin.getline(str0, 30);
 cin.getline(str1, 30);
 cout << "str0:" << str0 << endl;
 cout << "str1:" << str1 << endl;

 cout << "---------利用get()消除get()遺留下來的'\\n'-------" << endl;
 char str2[30], str3[30];
 cin.get(str2, 30).get(); // 注意這里!
 cin.get(str3, 30).get();
 cout << "str1: " << str2 << endl;
 cout << "str2: " << str3 << endl;

 cout << "--------沒消除get()遺留下來的'\\n'就被下一個(gè)get()讀取了,所以str5輸出為空-----" << endl;
 char str4[30], str5[30];
 cin.get(str4, 30); // 注意這里!
 cin.get(str5, 30);
 cout << "str4: " << str4 << endl;
 cout << "str5: " << str5 << endl;
 return 0;

}

以上這篇c讀取一行字符串,以及c++讀取一行字符串的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

上一篇:C/C++獲取鍵盤事件的方法

欄    目:C語言

下一篇:C語言實(shí)現(xiàn)linux網(wǎng)卡檢測精簡版

本文標(biāo)題:c讀取一行字符串,以及c++讀取一行字符串的實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/751.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有