流:若干字节数据从一端到另一端叫做流,数据的流动
流类体系
#include <iostream> #include <iomanip> #include <cstdio> #include <string> using namespace std; void testOstream() //output { //freopen() 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.precision(4); 所有的流控制符都会对应一个成员函数的方式 //进制输出 cout << hex << 32 << endl; //十六进制 cout << oct << 15 << endl; //八进制输出 cout << setbase(8) << 16 << endl; //8-16 //默认右对齐 //cout.with(8) //cout << resetiosflags << endl; cout << setiosflags(ios::left); //ios::right cout << setw(8) << "123" << setw(8) << "12345" << setw(8) << "3444" << endl; cout << setw(8) << "123" << setw(8) << "12345" << setw(8) << "3444" << endl; } int main() { //testOstream(); testiomanip(); /*freopen("1.txt","r",stdin); int a, b; scanf("%d%d", &a, &b); cout << a + b << endl;*/ while (1); return 0; }
运行结果:
#include <iostream> #include <sstream> using namespace std; //23,132,3443,54,54,65 void teststringstream() { //构建字符流对象,以及获取字符流对象中的数据 stringstream sso(string("onetwothree")); cout << sso.str() << endl; stringstream ssnull; ssnull << "aaaa" << endl; cout << ssnull.str() << endl; //错误 //stringstream ss=string("bbbbb"); string data; ssnull >> data; cout << data << endl; ssnull.str("bbbb"); //ssnull.clear(); 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; //多次对同一个流做数据转换操作,一定做clear操作 transs.clear(); //做重置操作 transs << num; transs >> input; cout << input << endl; } int main() { teststringstream(); while (1); return 0; }
运行结果:
#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); /*成员函数 eof() 在文件末尾 1.流的方式读写 1.1 空格和换行会被忽略*/ /*while (1) { char key; read >> key; if (read.eof()) { break; } write << key; } while (1) { char key; read.get(key); write.put(key); if (read.eof()) break; write.put(key); }*/ 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”)