在C语言里面一直很容易混淆的,gets和fgetS的区别:

char * fgets ( char * str, int num, FILE * stream );
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

A terminating null character is automatically appended after the characters copied to str.

Notice that fgets is quite different from gets: not only fgets accepts a stream argument, but also allows to specify the maximum size of str and includes in the string any ending newline character.
char * gets ( char * str );
Get string from stdin
Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or theend-of-file is reached.

The newline character, if found, is not copied into str.

A terminating null character is automatically appended after the characters copied to str.

Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

简单说来,fgets能够指定最大输入字符数(num - 1),他们都在遇到换行符的之后终止,fgets把换行符也读进字符串里,gets简单的丢掉换行符。

C++中对应的有cin的四个函数:

istream & get(char *, int, char);
istream & get(char *, int);
istream & getline(char *, int, char);
istream & getline(char *, int);

这里,get和getline都不会把换行符读进去,但是get会把换行符(或者指定的其他分隔符)留在流里面,而getline会把换行符从流中读取出来并丢弃。

												

C/C++中的行读取的更多相关文章

  1. python之从文件中按行读取数据

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' # 从文件中按行读取数据 file = open(& ...

  2. C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...

  3. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。

    C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...

  4. 读取memo中某行内容

    方法1 可用以下代码读取Memo中指定行的内容: var   aLine:String; begin   aLine:=Memo1.Lines[2]; end; 在使用中,读取的行在Memo中需要保证 ...

  5. [C#]统计文本文件txt中的行数(快速读取)

    快速统计文本文件中的行数( StreamReader.ReadLine() ): 测试代码如下: //读取txt文件中总行数的方法 public static int requestMethod(St ...

  6. Java中文本文件的读取(按行读取)

    在之前的学习过程中,经常会遇到将文本文件中的数据读取到数组或其他数据结构中.每次遇到,总是在网上搜索代码解决,解决之后并没有总结复习,因此在下一次遇到同样的问题时,又重复之前的过程.这样周而复始,并没 ...

  7. C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据

    练习8.1:编写函数.接受一个istream&参数,返回值类型也是istream&.此函数必须从给定流中读取数据,直至遇到文件结束标识时停止. #include <iostrea ...

  8. DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)

    /// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...

  9. 关于一些对map和整行读取文件操作

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

随机推荐

  1. hive2.1.1配置

    hive2.1.1配置 首先在conf下复制hive-site.xml <property> <name>javax.jdo.option.ConnectionURL</ ...

  2. 为什么 MongoDB 连接数被用满了?

    使用 MongoDB 时,可能会遇到因为 mongod 连接数用满了,导致客户端无法连接的问题.mongod的最大连接数通过 net.maxIncomingConnections 指定,默认值为100 ...

  3. thinkphp nginx 上配置 并解决get获取到数据现象

    server { listen 80; server_name XXXX.funova.net XXX.funova.com; root /opt/newgm; index index.php; lo ...

  4. (一)Spring容器相关操作

    一.spring事件 spring的事件有如下两个成员. 1.ApplicationEvent,容器事件,由容器发布 2.ApplicationListener 监听器,可以由容器中的任何监听器Bea ...

  5. Xcode The operation couldn’t be completed. (NSURLErrorDomain error -1012.)

    使用Xcode SVN 出现问题 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 解决方法: 打开终端 然后输 ...

  6. 一入python深似海--变量和对象

    一.基本原理 Python中一切都是对象,变量是对象的引用. 这是一个普遍的法则.我们举个样例来说.Python是怎样来处理的. x = 'blue' y = 'green' z = x 当pytho ...

  7. 禁用chrome默认打印框直接打印

    在做web打印的时候 重要弹出一个对话框选择打印机等等, 比较影响操作, 经过一番谷歌终于找到了解决方案 在桌面的chrome 图标 右键 打开属性对话框 在目标 添加  --kiosk-printi ...

  8. hibernate中inverse作用

    默认 inverse="false"即该元素指向的类负责维护该关系. 如: <hibernate-mapping> <class name="com.h ...

  9. 存一些可能会用得到的vue的UI框架

    VUX 项目主页:https://vux.li/#/ github地址:https://github.com/airyland/vux element UI(饿了么后台) Element 是由饿了么U ...

  10. 支付宝app支付提示 系统繁忙,请稍后重试

    v2版本的支付宝支付,无论怎么调试返回的都是系统繁忙,请稍后重试提示.经过对给的示例代码的仔细研究,最后发现是由于老版本的sign_type 不加入签名计算,而新版本的支付签名它是加入签名计算的.供大 ...