C++输入输出流

1 输入流信息获取

1.1 文本流直接传递给字符数组(或者String),遇到第一个空格会停止!

#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
	ifstream icin;//输入文件流 //fstream是文件流//ofstream是输出文件流
	icin.open("test.txt");//默认读写模式打开文件test.txt
	char temp[100];
	icin>>temp;
	cout<<temp<<endl;
	system("pause");
	return 0;
}


1.2 字符数组循环接收文件中的有效字符(在接收过程中,会忽略换行符和空格符号)

#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
	ifstream icin;//输入文件流 //fstream是文件流//ofstream是输出文件流
	icin.open("test.txt");//默认读写模式打开文件test.txt
	unsigned char temp[1000];
	int arrNo=0;
	while(!icin.eof()){
		icin>>temp[arrNo];
		cout<<temp[arrNo]<<endl;
		arrNo++;
	}
	cout<<temp<<endl;
	icin.close();
	system("pause");
	return 0;
}


1.3 getline(istream&,string &)函数

函数可以读取整行的数据,遇到换行符号才会停止

#include <fstream>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main(){
	ifstream icin;//输入文件流 //fstream是文件流//ofstream是输出文件流
	icin.open("test.txt");//默认读写模式打开文件test.txt
	string  inputData;
	getline(icin,inputData);
	cout<<inputData;
	icin.close();
	system("pause");
	return 0;
}


1.4 循环逐行读取文件,并用vector将每行存储起来

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
int main(){
	ifstream icin;//输入文件流 //fstream是文件流//ofstream是输出文件流
	icin.open("test.txt");//默认读写模式打开文件test.txt
<span style="white-space:pre">	</span>//三种流状态检查
<span style="white-space:pre">	</span>//if(icin.fail()){cout<<"流打开失败"<<endl;}
<span style="white-space:pre"></span><pre code_snippet_id="481002" snippet_file_name="blog_20141009_4_4947819" name="code" class="cpp"><span style="white-space:pre">	</span>//if(!icin){cout<<"流打开失败"<<endl;}
<span style="white-space:pre"></span><pre code_snippet_id="481002" snippet_file_name="blog_20141009_4_4947819" name="code" class="cpp"><span style="white-space:pre">	</span>if(!fin.is_open()){cout<<"流打开失败"<<endl;}
 string inputDataline;vector<string> inputData;while(!icin.eof()){getline(icin,inputDataline);inputData.push_back(inputDataline);cout<<inputDataline<<endl;}icin.close();system("pause");return 0;} 
 

2 流状态检查

2.1 流状态检查列表

成员描述
eofbit如果到达文件尾,则设置成1
badbit如果流被破坏,则设置成1。如:文件读取错误
failbit如果输入未读取预期的字符或输出未写入预期的字符,则设置为1
goodbit和failbit类似,不过出现上述情况则设置为0
good()如果流可以使用,则返回true
eof()eofbit被设置1,则返回true
bad()badbit被设置1,则返回true
fail()如果badbit或者failbit被设置成1,则返回true
rdstate()返回流状态
exceptions()返回一个位掩码,指出哪些标记导致异常被引发
exceptions(isostate ex)设置哪些状态将导致clear()引发异常;例如,如果ex是eofbit,则如果eofbit被设置为1,clear()将引发异常
clear(iostate s)将流状态设置成s;s的默认值是0(goodbit);如果(restate()&&exception())!=0,则引发异常,basic_ios::failure
setstate(iostate s)调用clear(rdstate()|s)。这里将设置与s中设置的位对应的流状态位,其他流状态位不变。

ps1:理解输入输出
程序设计时,我们将的输入输出时针对程序而言,如程序的输出就是文件的写入,程序的输入就是文件的读取。
ps2:以上虽然有13种流状态检查但是常用的只有如下几种:
eof()
clear()
fail()
在上面的程序中都有涉及。
其余的几种流状态成员的应用有待发掘。。。。

2.2上面程序列出了三种流打开状态检查方式。

<span>	</span>if(icin.fail()){...}
<span></span><pre code_snippet_id="481002" snippet_file_name="blog_20141009_4_4947819" name="code" class="cpp"><span>	</span>if(!icin){...}
<span></span><pre code_snippet_id="481002" snippet_file_name="blog_20141009_4_4947819" name="code" class="cpp"><span>	</span>if(!fin.is_open()){...}

 
 

新式is_open()方法更好,能检查出以不合适的方式打开文件。


3 文件打开模式和随机存储(见相关博文)





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值