[C++]文件的写入格式与输入输出流
2015-11-29 16:34
162 查看
在编写数值分析作业QR分解过程中,用到将数据结果写入文件的操作,在c++中用流来实现:
在c++中头文件fstream定义了三个类:
ifstream:从文件读取
ofstream:写入文件
fstream:读写文件
初始化方式:fstream fstrm(string file_name)或fstrm.open(string file_name);
这三种类型与其他IO类型不同的操作(以fstream为例):fstream.open(file_name);fstream.close();fstream.is_open();
在primer c++ 中,第八章讲解IO库类型,IO库类型包括:
iostream:cin即为istream类的一个对象,cout为ostream类的一个对象
fstream:如上
sstream:istringstream,ostringstream读写内存string对象
#include<iostream> #include<fstream>//用到ofstream类时要用 //主函数中 ofstream file_Qr; file_Qr.open("output_Qr.txt", ios::out); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (abs(Qr[i][j]) < E)Qr[i][j] = 0; file_Qr << setiosflags(ios::scientific) << setprecision(12) << setw(20) << Qr[i][j];// << endl; //cout << A[i][j] << " " << endl; // printf("%f ", A[i][j]); } //cout << '\n' << endl; file_Qr<< "\n" << endl; //printf("\n"); }
在c++中头文件fstream定义了三个类:
ifstream:从文件读取
ofstream:写入文件
fstream:读写文件
初始化方式:fstream fstrm(string file_name)或fstrm.open(string file_name);
这三种类型与其他IO类型不同的操作(以fstream为例):fstream.open(file_name);fstream.close();fstream.is_open();
在primer c++ 中,第八章讲解IO库类型,IO库类型包括:
iostream:cin即为istream类的一个对象,cout为ostream类的一个对象
fstream:如上
sstream:istringstream,ostringstream读写内存string对象
相关文章推荐
- c++ to_String()
- ioS C语言 循环结构
- 15泛型_15.1与C++比较
- c++运算符重载
- 【C语言】 使用回调函数实现冒泡排序
- c语言基础篇(一)
- c++11中的委派构造函数
- C语言解释器Java版-1-内存分配
- c++11中的右值引用以及移动构造
- 【C语言】递归实现栈的逆序及排序
- for循环 数组下标越界导致死循环
- c++11中的using关键字
- 编译原理(七) 算符优先分析法(构造算符优先关系表算法及C++实现)
- c++11中decltype的妙用
- bitset应用---产生1万个不重复的随机数
- c++11中的智能指针
- c++模板实现多参数任意传 - 类实现
- effective c++ 笔记之static_cast
- vs2008 dlib编译问题USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE
- 【C++】满二叉树与完全二叉树的区别及判断