使用ifstream和getline读取文件内容[c++]
转载:http://www.cnblogs.com/JCSU/articles/1190685.html
假设有一个叫 data.txt 的文件, 它包含以下内容:
Fry: One Jillion dollars.
[Everyone gasps.]
Auctioneer: Sir, that's not a number.
数据读取, 测试 。
以下就是基于 data.txt 的数据读取操作:
#include <iostream>
#include <fstream>
#include <string> using namespace std; //输出空行
void OutPutAnEmptyLine()
{
cout<<"\n";
} //读取方式: 逐词读取, 词之间用空格区分
//read data from the file, Word By Word
//when used in this manner, we'll get space-delimited bits of text from the file
//but all of the whitespace that separated words (including newlines) was lost.
void ReadDataFromFileWBW()
{
ifstream fin("data.txt");
string s;
while( fin >> s )
{
cout << "Read from file: " << s << endl;
}
} //读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
//If we were interested in preserving whitespace,
//we could read the file in Line-By-Line using the I/O getline() function.
void ReadDataFromFileLBLIntoCharArray()
{
ifstream fin("data.txt");
const int LINE_LENGTH = ;
char str[LINE_LENGTH];
while( fin.getline(str,LINE_LENGTH) )
{
cout << "Read from file: " << str << endl;
}
} //读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
//If you want to avoid reading into character arrays,
//you can use the C++ string getline() function to read lines into strings
void ReadDataFromFileLBLIntoString()
{
ifstream fin("data.txt");
string s;
while( getline(fin,s) )
{
cout << "Read from file: " << s << endl;
}
} //带错误检测的读取方式
//Simply evaluating an I/O object in a boolean context will return false
//if any errors have occurred
void ReadDataWithErrChecking()
{
string filename = "dataFUNNY.txt";
ifstream fin( filename.c_str());
if( !fin )
{
cout << "Error opening " << filename << " for input" << endl;
exit(-);
}
} int main()
{
ReadDataFromFileWBW(); //逐词读入字符串
OutPutAnEmptyLine(); //输出空行 ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组
OutPutAnEmptyLine(); //输出空行 ReadDataFromFileLBLIntoString(); //逐词读入字符串
OutPutAnEmptyLine(); //输出空行 ReadDataWithErrChecking(); //带检测的读取
return ;
}
使用ifstream和getline读取文件内容[c++]的更多相关文章
- 使用ifstream和getline读取文件内容[c++] ZZ
假设有一个叫 data.txt 的文件, 它包含以下内容: Fry: One Jillion dollars.[Everyone gasps.]Auctioneer: Sir, that's no ...
- 使用 istreambuf_iterator 读取文件内容,赋值给 std::string
需要一个一个字符输入时考虑使用istreambuf_iterator 假设我们要把一个文本文件拷贝到一个字符串对象中.似乎可以用一种很有道理的方法完成: ifstream inputFile(&quo ...
- shell读取文件内容
Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ------------------------------------ ...
- Python跳过第一行读取文件内容
Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...
- 用c#读取文件内容中文是乱码的解决方法:
用c#读取文件内容中文是乱码的解决方法: //方法1: StreamReader din = new StreamReader(@"C:\1.txt", System.Text.E ...
- android按行读取文件内容的几个方法
一.简单版 import java.io.FileInputStream; void readFileOnLine(){ String strFileName = "Filename.txt ...
- android逐行读取文件内容以及保存为文件
用于长时间使用的apk,并且有规律性的数据 1,逐行读取文件内容 //首先定义一个数据类型,用于保存读取文件的内容 class WeightRecord { String timestamp; flo ...
- 7 RandomAccessFile读取文件内容保存--简单例子(需要验证)
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * 读取动态产生的文件内容 */ publ ...
- shell读取文件内容并进行变量赋值
需求: shell读取文件内容,然后把内容赋值给变量然后进行字符串处理 实现: dataline=$(cat /root/data/data.txt) echo $dataline
随机推荐
- JAVA中默认的编码方式
转:http://blog.csdn.net/scyatcs/article/details/31356823 编码问题存在两个方面:JVM之内和JVM之外.1.Java文件编译后形成class这里J ...
- linux中vim常用的快捷键
移动光标的方法 h或者向左箭头:光标向左移动一个字符 j或者向下箭头:光标向下移动一个字符 k或者向上箭头:光标向上移动一个字符 i或者向右箭头:光标向右移动一个字符 Ctrl+f:屏幕向下移动一页[ ...
- cdh版本的hue安装配置部署以及集成hadoop hbase hive mysql等权威指南
hue下载地址:https://github.com/cloudera/hue hue学习文档地址:http://archive.cloudera.com/cdh5/cdh/5/hue-3.7.0-c ...
- python+xlsxwriter+PIL自动压图贴图到Excel小工具
一.环境 windows10/mac + python3.6 python第三方库 xlsxwriter.PIL.argparse 二.需求 1.运行每条测试case成功与否都需要把截图放在img文件 ...
- 洛谷 P3616 富金森林公园 [树状数组]
传送门 维护一个山脉,单点修改,查询有多少山峰高出水面 我是沙茶沙茶题都不会做只想到无修改可以用扫描线 答案就是所有比水面高的-相邻都比水面高的啊 因为没有区间询问写个$BIT$都可以 有区间询问?可 ...
- python中常见的三种句型if,while,for
1.if语句: 特别说明:条件后面的冒号不能少,同样必须是英文字符. 特别特别说明:if内部的语句需要有一个统一的缩进,一般用4个空格.python用这种方法替代了其他很多编程语言中的{}. num= ...
- Atom Mac安装 有快捷方式
https://jeffjade.com/2016/03/03/2016-03-02-how-to-use-atom/ 如何在 PyCharm 中使用 MacDown 作为外部编辑器 新编码神器Ato ...
- img alt与title的区别
前端 alt是图片加载不出来时候,对图片的文本替代 title 是鼠标放在图片上时,对图片的进一步说明 seo 搜索引擎对图片意思的理解主要靠 alt
- Deep Learning for Information Retrieval
最近关注了一些Deep Learning在Information Retrieval领域的应用,得益于Deep Model在对文本的表达上展现的优势(比如RNN和CNN),我相信在IR的领域引入Dee ...
- 自制PHP高防防盗链(不是一般的高)(思路)
原理:根据IP,资源ID,时间戳,一次性Access_Token,APPKEY(暴露在前台)和APPSERECT(后台)来生成参数,具体见下面: 浏览器请求页面=>后台引用防盗链代码=>生 ...