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;
}

c++文件的读写的更多相关文章

  1. C#对于文件的读写

    C#文件的读写操作 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// 写入txt文件 /// </summary> ...

  2. java filechannel大文件的读写

    java读取大文件 超大文件的几种方法 转自:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/   java 读取一个 ...

  3. c# txt文件的读写

    在公司实习,任务不太重,总结一下c#关于txt文件的读写,以便以后有用到的时候可以查看一下.如果有写得不完整的地方还请补充 说明:本人C#水平可能初级都谈不上,高手轻喷,参考:http://www.c ...

  4. C++中关于文件的读写

    在C++的学习过程中,我们时常要用到对文件的操作,下面我们讲一下文件的读写. 首先,读.也就是把已有的文件读到控制台上,那么如何操作呢?首先要将文件操作的输入输出流包含进去. <fstream& ...

  5. php高并发状态下文件的读写

    php高并发状态下文件的读写   背景 1.对于PV不高或者说并发数不是很大的应用,不用考虑这些,一般的文件操作方法完全没有问题 2.如果并发高,在我们对文件进行读写操作时,很有可能多个进程对进一文件 ...

  6. INI 文件的读写操作

    在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...

  7. Java程序员的日常—— Properties文件的读写

    在日常的Java程序开发中,Properties文件的读写是很常用的.经常有开发系统通过properties文件来当做配置文件,方便用户对系统参数进行调整. 那么本片就来简单的介绍下,如何使用Prop ...

  8. Android 对 properties文件的读写操作

    -. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...

  9. C++学习48 对ASCII文件的读写操作

    如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...

  10. Objective-C 【从文件中读写字符串(直接读写/通过NSURL读写)】

    ———————————————————————————————————————————从文件中读写字符串(直接读写/通过NSURL读写) #import <Foundation/Foundati ...

随机推荐

  1. Python numpy中矩阵的用法总结

    关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...

  2. CC2530微处理器接口开发技术——信号灯的设计与实现

    本问主要介绍了CC2530处理器的通用输入/输出接口(GPIO),以及GPIO的位操作,理解GPIO的基本原理和功能,最后使用C语言驱动CC2530的GPIO实现对信号灯的控制. CC2530的GPI ...

  3. 前端(五)之display 总结与浮动

    前端之浮动布局.清浮动 display 总结 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  4. 第一册:lesson thirty three。

    原文:A fine day. It is a fine day today. There are some clouds in the sky. But the sun is shining. Mr. ...

  5. MyBatis学习总结(一)——ORM概要与MyBatis快速起步

    程序员应该将核心关注点放在业务上,而不应该将时间过多的浪费在CRUD中,多数的ORM框架都把增加.修改与删除做得非常不错了,然后数据库中查询无疑是使用频次最高.复杂度大.与性能密切相关的操作,我们希望 ...

  6. Docker虚拟机理论

    Docker虚拟机架构     ◆ Docker架构                 Docker创建的所有虚拟实例共用同一个Linux内核,对硬件占用较小,属于轻量级虚拟机   Docker镜像与容 ...

  7. WPF里ItemsControl的分组实现 --listbox 实现分组

    我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...

  8. tomcat域名绑定设置

    域名绑定分为单域名绑定.多域名绑定,配置主要涉及到tomcat目录下conf/server.xml文件 一.单域名绑定 1.修改server.xml 大约105行的内容(不是必须修改,如果只是绑定一个 ...

  9. linux下定时执行任务的方法

    linux下定时执行任务的方法 在LINUX中你应该先输入crontab -e,然后就会有个vi编辑界面,再输入0 3 * * 1 /clearigame2内容到里面 :wq 保存退出. 在LINUX ...

  10. 从零开始学安全(十)●TCP/IP协议栈

    局域网靠mac 地址通信