c++ primer plus 第十七章 输入 输出 文件 IO iostream fstream
来源:互联网 发布:淘宝冰点营销怎么使用 编辑:程序博客网 时间:2024/06/05 15:58
#include <iostream>#include <fstream>#include <iomanip>using namespace std;struct times{int YEAR;int MON;int DAY;}timeNow = {2016,3,5};int main(){//1,ostream的其他方法//put//原型 ostream & put(char)cout.put('a');char str[10] = "bcdefg";cout.put(str[0]).put(' ');cout.put(*str+1).put('\n');cout << "=====================\n";//write//原型 basc_ostream<charT,traits>& write(const char_type* s, streamsize n);cout.write(str,3).put('\n');cout.write(str,10); //注意并不会停止 越界cout << endl;//cout 格式输出//进制//oct 8进制oct(cout);cout << 16 << endl;//dec 10进制dec(cout);cout << 16 << endl;//hex 16进制hex(cout);cout << 16 << endl;cout << "===================\n";cout << oct << 16 << " "<< dec << 16 << " " << hex << 16 << " " << endl;dec(cout); //恢复十进制//宽度cout << "===================\n";int orl = cout.width(12); //调节输出12的宽度 右对齐 返回以前设置的宽度int orl1 = cout.width(); //获得当前字符宽度cout.fill('*'); //填充字符 (与宽度不同 这里设置一次知道重设以后都不变)cout << "hello" << endl;cout << "orl: " << orl << "orl1: "<< orl1 << endl;//设置小数点精度(c++ 默认6位)cout.precision(5);cout << 2.123456789 << endl;cout << 2.000000000 << endl; //0自动末尾舍弃cout.setf(ios_base::showpoint); //显示末尾0;cout << 2.0000000000 << endl;//setf//ios_base::boolalpha 输出输入布尔为 true 或者falsecout.setf(ios_base::boolalpha);cout << true << endl;//ios_base::showbase 输出使用c++基数前缀(0,0x)//ios_base::showpoint 显示末尾的小数//ios_base::uppercase 十六进制使用大写 E表示法//ios_base::showpos 正数前面加上+//setf(long,long)//(1//第一个参数//ios_base::dec 使用基数10(十进制)//ios_base::oct 使用基数8(8进制)//ios_base::hex 使用基数16(16进制)//第二个参数//ios_base::basefield//(2//第一个参数//ios_base::fixed 使用定点计数法//ios_base::scientific 使用科学计数法//第二个参数//ios_base::floatfield//(3//第一个参数//ios_base::left 使用左对齐//ios_base::right 使用右对齐//ios_base::internal 符号或者基数前缀左对齐 值右对齐//第二个参数//ios_base::adjustfield;//iomanip 头文件//setprecision 设置精度 (注意要四舍五入)cout << setprecision(5) << 2.123456789 << endl;cout << setprecision(0); //恢复精度(默认6位)cout << 2.123456789 << endl;//setw 输出宽度 cout << setw(10) << 10 << endl;//setfill() 填充字符cout << setw(10) << setfill(' ') << setfill('@') << 2 << endl; //清除前面的cout.fill 然后用setfill()设置填充符号//文件// ofstream 输出文件流ofstream outFile;outFile.open("file.txt");//ofstream outFile("files.txt"); 同上两步//ofstream继承ostream 所以可以使用它的所有方法 如下outFile << "hello";outFile.put('1');char strToFile[100] = "2222222";outFile.write(strToFile,3);outFile.close(); //关闭文件流//ifstream 输入文件流ifstream inFile("file.txt");if(!inFile.is_open()){cout << "抱歉文件打开失败!";return 0;}//ifstream 继承与istream 所以可以使用它的所有方法char strFile[100];inFile >> strFile;cout << strFile << endl;inFile.close();inFile.open("file.txt");if(!inFile.is_open()){cout << "抱歉文件打开失败!";return 0;}char ch;inFile.get(ch);cout << ch << endl;char strFile1[100];inFile.getline(strFile1,4);cout << strFile1 << endl;inFile.close();//检查文件流状态//is_open()//fail()//good()//eof//fstream 可以读写的文件//文件模式//ios_base::in //打开文件以便读取//ios_base::out //打开文件以便写入//ios_base::ate //打开文件并移到文件尾//ios_base::app //追加到文件尾//ios_base::trunc //如果文件存在则截断文件//ios_base::binary //二进制文件outFile.open("file.txt",ios_base::out|ios_base::app); //追加在文件末尾outFile << "!!!!"; //追加在文件末尾outFile.close();//二进制文件//writefstream biFile("binaryfile.txt",ios_base::binary|ios_base::out);biFile.write((char*)&timeNow,sizeof timeNow);biFile.close();//readtimes timeNowOne;biFile.open("binaryfile.txt",ios_base::binary|ios_base::in);biFile.read((char*)&timeNowOne,sizeof timeNowOne);cout << timeNowOne.YEAR << " " << timeNowOne.MON << " " << timeNowOne.DAY << endl;biFile.close();//随机存取//seekg() 将输入指针移到指定的位置//原型 basic_istream<charT, traists>& seekg(off_type, ios_base::seekdir);//seekdir://ios_base::beg //开始//ios_base::cur //当前//ios_base::end //结尾//原型 basic_istream<charT, traits>& seekg(pos_type) //pos_type // 相对于开始以一个为0 开始算(如 .seekg(12) 指向文件中的第13个字节)//seekp() 将输出指针移到指定的位置//tellg() //汇报输入指针的当前位置//tellp() //汇报输出指针的当前位置system("pause");return 0;}
1 0
- c++ primer plus 第十七章 输入 输出 文件 IO iostream fstream
- c++primer plus第十七章-文件输入和输出
- c++primer plus第十七章-输入和输出概述
- c++primer plus第十七章-cin输入、cout输出和文件I/O
- 【C Primer Plus】第十三章 文件的输入/输出
- c primer plus第13章总结:文件输入/输出
- 《C++ primer plus》第十七章 输入、输出和文件(二):文件的输入和输出 学习笔记
- 《C++ primer plus》第十七章 输入、输出和文件(一):输入和输出 学习笔记
- C++primer plus第十七章编程练习
- 2012/2/11 《C++ Primer Plus》第十七章:输入、输出和文件 学习笔记
- c primer plus 程序笔记 (二) 第13章 文件输入/输出
- C primer plus第13章(文件输入/输出)对文本文件及二进制文件的处理
- C primer plus第13章(文件输入/输出)习题
- 《C++ Primer Plus(第六版)》(38)(第十七章 输入、输出和文件 笔记1)
- 《C++ Primer Plus(第六版)》(39)(第十七章 输入、输出和文件 笔记2)
- 《C++ Primer Plus(第六版)》(40)(第十七章 输入、输出和文件 复习题和答案)
- 《C++ Primer Plus(第六版)》(41)(第十七章 输入、输出和文件 编程练习和答案1)
- 《C++ Primer Plus(第六版)》(42)(第十七章 输入、输出和文件 编程练习和答案2)
- Spring集成AspectJ的注解模式
- Java适配器模式、代理模式及动态代理
- Webpack笔记
- 中继器
- 理解OAuth 2.0
- c++ primer plus 第十七章 输入 输出 文件 IO iostream fstream
- Remove Duplicates from Sorted List II
- XmlSerializer 短信备份
- java初步学习之堆栈
- Maven内置隐式变量(转)
- JavaScript常见的数据类型
- Android 仿instagram登陆页面动画
- 善用setup函数
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片的优缺点比较