c++文件的读写

1.文本方式的写文件

#include <iostream>
#include <fstream>
using namespace std; int main(){
int ar[] = {1123,123,43,45,63,43,2,3};
//方法1,ios::out含义是也写的方式打开流
ofstream ofile1("./test.txt", ios::out);
//方法2
ofstream ofile2;
ofile2.open("./test.txt");
if(!ofile1){//文件打开失败
cerr << "open err" << endl;
exit(1);
}
for(int i = 0; i < sizeof(ar) / sizeof(int); ++i){
ofile1 << ar[i] << " ";
}
ofile1.close();
}

2.文本方式的读文件

#include <iostream>
#include <fstream>
using namespace std; int main(){
int ar[10];
ifstream ifile("./test.txt",ios::in);
if(!ifile){
cerr << "open err" << endl;
exit(1);
}
for(int i = 0; i < 10; ++i){
//用空格分割读进数组
ifile >> ar[i];
}
}

3.二进制方式的写文件

#include <iostream>
#include <fstream>
using namespace std; int main(){
int ar[] = {11,232,123123,1223,455,4,4,5,56,4,33};
ofstream ofile("./text2.txt", ios::out | ios::binary);
if(!ofile){
cerr << "open err" << endl;
}
ofile.write((char*)ar, sizeof(ar));
ofile.close();
}

4.二进制方式的读文件

#include <iostream>
#include <fstream>
using namespace std; int main(){
int ar[10];
ifstream ifile("./text2.txt",ios::in | ios::binary);
if(!ifile){
cerr << "open err" << endl;
}
ifile.read((char*)ar, sizeof(ar));
ifile.close();
}

5.按位置读写文件

  • 文本方式的按位置读

    假设文件的内容:【1 12 222 3232 2232323】,每个数字节数都不一样,不能正确读出想要的。

    解决办法,使用二进制方式的按位置读。

#include <iostream>
#include <fstream>
using namespace std; int main(){
ifstream ifile("./test.txt", ios::in);
if(!ifile){
cerr << "open err" << endl;
}
int index;
int value;
while(1){
cin >> index;
ifile.seekg(index, ios::beg); //移动指针
ifile >> value;
cout << value << endl;
}
}
  • 进制方式的按位置读
#include <iostream>
#include <fstream>
using namespace std; int main(){
ifstream ifile("./test.txt", ios::in | ios::binary);
if(!ifile){
cerr << "open err" << endl;
}
int index;
int value;
while(1){
cin >> index;
ifile >> value;
cout << value << endl;
}
}

6.文件与对象的例子

在构造函数里读取文件A,用文件A里的数据初始化对象;在析构函数里把对象的数据写入文件A。

#include <iostream>
#include <fstream>
using namespace std; class C{
friend ostream& operator<<(ostream&, const C&);
public:
C() : shi(0), xu(0){
ifstream ifile("./data.dat", ios::in);
if(!ifile){
cerr << "open err" << endl;
exit(1);
}
ifile >> shi >> xu;
ifile.close();
}
C(int i, int j) : shi(i), xu(j){}
~C(){
ofstream ofile("./data.dat", ios::out);
if(!ofile){
cerr << "open err" << endl;
exit(1);
}
ofile << shi << " " << xu;
ofile.close();
}
C(int i, int j) : shi(i), xu(j){}
~C(){
ofstream ofile("./data.dat", ios::out);
if(!ofile){
cerr << "open err" << endl;
exit(1);
}
ofile << shi << " " << xu;
ofile.close();
}
void setC(int i, int j){
shi = i;
xu = j;
}
private:
int shi;
int xu;
}; ostream& operator<<(ostream& out, const C& c){
out << "(" << c.shi << "," << c.xu << ")";
return out;
}
int main(){
C c;
cout << c << endl;
c.setC(10,22);
cout << c << endl;
}

最新文章

  1. iOS 类微信语音播放之切换听筒和扬声器的方法解决方案
  2. Xenu Link Sleuth-简单使用
  3. 【剑指offer】题目36 数组中的逆序对
  4. Atitit.如何选择技术职业方向
  5. 科谱,如何单机环境下合理的备份mssql2008数据库
  6. 使用VirtualEnvWrapper隔离python项目的库依赖
  7. 第1章 游戏之乐——让CPU占用率曲线听你指挥
  8. 【DP/二分】BZOJ 1863:[Zjoi2006]trouble 皇帝的烦恼
  9. POJ 3422 Kaka&#39;s Matrix Travels(最小费用最大流)
  10. 武汉科技大学ACM :1008: 小t和小w
  11. C# WinForm dataGridView 技巧小结
  12. 三种计算c#程序运行时间的方法
  13. 某pdf转word v6.3.0.2算法分析
  14. JS方法:数字转换为千分位字符
  15. 关于接口(Interface)
  16. 用C#写的一个OA类的APP, ios、Android都能跑,有源代码
  17. idea基本使用1
  18. Jenkins部署码云SpringBoot项目到远程服务器
  19. (转)Shiro学习
  20. Java基础-虚拟内存之映射字节缓冲区(MappedByteBuffer)

热门文章

  1. QT 完美实现圆形按钮
  2. UVC 驱动调用过程与驱动框架的简单分析
  3. 【golang-GUI开发】Qt项目的打包发布
  4. “笨方法”学习Python笔记(2)-VS Code作为文本编辑器以及配置Python调试环境
  5. [转]Ble蓝牙的使用手册
  6. 行为型---命令模式(Command Pattern)
  7. mysql双主+keepalived
  8. 改变eclipse默认的Tomcat部署路径
  9. 【Spring】17、spring cache 与redis缓存整合
  10. Javascript继承1:子类的的原型对象----类式继承