C++IO流
一、IO流
流:若干字节数据从一端到另一端叫做流,数据的流动 流类体系
二、输入输出流
- ostream类
- cout
- cerr
- clog
- cin
- 字符类的处理
- 格式控制符
- 包含头文件iomanip
- 常用的格式控制,一种是调用成员函数方式,一种流控制字符去做
- 设置有效位数:setprecision(n)
- 设置精度:fixed结合setprecision使用
- istream 类 cin
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
using namespace std;
void testOstream()
{
cout << "标准输出" << endl;
cerr << "标准错误输出" << endl;
clog << "标准错误输出" << endl;
cout.put('a');
cout << 'a' << endl;
char c = 'C';
cout.put(c);
cout << c << endl;
cout.write("onetwothree", 5);
cout << endl;
cout.put(cin.get());
cout << endl;
while (cin.get() != '\n');
while (getchar() != '\n');
char str[20]="";
cin.getline(str, 20);
cout.write(str,20);
}
void testiomanip()
{
double pi = 34.12343;
cout << "设置有效位数:" << setprecision(4) << pi <<endl;
cout << "有效小数位"<< fixed << setprecision(4) << pi << endl;
cout << hex << 32 << endl;
cout << oct << 15 << endl;
cout << setbase(8) << 16 << endl;
cout << setiosflags(ios::left);
cout << setw(8) << "123" << setw(8) << "12345" << setw(8) << "3444" << endl;
cout << setw(8) << "123" << setw(8) << "12345" << setw(8) << "3444" << endl;
}
int main()
{
testiomanip();
while (1);
return 0;
}
运行结果:
二、字符流
- 用的头文件是::sstream类
- istringstream类
- ostringstream类
- 一般用的是stringstream类对象即可
- 获取字符流对象中的数据
- string str() //获取string
- void str(const string& str); 重新设置字符流对象的数据
- 一般字符流对象做字符串处理
#include <iostream>
#include <sstream>
using namespace std;
void teststringstream()
{
stringstream sso(string("onetwothree"));
cout << sso.str() << endl;
stringstream ssnull;
ssnull << "aaaa" << endl;
cout << ssnull.str() << endl;
string data;
ssnull >> data;
cout << data << endl;
ssnull.str("bbbb");
cout<<ssnull.str() << endl;
int num = 1234;
char input[20] = "";
stringstream transs(input);
transs << num;
transs >> input;
cout << input << endl;
stringstream snum("12345");
int temp = 0;
snum >> temp;
cout << temp << endl;
stringstream sData("23,132,3443,54,54,65");
int numdata[6];
char cData[5];
for (int i = 0; i < 6; i++)
{
if (i == 5)
{
sData >> numdata[i];
}
else
sData >> numdata[i] >> cData[i];
}
for (int i = 0; i < 6; i++)
{
cout << numdata[i] << " ";
}
cout << endl;
transs.clear();
transs << num;
transs >> input;
cout << input << endl;
}
int main()
{
teststringstream();
while (1);
return 0;
}
运行结果:
三、文件流
- 流泪体系
- ofstream类 写操作 output输出到文件
- ifstream类 读操作
- fstream类 可读可写 用的时候包含头文件 #include
- 打开关闭文件
- 打开文件:void open(sonst char *URL,ios::openmode mode);
- 读写的方式是
- ios::in 读的方式打开文件
- ios::out 写方式打开文件 具有创建功能
- ios::app 追加模式 具有在创建功能
- ios::ate 追加模式,文件指针指向末尾
- ios::trunc 具备创建功能
- ios::nocreate 不具备创建
- ios::noreplace 不替换(类似C语言文件操作w方式)
- ios::binary 二进制的形式
- 读写的组合方式用的是
- 可读可写: ios::in|ios::out|ios::trunc
- 二进制的可读可写可创建: ios::in|ios::out|ios::trunc|ios::binary
- 判断打开文件是否成功
- 读写文件
- 文件指针定位
- istream类的对象
- istream& seekg(long int pos);
- istream& seekg(long int pos,ios_base::seekdir position);
- ofstream类的对象
- ostream& seekp(long int pos);
- ostream& seekp(long int pos,ios_base::seekdir position);
- iosbase::seekdir
- ios::beg 文件开始位置 //SEEK_SET
- ios::end 文件结束位置
- ios::cur 文件当前位置
#include<iostream>
#include<fstream>
#include <cstring>
using namespace std;
void testOpenFile()
{
fstream file;
file.open("2.txt", ios::out);
if (!file)
{
cout << "文件打开失败" << endl;
return;
}
file.close();
}
void asciRWFile(const char* readFileName, const char* writeFileName)
{
fstream read(readFileName, ios::in);
fstream write(writeFileName, ios::out);
char str[1024] = "";
while (!read.eof())
{
read.getline(str, 1024);
write.fstream::write(str, strlen(str));
write.put('\n');
}
read.close();
write.close();
}
void binaryRWFile(const char* readFileName, const char* writeFileName)
{
fstream readFile(readFileName, ios::in|ios::binary);
fstream writeFile(writeFileName, ios::out | ios::binary);
while (!readFile.eof())
{
char str[1024] = "";
readFile.read(str, 1024);
writeFile.write(str, 1024);
}
readFile.close();
writeFile.close();
}
void testSeekRead(const char* fileName)
{
fstream fread(fileName, ios::in);
if (!fread)
{
cout << "打开文件失败" << endl;
}
char key = fread.get();
cout << key;
fread.seekg(4,ios::beg);
key = fread.get();
fread.seekg(-4,ios::end);
key = fread.get();
cout << key<< endl;
fread.close();
}
int main()
{
testOpenFile();
asciRWFile("read.txt","write.txt");
binaryRWFile("br.txt","bw.txt");
testSeekRead("test.txt");
while (1);
return 0;
}
运行结果: asciRWFile(“read.txt”,“write.txt”) binaryRWFile(“br.txt”,“bw.txt”)
testSeekRead(“test.txt”)
|