c++_note_10_io

11. io

c++的io包括三部分:键盘、文件、内存,分别简称标准i/o、文件i/o、串i/o.

ios
istream
ostream
ifstream
istringstream
iostream
ofstream
ostringstream
fstream

11.1 标准io

11.1.1 标准输入流

#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	int a;
	float b;
	char s[10];
	cin>>a;
	cin>>b;
	cin>>s; //不能接收空格
	
	cout<<a<<endl;
	cout<<b<<endl;
	cout<<s<<endl;
	cin.get();
	return 0;
}

/*
1
1.2
a b
1
1.2
a
*/

程序和键盘之间存在输入缓冲区,和屏幕之间存在输出缓冲区。

输入时,若缓冲区没有数据则会阻塞。

#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char c;
	while( (c = cin.get()) != EOF)
	{
		cout<<c<<endl;
	}
	cin.get();
	return 0;
}

/*
aa	//此时已经输入input buf
a
a


^Z
请按任意键继续. . .
*/

cin.get():

  • istream& get (char& c);
  • istream& get (char* s, streamsize n);
  • istream& get (char* s, streamsize n, char delim);
  • istream& get (streambuf& sb);
  • istream& get (streambuf& sb, char delim);
#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char a,b,c;
	cin.get(a);
	cin.get(b);
	cin.get(c);
	
	cout<<a<<b<<c<<"there is data in buf.\n";
	
	cin.get(a).get(b).get(c);
	cout<<a<<b<<c<<endl;
	
	cin.get();
	return 0;
}
/*
abcdefg
abcthere is data in buf.
def
*/

输入含空格的字符串用getline()

  • istream& getline (char* s, streamsize n );
  • istream& getline (char* s, streamsize n, char delim );
#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char s[10];
	cin.getline(s,10);
	cout<<s<<endl;
	
	cin.get();
	return 0;
}

/*
aaaaa sssss dd
aaaaa sss
*/

下面是3个不常用的函数:

  • istream& ignore (streamsize n = 1, int delim = EOF);,ignore until delim
  • int peek(); 检测缓冲区是否有数据
  • istream& putback (char c); 把c退回缓冲区
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
	char buf1[10],buf2[10];
	int ret;
	cin>>buf1;
	cin.ignore(1);
	ret = cin.peek();
	cout<<"ret:"<<ret<<endl;
	
	cin>>buf2;
	cout<<"buf1:"<<buf1<<endl;
	cout<<"buf2:"<<buf2<<endl;
	
	cin.get();
	return 0;
}

/*
aa  bb
ret:32
buf1:aa
buf2:bb
*/

下面是官网给出的putback()示例。

// istream::putback example
#include <iostream>     // std::cin, std::cout
#include <string>       // std::string

int main () {
  std::cout << "Please, enter a number or a word: ";
  char c = std::cin.get();

  if ( (c >= '0') && (c <= '9') )
  {
    int n;
    std::cin.putback (c);
    std::cin >> n;
    std::cout << "You entered a number: " << n << '\n';
  }
  else
  {
    std::string str;
    std::cin.putback (c);
    getline (std::cin,str);
    std::cout << "You entered a word: " << str << '\n';
  }
  return 0;
}

11.1.2 标准输出

标准输出流的定义:extern ostream cout;

常用函数:

  • ostream& flush(); 让缓冲区的信息立即强制输出。
  • ostream& put (char c);cout.put('a').put('b');
  • ostream& write (const char* s, streamsize n);,输出二进制流。
  • streamsize width();
  • char fill();
  • fmtflags setf();
char *p = "hello";
cout.write(p,5)<<endl;
#include<iostream>
using namespace std;
#include <iomanip>
int main(int argc, char *argv[])
{
   cout<<"<start>"
   	<<setw(30)
   	<<setfill('*')
   	<<setiosflags(ios::showbase)
   	<<hex
   	<<100
   	<<"<end>"
   	<<endl;
   cout<<endl;
   cout.width(5);
   cout.fill('*');
   cout.setf(ios::showbase);
   cout.setf(ios::internal);
   cout<<hex<<100<<endl;
   
   cin.get();
   return 0;
}

/*
<start>**************************0x64<end>

0x*64
*/

11.2 文件io

打开文件:void open (const char* filename, ios_base::openmode mode = ios_base::out);

调用bool is_open() const;判断是否打开成功。

读写函数:

  • ostream& write (const char* s, streamsize n);
  • istream& read (char* s, streamsize n);

输入

std::ifstream ifs;
ifs.open ("test.txt", std::ifstream::in);

char c = ifs.get();
while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
}

ifs.close();
member constantstands foraccess
in *inputFile open for reading: the internal stream buffer supports input operations.
outoutputFile open for writing: the internal stream buffer supports output operations.
binarybinaryOperations are performed in binary mode rather than text.
ateat endThe output position starts at the end of the file.
appappendAll output operations happen at the end of the file, appending to its existing contents.
trunctruncateAny contents that existed in the file before it is open are discarded.

输出

std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
//or
//std::ofstream fout("test.txt");

注意,out方式,若文件存在,则清除内容。

member constantstands foraccess
ininputFile open for reading: the internal stream buffer supports input operations.
out *outputFile open for writing: the internal stream buffer supports output operations.
binarybinaryOperations are performed in binary mode rather than text.
ateat endThe output position starts at the end of the file.
文件不存在则报错
appappendAll output operations happen at the end of the file, appending to its existing contents.
trunctruncateAny contents that existed in the file before it is open are discarded.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值