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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值