在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. continue和pass測试

    >>> for i in range(1,10): print i try:int('sdfa') except:pass 1 2 3 4 5 6 7 8 9 >>> ...

  2. HDU 1075 What Are You Talking About (Trie)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  3. poj3020 Antenna Placement 匈牙利算法求最小覆盖=最大匹配数(自身对应自身情况下要对半) 小圈圈圈点

    /** 题目:poj3020 Antenna Placement 链接:http://poj.org/problem?id=3020 题意: 给一个由'*'或者'o'组成的n*m大小的图,你可以用一个 ...

  4. 修改storm ui 默认端口

    vim conf/storm.yaml 在下面添加 ui.port: 8080

  5. 关于css中层叠性的一点理解

    关于css层叠性的一点理解 标签(空格分隔): html css 我们平时在写css的时候会遇到这样的情况 <!DOCTYPE html> <html lang="en&q ...

  6. 请说明SQLServer中delete from tablea & truncate table tablea的区别

    请说明SQLServer中delete from tablea & truncate table tablea的区别 解答:两者都可以用来删除表中所有的记录.区别在于:truncate是DDL ...

  7. 开发人员需知的Web缓存知识(转)

    什么是Web缓存,为什么要使用它? 缓存的类型: 浏览器缓存: 代理服务器缓存: 网关缓存: Web缓存无害吗?为什么要鼓励缓存? Web缓存如何工作 如何控制缓存和不缓存: HTML Meta标签 ...

  8. 【BZOJ】3433: [Usaco2014 Jan]Recording the Moolympics (贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3433 想了好久啊....... 想不出dp啊......sad 后来看到一英文题解......... ...

  9. css各居中大法

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  10. 66、多种多样的App主界面Tab(1)------ ViewPager实现Tab

    <?xml version="1.0" encoding="utf-8"?> <!-- bottom.xml --> <Linea ...