C/C++中的行读取
在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 );
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++中的行读取的更多相关文章
- python之从文件中按行读取数据
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' # 从文件中按行读取数据 file = open(& ...
- C++/Php/Python/Shell 程序按行读取文件或者控制台
写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...
- C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...
- 读取memo中某行内容
方法1 可用以下代码读取Memo中指定行的内容: var aLine:String; begin aLine:=Memo1.Lines[2]; end; 在使用中,读取的行在Memo中需要保证 ...
- [C#]统计文本文件txt中的行数(快速读取)
快速统计文本文件中的行数( StreamReader.ReadLine() ): 测试代码如下: //读取txt文件中总行数的方法 public static int requestMethod(St ...
- Java中文本文件的读取(按行读取)
在之前的学习过程中,经常会遇到将文本文件中的数据读取到数组或其他数据结构中.每次遇到,总是在网上搜索代码解决,解决之后并没有总结复习,因此在下一次遇到同样的问题时,又重复之前的过程.这样周而复始,并没 ...
- C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据
练习8.1:编写函数.接受一个istream&参数,返回值类型也是istream&.此函数必须从给定流中读取数据,直至遇到文件结束标识时停止. #include <iostrea ...
- DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)
/// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...
- 关于一些对map和整行读取文件操作
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
随机推荐
- 1.3 Services - 服务
服务是一种应用组件,它可以在后台执行耗时的操作,它是没有用户界面的.其它的应用组件都可以开启一个服务,服务开启后,即使用户离开了应用,服务仍然可以在后台运行.此外,绑定到服务的组件可以与服务进行交互, ...
- hive2.1.1配置
hive2.1.1配置 首先在conf下复制hive-site.xml <property> <name>javax.jdo.option.ConnectionURL</ ...
- hadoop 调试mapperduce
问题描述 运行hadoop的MapReduce示例,在running job卡住 在页面中显示一直处于 ACCEPTED Applications 状态 修改日志级别export HADOOP_ROO ...
- 最短路中部分点仅仅能从中随意选取K个问题
题意:给N个点,还有另外m个点(当中仅仅能选K个).求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其它点了),若该点是m个点中的,则count[i]=原来的+1:若不是. ...
- Java IO系统
理论上,我们可将任何程序分割为三部分:输入.处理和输出.这意味着 IO(输入/输出)是所有程序最为关键的部分.在这一章中,大家将学习Java 为此提供的各种类,如何用它们读写文件.内存块以及控制台 等 ...
- 2017 ACM区域赛(西安) 参赛流水账
day 0: 周五, 鸽了概统课,早上和紫金港的几位小伙伴一起打车去萧山机场,从咸阳机场到西北工业大学坐了五十多个站的公交车,感觉身体被掏空.晚上在宾馆本来打算补之前训练的一个题,想想还是先花个十来分 ...
- 首次接触XAMPP,端口被占用困恼
本人运气比较好,首次安装XAMPP就碰到了各种问题啊!并且已经解决,以下是我问题的出处并且解决. 问题描述: apache无法打开,并且连带的出现了mySql无法打开.(即80端口冲突问题) 解决办法 ...
- IOS 分节表视图
分节表视图 简单的表视图仅仅有一个节,它实际上是分节表视图的一个特例.一个表能够有多个节,节也有头有脚,分节是加入索引和分组的前提. 索引的正确使用原则如所看到的: 1.索引标题不能与显示的标题全然一 ...
- js ie 6,7,8 使用不了 firstElementChild
一. <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementB ...
- Linux CentOS7.2下安装Redis && 配置Redis开机自启动
1.安装redis 第一步:下载redis安装包 wget http://download.redis.io/releases/redis-4.0.11.tar.gz 第二步:解压压缩包 tar -z ...