在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. Cocos3.0 的android返回键功能实现

    比如:Game.h   Game.cpp 头文件Game.h中定义: void onKeyReleased(EventKeyboard::KeyCode keyCode,Event * pEvent) ...

  2. Intellij IDEA和EclipsE之间的的全面对比

    两个IDE之中我最常用的快捷键: 描述 Eclipse IntelliJ 代码补全 Ctrl+space ctrl+space 打开类或者接口 (两个IDE都支持使用"驼峰字符"前 ...

  3. EasyUI项目学习

    介绍easyui的使用,主要包括以下组件 布局面板 - layout 可伸缩面板 - accordion 选项卡 - tabs 控制面板 - panel 窗口 - window 对话框 - dialo ...

  4. Java实现验证码的制作

    验证码概述 为什么使用验证码? 验证码(CAPTCHA)是一种全自动程序.主要是为了区分“进行操作的是不是人”.如果没有验证码机制,将会导致以下的问题: 对特定网站不断进行登录,破解密码: 对某个网站 ...

  5. Sublime Text 加入右键菜单

    Sublime Text 2 是现在很受大家欢迎的编辑器了,不仅是在web前端,在书定简单的php.Js等代码时,也是相当的好用,再配合多种的插件和新颖的界面,更是让人欲罢不能. 在使用时,我们通过喜 ...

  6. 两天学会DirectX 3D之第二天

    提要 前几天非常easy地跑了一个DirectX 9 程序,以为DirectX就那么绘制,事实证明有点Naive了. 之前的那个程序最多也就是个固定流水线的东西. 可是今天要用DirectX11来写一 ...

  7. 使用 Visual Studio 2015 编译 QT 工程

    简单进行一下几步就可以了 1.下载源代码 qt-everywhere-opensource-src-5.6.0-alpha.7z .解压到 D:\ToolKits\5.6.0\src 目录下2.网站 ...

  8. python技巧之下划线(一)

    1.python的moudles文件中__all__作用 Python的moudle是很重要的一个概念,我看到好多人写的moudle里都有一个__init__.py文件.有的__init__.py中是 ...

  9. CentOS firewalld 防火墙操作

    Centos 7 开启端口CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启80端口是不可以的 CentOS 7 采用了 firewalld 防火墙 如要查询 ...

  10. 【BZOJ4870】[Shoi2017]组合数问题 动态规划(矩阵乘法)

    [BZOJ4870][Shoi2017]组合数问题 Description Input 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < ...