求getline()函数具体用法,给个例子!

Mingci_why 2009-08-15 01:48:05
getline()这个函数有什么功能啊,能否给个例子,越详细越好 谢谢!
...全文
179 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mingci_why 2009-08-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ljx87085210 的回复:]
csdn上大家答问题真踊跃啊。
[/Quote]
嘿嘿是啊,这就是我喜欢来CSDN的原因。
ljx87085210 2009-08-15
  • 打赏
  • 举报
回复
csdn上大家答问题真踊跃啊。
  • 打赏
  • 举报
回复
http://www.diybl.com/course/3_program/c++/cppjs/20090403/164143.html
这时一篇对比的文章,可以看看。
wanjingwei 2009-08-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lly212 的回复:]
getline:

cin.getline(地址,大小,结束符); 针对c格式字符串 由于不直接读取一串字符有越界危险

getline(对象,对象名,结束符);  主要面对string

知道上面就简单了
string name;
getline(cin,name); //读取name 默认是'\n'结束

char name[20];
cin.getline(name,19);
name[19] = '\0';
[/Quote]
UP
lly212 2009-08-15
  • 打赏
  • 举报
回复
getline:

cin.getline(地址,大小,结束符); 针对c格式字符串 由于不直接读取一串字符有越界危险

getline(对象,对象名,结束符); 主要面对string

知道上面就简单了
string name;
getline(cin,name); //读取name 默认是'\n'结束

char name[20];
cin.getline(name,19);
name[19] = '\0';
Mingci_why 2009-08-15
  • 打赏
  • 举报
回复
0.0 例子怎么那么的长
starcat 2009-08-15
  • 打赏
  • 举报
回复
读取一行到s数组中,返回该行的长度

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print the longest input line */
main()
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */

max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /* there was a line */
printf("%s", longest);
return 0;
}

/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}

/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
int i;

i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
starcat 2009-08-15
  • 打赏
  • 举报
回复
这个貌似不是标准的库函数
而是C圣经上的例题
mstlq 2009-08-15
  • 打赏
  • 举报
回复
Example
// string_getline_sample.cpp
// compile with: /EHsc
// Illustrates how to use the getline function to read a
// line of text from the keyboard.
//
// Functions:
//
// getline Returns a string from the input stream.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

int main()
{
string s1;
cout << "Enter a sentence (use <space> as the delimiter): ";
getline(cin,s1, ' ');
cout << "You entered: " << s1 << endl;;
}


Input
test this

Sample Output
Enter a sentence (use <space> as the delimiter): test this
You entered: test
mstlq 2009-08-15
  • 打赏
  • 举报
回复
basic_istream::getline
Gets a line from the input stream.

basic_istream& getline(
char_type *_Str,
streamsize _Count
);
basic_istream& getline(
char_type *_Str,
streamsize _Count,
char_type _Delim
);

Parameters
_Count
The number of characters to read from strbuf.
_Delim
The character that should terminate the read if it is encountered before _Count.
_Str
A string in which to write.
Return Value
The stream (*this).
Remarks
The first of these unformatted input functions returns getline(_Str, _Count, widen('\n')).
The second function extracts up to _Count - 1 elements and stores them in the array beginning at _Str. It always stores char_type after any extracted elements it stores. In order of testing, extraction stops:
At end of file.
After the function extracts an element that compares equal to _Delim, in which case the element is neither put back nor appended to the controlled sequence.
After the function extracts _Count - 1 elements.
If the function extracts no elements or _Count - 1 elements, it calls setstate(failbit). In any case, it returns *this.
Example
// basic_istream_getline.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main( )
{
char c[10];

cin.getline( &c[0], 5, '2' );
cout << c << endl;
}
Input
12
Output
1

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧