标准I/O库
输入输出对象 istream cin
ostream cout
格式化输入输出
- cin >> //输入
- cout << //输出
- hex、oct、dec和setbase
- 浮点精度precision / setprecision
- 域宽width / setw
- 用户自定义流操纵算子-’\n’ ‘\t’ ‘\r’
格式化输出:
非格式化输入/输出
//输出
ostream& put ( char c ); //写一个字符出去
ostream& write ( const char* s , streamsize n );//写指定长度的字符串出去
//输入
int 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 );
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
istream& read ( char* s, streamsize n );
非格式化输出:
非格式化输入:read() / getline() 同get()
#include <iostream>
using namespace std;
int main()
{
// cout.put('h').put('i') << endl;
// cout.write("hello",2).write("world",20) << endl;
// cout << cin.get() << endl;
// char str;
// cin.get(str);
// cout << str << endl;
// char str1[10];
// cin.get(str1,6);
// cout << str1 << endl;
char str1[10];
cin.get(str1,6,' ');
cout << str1 << endl;
return 0;
}
文件操作
文件的打开方式:(ios = ios_base)(默认in|out)
- ios_base::app:文件末尾处添加
- ios_base::ate:文件末尾处添加
- ios_base::binary:文件以二进制的形式打开
- ios_base::in:文件以写入的方式打开
- ios_base::out:文件以读出的方式打开
- ios_base::trunc:文件以擦除重写的方式打开
文件位置指针
用于指示读写操作所在的下一个字节号;是个整数值,指定文件中离文件开头的相对位置(也称为离文件开头的偏移量) 。
//获取文件指针
fstream file;
...
file.tellp();
file.tellg();
//文件指针的重新定位
file.seekp(10,ios::beg);
file.seeekg(10,ios::end);
- ios::beg(默认)相对于流的开头定位
- ios::cur相对于流当前位置定位
- ios::end相对于流结尾定位
streampos ostream::tellp ( ); //返回值可赋给long类型的变量
ostream& ostream::seekp ( streampos pos );
ostream& ostream::seekp ( streamoff off, ios_base::seekdir dir );
streampos istream::tellg ( );
istream& istream::seekg ( streampos pos );
istream& istream::seekg ( streamoff off, ios_base::seekdir dir );
//一个文件有一个指针,输入/输出复用
ios::operator! 与 ios::operator void*
#include <iostream>
#include <iomanip>
using namespace std;
#if 0//进制的格式化输出
int main()
{
int number;
cout << "Enter a dec number: ";
cin >> number;
cout << hex << number << endl;
cout << oct << number << endl;
cout << dec << number << endl;
cout << setbase(8) << number << endl;
//hex oct dec setbase(8/10/16)修改了数据的格式
//在修改格式之前,原来的格式将一直有效
cout << number << endl;
return 0;
}
#endif // 0
#if 0//浮点的精度格式化输出
int main()
{
double root2 = 122.61241232;
// cout << fixed;
for (int places = 0; places <= 9;places++ )
{
cout.precision( places );
cout << root2 << endl;
// cout << setprecision(places) << root2 << endl;
}
return 0;
}
#endif // 0
#if 0//设置域宽的格式化输出
int main()
{
int widthValue = 4;
char str[10];
cout << "Enter a str:" << endl;
cin.width( 5 );
while (cin >> str)
{
cout.width( widthValue++ );
cout << right << str << endl;
//cout<<setw(widthValue++) << str << endl;
cin.width( 5 );
}
return 0;
}
#endif // 1
#if 0//自定义流的格式化输出
ostream &endLine(ostream &out)
{
out << '\n';
return out;
}
int main()
{
cout << "HelloWorld" << endLine << "!!!" << endLine;
return 0;
}
#endif // 0
#if 0//非格式化输出
int main()
{
// cout.put('h').put('i') << endl;
// cout.write("hello",2).write("world",20) << endl;
// cout << cin.get() << endl;
// char str;
// cin.get(str);
// cout << str << endl;
// char str1[10];
// cin.get(str1,6);
// cout << str1 << endl;
char str1[10];
cin.get(str1,6,' ');
cout << str1 << endl;
return 0;
}
#endif // 0
#if 0//文件操作
#include <fstream>
int main()
{
fstream file("hello.txt",ios::app|ios::in|ios::out);
if (!file)
{
cout << "File could not be opened" << endl;
return 0;
}
file << "hello_world" << endl;
file.close();
char str[50];
file.open("hello.txt",ios::app|ios::in|ios::out);
if (!file.is_open())
{
cout << "File could not be opened" << endl;
return 0;
}
file >> str;
cout << str << endl;
file.close();
return 0;
}
#endif // 0