c12-文件流基础

 

1.类中有强制类型转换重载时:cout<<A<<endl; 会找A的强制类型转换后输出
2.cerr,clog没有缓冲,不能重定向

3.键盘输入:xyz,程序cin>>ch; cout<<ch<<endl; cin>>ch1; cout<<ch1<<endl; 会直接输出xy的值,因为输入缓冲区中有数据

4.cout<<"hello"; a.out > a.txt, cout可以重定向

5.ofstream.fout("a.txt"); fout<<"hello"<<endl; fout.close();

6.ifstream.fin("a.txt"); fin>>变量名; fin.close();

7.getline(fin, str);可以从文件输出

8.istream, ostream重载了类型转换运算符void* ,当I/O出错时返回值为NULL可以看为假,如创建文件失败

9.istream fin(c_str);

10.计算文件中行数:

 

	int n = 0;
	ifstream fin("G://a.txt");
	string str;
	for (;;)
	{
		getline(fin, str);
		if(!fin) break;
		++n;
	}
	cout<<n<<endl;

 

 

	int n = 0;
	ifstream fin("G://a.txt");
	string str;
	for(; getline(fin, str); ++n);
	cout<<n<<endl;


11.istream操作:int get(); 从输入流中读取一个字符,返回字符的ASCII码,cout<<cin.get()<<endl; 键入a; 显示97;istream get(char&);从输入流中读取一个字符,保存到形参中,返回istream对象;能读取换行、空格、制表符等

12.cin.getline(cstr, 20); 输入多于20,cin为NULL,错误。输入流一旦处于错误状态,就不在执行任何输入。cin.clear();可以恢复正常状态,从第21个开始读取,可以使用while(cin.get()!='\n');或cin.ignore(len, '\n');清空缓冲区。ignore函数原型

 

	void istream::ignore(int len = 1, char c = -1)
	{
		for(int i = 0; i < len && get() != c; ++i);
	}


13.istream& get(fin, str, '\n'); size_t cin.getline(buf, len, '\n');其中‘\n'读取但不保存

 

14.peek, 偷看输入流的下一个字符,返回它的ASCII码(整数),如果用get将取走,peek不会读走。

15.putback把刚读取的数据返回输入流中,与getchar(char&)配合使用。cin.putback(ch);

16.cin.width(10);只读取10个字符;cin>>ws;跳过输入缓冲区的空白字符

 

	string str1;
	//cin>>ws;
	getline(cin,str1);
	cout<<str1;


17.put(char),输出一个字符,cout.put(cin.get());

 

18.fout.write(地址(char*),字节数sizeof()),把内存空间的内容读到文件中去。fin.read((char*), sizeof())把文件中的内容读到一块内存空间中。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值