<强> 1号强>
# include & lt; stdio.h> # include & lt; stdlib.h> # include & lt; string.h> int main () { int大?1024; char *迷=(char *) malloc(大小);//读取行 而(零!=(浅黄色)){ printf(“阅读与len: % d \ n”, strlen(浅黄色)); printf (" % s ",浅黄色); }//免费的迷 免费(浅黄色); }
<>强利用获取字符()读取一个个字符来读取一行强>
# include & lt; stdio.h> # include & lt; stdlib.h> int my_getline (char *线,int max_size) { int c; int len=0; 在((c=获取字符())!=EOF和,len & lt;max_size) { 线(len + +)=c; 如果(' \ n '==c) 打破; } 线(len)=' \ 0 '; 返回兰; } int main () { int max_size=1024; char *迷=(char *) malloc (sizeof (char) * max_size);//getline int len; 在(0 !=(len=my_getline(浅黄色,max_size))) { printf(“阅读与len: % d \ n”, len); printf (" % s ",浅黄色); } 免费(浅黄色); } >之前
cin.get()和cin.getline () # include使用名称空间性病; int main () { cout & lt; & lt;“- - - - - - - - - - - getline忽略”\ \ n - - - - - - - - - - - - - - - - - -“& lt; & lt;endl; char str0 [30], str1 [30]; cin。getline (str0、30); cin。getline (str1、30); cout & lt; & lt;”str0:“& lt; & lt;str0 & lt; & lt;endl; cout & lt; & lt;”str1:“& lt; & lt;str1 & lt; & lt;endl; cout & lt; & lt;”- - - - - - - - - -利用get()消除得到()遗留下来的‘\ \ n’- - - - - - -”& lt; & lt;endl; char str2 [30], str3 [30]; cin。得到(str2 30) . get ();//注意这里! cin。得到(str3 30) . get (); cout & lt; & lt;”str1:“& lt; & lt;str2 & lt; & lt;endl; cout & lt; & lt;”str2:“& lt; & lt;str3 & lt; & lt;endl; cout & lt; & lt;”- - - - - - - - -没消除get()遗留下来的“\ \ n”就被下一个得到()读取了,所以str5输出为空- - - - - -“& lt; & lt;endl; char str4 [30], str5 [30]; cin。得到(str4 30);//注意这里! cin。得到(str5 30); cout & lt; & lt;”str4:“& lt; & lt;str4 & lt; & lt;endl; cout & lt; & lt;”str5:“& lt; & lt;str5 & lt; & lt;endl; 返回0; } >之前
以上这篇c读取一行字符串,以及c++读取一行字符串的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
c读取一行字符串,以及c++读取一行字符串的实例