使用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
随机推荐
- Erlang调度器细节探析
Erlang调度器细节探析 Erlang的很多基础特性使得它成为一个软实时的平台.其中包括垃圾回收机制,详细内容可以参见我的上一篇文章Erlang Garbage Collection Details ...
- python3操作redis
redis也被称为缓存 1.redis是一个key-value存储系统,没有ForeignKey和ManyToMany的字段. 2.在redis中创建的数据彼此之间是没有关系的,所以也被称为是非关系型 ...
- vue-router 二级路由
/** * Created by 我 on 2017/12/4. */ import Vue from 'vue' //import导入 Vue(自己起的名) from 从 vue import Vu ...
- 51NOD 1220 约数之和 [杜教筛]
1220 约数之和 题意:求\(\sum_{i=1}^n \sum_{j=1}^n \sigma_1(ij)\) \[ \sigma_0(ij) = \sum_{x\mid i}\sum_{y\mi ...
- BZOJ 3544: [ONTAK2010]Creative Accounting [set]
给定一个长度为N的数组a和M,求一个区间[l,r],使得$(\sum_{i=l}^{r}{a_i}) mod M$的值最大,求出这个值,注意这里的mod是数学上的mod 这道题真好,题面连LaTeX都 ...
- POJ 2007 Scrambled Polygon [凸包 极角排序]
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8636 Accepted: 4105 ...
- python学习1:程序元素和基本使用方法(跟随mooc学习)
程序元素: 注释,缩进,变量,常量,表达式 输入,输出,分支,循环 示例程序,温度转换程序: #TempConvert.pyval=input("请输入带温度表示符号发温度值(例如:32C) ...
- 安装gitlab8.0在reconfigure报错
现象: https://gitlab.com/gitlab-org/omnibus-gitlab/issues/303 参考方法: https://forum.gitlab.com/t/gitlab- ...
- Ubuntu配置Nginx虚拟主机和支持ThinkPHP
[Nginx配置虚拟主机] 每一个 server { listen 80; server_name www.a.com; ..... } 就表示一台虚拟域名, 然后对应的 ...
- [业界良心系列] OI资料分享
正式退役辣....混吃等死了这么久以后....终于也是必然的结果吧.... 分享一些资料: 链接:http://pan.baidu.com/s/1c1SRFmo 密码:bcfc 有一些资料有版权, 如 ...