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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章