c++文件的读写

1.文本方式的写文件

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

2.文本方式的读文件

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

3.二进制方式的写文件

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

4.二进制方式的读文件

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

5.按位置读写文件

  • 文本方式的按位置读

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

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

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main(){
  5. ifstream ifile("./test.txt", ios::in);
  6. if(!ifile){
  7. cerr << "open err" << endl;
  8. }
  9. int index;
  10. int value;
  11. while(1){
  12. cin >> index;
  13. ifile.seekg(index, ios::beg); //移动指针
  14. ifile >> value;
  15. cout << value << endl;
  16. }
  17. }
  • 进制方式的按位置读
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main(){
  5. ifstream ifile("./test.txt", ios::in | ios::binary);
  6. if(!ifile){
  7. cerr << "open err" << endl;
  8. }
  9. int index;
  10. int value;
  11. while(1){
  12. cin >> index;
  13. ifile >> value;
  14. cout << value << endl;
  15. }
  16. }

6.文件与对象的例子

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

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. class C{
  5. friend ostream& operator<<(ostream&, const C&);
  6. public:
  7. C() : shi(0), xu(0){
  8. ifstream ifile("./data.dat", ios::in);
  9. if(!ifile){
  10. cerr << "open err" << endl;
  11. exit(1);
  12. }
  13. ifile >> shi >> xu;
  14. ifile.close();
  15. }
  16. C(int i, int j) : shi(i), xu(j){}
  17. ~C(){
  18. ofstream ofile("./data.dat", ios::out);
  19. if(!ofile){
  20. cerr << "open err" << endl;
  21. exit(1);
  22. }
  23. ofile << shi << " " << xu;
  24. ofile.close();
  25. }
  26. C(int i, int j) : shi(i), xu(j){}
  27. ~C(){
  28. ofstream ofile("./data.dat", ios::out);
  29. if(!ofile){
  30. cerr << "open err" << endl;
  31. exit(1);
  32. }
  33. ofile << shi << " " << xu;
  34. ofile.close();
  35. }
  36. void setC(int i, int j){
  37. shi = i;
  38. xu = j;
  39. }
  40. private:
  41. int shi;
  42. int xu;
  43. };
  44. ostream& operator<<(ostream& out, const C& c){
  45. out << "(" << c.shi << "," << c.xu << ")";
  46. return out;
  47. }
  48. int main(){
  49. C c;
  50. cout << c << endl;
  51. c.setC(10,22);
  52. cout << c << endl;
  53. }

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. JAVA课程设计——一个简单的教务人事管理系统

    大三上学期期末总结,没错,上学期,写在下学期新学期开始,哈哈哈. 上学期学习了面向对象程序设计,课程设计的题目使用JAVA语言完成一个简单的教务人事管理系统,能够实现访问数据库的登录验证,分别按部门和 ...

  2. 动手实践Mybatis插件

    前言 Mybatis的插件开发过程的前提是必须要对Mybatis整个SQL执行过程十分熟悉,这样才能正确覆盖源码保证插件运行,总的来说Mybatis的插件式一种侵入式插件,使用时应该十分注意. 在之前 ...

  3. “多个单核CPU”与“单个多核CPU”哪种方式性能较强?

    多个单核CPU: 成本更高,因为每个CPU都需要一定的线路电路支持,这样对主板上布局布线极为不便.并且当运行多线程任务时,多线程间通信协同合作也是一个问题.依赖总线的传输,速度较慢,且每一个线程因为运 ...

  4. 集成腾讯位置服务到webapi

    经纬度转换为详细地址信息 参考文档:http://lbs.qq.com/webservice_v1/guide-gcoder.html 首先申请key,如果使用的是服务端请求webservice AP ...

  5. 从零开始学安全(十一)●IP地址

    127 都是本机地址 ip  DE 类网段 都是广播网段 它并不指向特定的网络 用不上

  6. sql语句求百分比

    此sql语句包括了两个聚合函数做除法求百分比,并保留两位小数,直接输出字符串形式的百分比.以及对case when在聚合函数的应用. SELECT ss.SS_NAME,SS_ID, COUNT(ea ...

  7. npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\package.json'

    在使用 npm 命令安装常用的 Node.js web框架模块 express时出现: 解决方法是 在命令行切换到安装nodejs文件下的nodejs\node_modules\npm  后执行npm ...

  8. 【Java每日一题】20170207

    20170206问题解析请点击今日问题下方的“[Java每日一题]20170207”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...

  9. Verification and validation

    Verification Verification is the process to make sure the product satisfies the conditions imposed a ...

  10. Advanced redirection features

    here are three types of I/O, which each have their own identifier, called a file descriptor: standar ...