-
流的概念
-
流: 若干字节数据从一端到另一端我们叫做流
-
流类体系
-
流对象
-
流运算符 >> <<
-
-
-
输入输出流
-
ostream类
-
cout
-
cerr
-
clog
-
字符类的处理
-
正常的操作
-
调用成员函数的方式
-
-
格式控制字符
-
包含头文件 iomanip
-
常用的格式控制,一种是调用成员函数方式,一种流控制字符去做
-
设置有效位数: setprecision(n)
-
设置精度: fixed结合setprecision使用
-
-
-
-
istream 类 cin
-
#include<iostream> #include <iomanip> using namespace std; void testistream() { char str[20] = ""; //cin >> str; //cout << str << endl; //cin.getline(str, 10,'1');//getline(str,n,ch),从输入流中接收n-1个字符给str变量,当遇到指定ch字符时会停止读取,默认情况下 ch 为 '\0'。 cin.getline(str, 20); cout << str << endl; char c = cin.get(); cout << c << endl; //清空缓冲区 while (cin.get() != '\n'); //while (getchar() != '\n'); } void testostream() { //cout << "标准输出" << endl; //cerr << "标准错误输出" << endl; //clog << "标准错误输出" << endl; // //1.输入 //cout.put(cin.get()); //2.字符 //cout.put('b'); //char c = 'b'; //cout.put(c); //3.字符串 cout.write("CCTV", 4); } void testiomanip() { //格式控制 //设置格式 double pi = 3.141592653; cout << "设置有效位数是:" << setprecision(6) << pi << endl; cout << "设置有效位数是:" << setprecision(7) << pi << endl; cout << "设置有效小数位数:" << fixed << setprecision(7) << pi << endl; //进制输出 cout << hex << 32 << endl; //十六进制 cout << oct << 15 << endl; //8进制输出 cout << setbase(16) << 15 << endl; //8/16 //制表 默认右对齐 cout << setw(8) << "1001" << setw(8) << "1002" << setw(8) << "1003" << endl; cout << setiosflags(ios::left); //ios::right 设置对齐 cout << setw(8) << "1001" << setw(8) << "1002" << setw(8) << "1003" << endl; } int main() { //testistream(); //testostream(); testiomanip(); return 0; }
-
-
字符流
-
用的头文件是:sstream类
-
istringstream类
-
ostringstream类
-
一般用时stringstream类对象即可
-
-
获取字符流对象中的数据
-
string str() //获取string
-
void str(const string& str); 重新设置字符流对象的数据
-
-
一般字符流对象做字符串处理
-
字符串分割
-
字符串转换问题
-
-
#include<iostream> #include <sstream> #include<string> #include <cstdio> using namespace std; void teststringstream() { //构建字符流对象,以及获取字符流对象中的数据 stringstream sso(string("HelloWodld")); cout << sso.str() << endl; stringstream ssnull; ssnull << "Hello"; cout << ssnull.str() << endl; string str = "World"; ssnull << str; cout << ssnull.str() << endl; ssnull.str("ABC"); cout << ssnull.str() << endl; //字符串与数字之间的转换 //数字转字符串 int num = 123456; char str1[20] = ""; stringstream charput; charput << num; charput >> str1; cout<< str1 << endl; char str2[10] = "12345678"; stringstream intput; int num2; intput << str2; intput >> num2; cout << num2 << endl; //分割字符串"23,132,3443,54,54,65" char str3[30] = "23,132,3443,54,54,65"; stringstream separate; separate << str3; int numData[6]; char ch[5]; for (int i=0;i<6;i++) { if (i==5) { separate >> numData[i]; } else { separate >> numData[i] >> ch[i]; } } for (int i=0;i<5;i++) { cout << numData[i] << " "; } } int main() { teststringstream(); return 0; }
-
-
文件流
-
流类体系
-
ofstream 类 写操作 output输出到文件
-
ifstream 类 读操作
-
fstream类 可读可写 用的时候包含头文件 #include <fstream>
-
-
打开关闭文件
-
打开文件: void open(const 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
-
-
判断打开文件是否成功
-
用文件流对象重载的运算!
-
is_open成员函数判断
-
返回true 打开成功
-
返回false打开失败
-
-
-
-
关闭文件: void close()
-
-
读写文件
-
流的方式读写
-
二进制的方式读写
-
把string写到文件
-
需要先转换为char* 再写进去
-
-
-
文件指针定位
-
ifstream类的对象
-
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);
-
-
ios_base::seekdir
-
ios::beg 文件开始位置 //SEEK_SET
-
ios::end 文件结束位置
-
ios::cur 文件当前位置
-
-
-
#include<iostream> #include <fstream> #include <cstring> using namespace std; //打开文件 void testOpenFile() { //fstream file; //file.open("1.txt",ios::app ); fstream file("1.txt", ios::out); if (!file.is_open()) { cerr << "文件打开失败" << endl; return; } //if (!file) //{ // cerr << "文件打开失败" << endl; // return; //} file.close(); } //从一个文件读取一个文件写到另一个文件 void testRWfile(const char*readFileName,const char*writeFileName) { fstream readFile(readFileName,ios::in); if (!readFile) { cerr << "读取文件打开失败" << endl; } fstream writeFile(writeFileName,ios::out); if (!writeFile) { cerr << "写入文件打开失败" << endl; } //成员函数 eof() 再文件末尾 //1.流的方式读写 //1.1 流运算符读写 空格和换行会被忽略 //while (1) //{ // char ch; // readFile>> ch; // if (readFile.eof()) break; // writeFile << ch; //} //1.2成员函数 while (1) { char ch; readFile.get(ch); if (readFile.eof()) break; writeFile.put(ch); } //while (!readFile.eof()) //{ // char str[1024] = ""; // readFile.getline(str, 1024); // writeFile.write(str, strlen(str)); // writeFile.put('\n'); //} readFile.close(); writeFile.close(); } //二进制读写 void binaryRWFile(const char*readFileName,const char*writeFileName) { fstream readFile(readFileName, ios::in | ios::binary); if (!readFileName) { cerr << "读取文件打开失败!"; return; } fstream writeFile(writeFileName, ios::out | ios::binary); if (!writeFileName) { cerr << "写入文件打开失败!"; return; } if (!readFile.eof()) { char str[1024]=""; readFile.read(str, 1024); writeFile.write(str, strlen(str)); } readFile.close(); writeFile.close(); } void seekFile(const char*fileName) { fstream seekfile(fileName, ios::in); if (!fileName) { cerr << "文件打开失败!"; return; } char ch = seekfile.get(); cout << ch << endl; seekfile.seekg(5, ios::beg); ch = seekfile.get(); cout << ch << endl; seekfile.seekg(-3, ios::end); ch = seekfile.get(); cout << ch << endl; seekfile.seekg(1, ios::cur); ch = seekfile.get(); cout << ch << endl; seekfile.close(); } int main() { //testOpenFile(); //testRWfile("read.txt", "write.txt"); //binaryRWFile("read.txt", "binaryWrite.txt"); seekFile("test.txt"); return 0; }
-
10.C++IO流
最新推荐文章于 2024-10-16 10:13:45 发布