File Input and Output

Use of File Stream

Assume ifle and ofile is the string object storing the names of input and output files‘ namess.

string ifile = "inputFile.txt";
string ofile = "outputFile.txt";

Then the use of file stream is like this:

// construct an ifstream and bind it to the file named ifile
ifstream infile(ifile.c_str());
// ofstream output file object to write file named ofile
ofstream outfile(ofile.c_str());

Also, we can define unbound input and output file stream first, and then use open function to boud the file we‘ll access:

ifstream infile;		// unbound input file stream
ofstream outfile;		// unbound output file stream

infile.open("in");		// open file named "in" in the current directory
outfile.open("out");	// open file named "out" in the current directory

After opening the file, we need to check if it is successful being opened:

// check that the open succeeded
if(!infile){
	cerr << "error: unable to open input file: "
		 << infile << endl;

	return -1;
}

Rebound File Stream with New File

If we want to bound the fstream with another file, we need to close the current file first, and then bound with another file:

ifstream infile("in");		// open file named "in" for reading
infile.close();				// closes "in"
infile.open("next");		// open file named "next" for reading

Clear File Stream Status

Opening all file names in a string vector, one direct version is:

vector<string> files;
...
vector<string>::const_iterator it = files.begin();
string s;	// string buffer 

// for each file in the vector
while(it != files.end()){
	ifstream input(it->c_str());	// open the file

	// if the file is ok, read and "process" the input
	if(!input)
		break;						// error: bail out!
	while(input >> s)				// do the work on this file
		process(s);
	++it;							// increament iterator to get next file
}

More efficient way but with much more accurate operation version:

ifstream input;
vector<string>::const_iterator it = files.begin();

// for each file in the vector
while(it != files.end()){
	input.open(it->c_str());		// open the file

	// if the file is ok, read and "process" the input
	if(!input)
		break;						// error: bail out!
	while(input >> s)				// do the work on this file
		process(s);

	input.close();					// close file when we‘re done with it
	input.clear();					// reset state to ok
	++it;
}
时间: 2024-09-28 16:11:34

File Input and Output的相关文章

OpenCV Tutorials &mdash;&mdash; File Input and Output using XML and YAML files

They are two kinds of data structures you may serialize: mappings(like the STL map) and element sequence (like the STL vector>. The difference between these is that in a map every element has a unique name through what you may access it. For sequence

OpenCV_Tutorials——CORE MODULE.THE CORE FUNCTIONALITY—— File Input and Output using XML and YAML files

2.9XML和YAML格式作为文件输入输出 目标 你会从文中找到下面问题的答案: 1.如何从OpenCV使用的YAML或者XML文件中读取和打印文字条目.? 2.对于OpenCV数据结构如何做到相同的事情? 3.对你的数据结构如何做到? 4.OpenCV的数据结构,例如FileStorage,FileNode或者FileNodeIterator的使用方法. 源代码 你可以从这里下载代码或者从OpenCV的源代码库的samples/cpp/tutorial_code/core/file_input

标准库 - 输入输出处理(input and output facilities) lua

标准库 - 输入输出处理(input and output facilities)责任编辑:cynthia作者:来自ITPUB论坛 2008-02-18 文本Tag: Lua [IT168 技术文档]I/O库提供两种不同的方式进行文件处理1.io表调用方式:使用io表,io.open将返回指定文件的描述,并且所有的操作将围绕这个文件描述 io表同样提供三种预定义的文件描述io.stdin,io.stdout,io.stderr2.文件句柄直接调用方式,即使用file:XXX()函数方式进行操作,

Python Tutorial 学习(七)--Input and Output

7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转换为字符型值的方法:repr()和str(),二者的区别在于,一个是给机器读的,一个是给人读的,str()返回的是更适合人阅读的样式 一些栗子: >>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>>

input和output常用模块的讲解和使用(logstash)

1 ELK 是一个实时分布式的日志分析平台ELK 是一整套的解决方案(E)lasticsearch -- 数据库(L)ogstash -- 收集日志.标准化的程序(K)ibana -- 图形的展示工具 2 数据批量导入-X 导入使用的方法 POST--data-binary 导入数据的格式@urfile 导入数据的文件名_bulk 导入关键字curl -X "POST" "http://192.168.1.13:9200/_bulk" --data-binary @

C lang:character input and output

Xx_Introduction Character input and output is by more line character conpose of the text flow  Define name common use capital  letter,easy read. The Standard C Library ----->provide I/O model ------>use character flow way. Ax_Application file copy,c

[20160704]Addition program that use JOptionPane for input and output

1 //Addition program that use JOptionPane for input and output. 2 3 import javax.swing.JOptionPane; 4 5 public class Addition{ 6 public static void main(String[] args) { 7 String firstNumber=JOptionPane.showInputDialog("Enter first integer!"); 8

转载:Pixhawk源码笔记四:学习RC Input and Output

转自:新浪@WalkAnt 第五部分 学习RC Input and Output 参考:http://dev.ardupilot.com/wiki/learning-ardupilot-rc-input-output/ RC Input,也就是遥控输入,用于控制飞行方向.改变飞行模式.控制摄像头等外围装置.ArduPilot支持集中不同RC input(取决于具体的硬件飞控板): 1. PPMSum – on PX4, Pixhawk, Linux and APM2 2. SBUS – on P

bootstrap file input 官方文档翻译

file input 特性 1.这个插件会把简单的html文件变成一个更好用的文件选择输入控件,通过一个html的文件输入框,能兼容那些不支持jquery或js的浏览器. 2.fileinput 插件包括options,templates等三个部分来控制展示 .文件标题部分:用来展示选中的文件的简介信息 .文件按钮行为部分:用来浏览,移除和上传文件 .文件预览部分:用来将展示选中的文件到客户端实现预览(支持图片, 文档, flash, 和视频类型),别的文件类型将以宿略图的形式预览 3.如果你设