11. io
c++的io包括三部分:键盘、文件、内存,分别简称标准i/o、文件i/o、串i/o.
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 delimint 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 constant | stands for | access |
---|---|---|
in * | input | File open for reading: the internal stream buffer supports input operations. |
out | output | File open for writing: the internal stream buffer supports output operations. |
binary | binary | Operations are performed in binary mode rather than text. |
ate | at end | The output position starts at the end of the file. |
app | append | All output operations happen at the end of the file, appending to its existing contents. |
trunc | truncate | Any 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 constant | stands for | access |
---|---|---|
in | input | File open for reading: the internal stream buffer supports input operations. |
out * | output | File open for writing: the internal stream buffer supports output operations. |
binary | binary | Operations are performed in binary mode rather than text. |
ate | at end | The output position starts at the end of the file. 文件不存在则报错 |
app | append | All output operations happen at the end of the file, appending to its existing contents. |
trunc | truncate | Any contents that existed in the file before it is open are discarded. |