getline() C/C++差异

C++原型(Win32和Linux):

#include <iostream>
istream& getline ( istream &is , string &str , char delim );
istream& getline ( istream& , string& );

说明:

在终结符的处理上(默认换行符作为终结符)
在遇到终结符delim后,delim会被丢弃,不存入str中。在下次读入操作时,将在delim的下个字符开始读入

测试代码:

#include <iostream>
#include <string>
int read_line(void)
{
    std::string buf;
    getline(std::cin, buf);

    std::cout << buf;
    return 0;    
}

C原型(Linux):

#include <stdio.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream);

测试代码:

#include <stdio.h>
int read_line_c(void)
{
    char buffer[100];
    getline(buffer, sizeof(buffer), stdin);
    printf(buffer);
}

说明:

DESCRIPTION
getline() reads an entire line from stream, storing the address of  the
buffer  containing  the  text into *lineptr.  The buffer is null-terminated and includes the newline character, if one was found. 
用于读取一行字符直到换行符,包括换行符

备注:
相关的函数,对于fgets()函数,两个平台下都会读取换行符,gets()函数,两个平台下都不会读取换行符

参考链接:

http://baike.baidu.com/view/3127321.htm

http://baike.baidu.com/view/8684247.htm


发布了231 篇原创文章 · 获赞 5 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章