博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
128.C++文件操作小结
阅读量:4558 次
发布时间:2019-06-08

本文共 3696 字,大约阅读时间需要 12 分钟。

打开后缀参数

 

 

1 #include 
2 #include
3 using namespace std; 4 5 //文本读写 6 //文件写入 7 void main1() 8 { 9 //写入到文件 in读o写 10 //创建一个输出流 11 ofstream out; 12 //输出到文件 13 out.open("C:\\123.txt"); 14 out << "hello file" << endl; 15 out.close(); 16 system("C:\\123.txt"); 17 cin.get(); 18 } 19 20 //文件读取 21 void main2() 22 { 23 //创建一个输出流 24 ifstream in; 25 in.open("C:\\123.txt"); 26 char str[256]{ 0 }; 27 //in >> str; 28 //从文件读取 29 in.getline(str, 256); 30 in.close(); 31 cout << str << endl; 32 cin.get(); 33 system("C:\\123.txt"); 34 } 35 36 //文件打开方式,追加 37 void main3() 38 { 39 //写入到文件 in读o写 40 //创建一个输出流 41 ofstream out; 42 //输出到文件(追加模式) 43 out.open("C:\\123.txt",ios::app); 44 out << " hello world" << endl; 45 out.close(); 46 system("C:\\123.txt"); 47 cin.get(); 48 } 49 50 51 //二进制文件读写 52 struct info 53 { 54 char name[10]; 55 int id; 56 double price; 57 }; 58 59 //把结构体的信息按二进制方式写入文件 60 void main4() 61 { 62 struct info infs[3] = { {
"xiaowang1",1,100},{ "xiaowang2",2,200 },{ "xiaowang3",3,300 } }; 63 //二进制方式读 64 ofstream fout("bin.bin", ios::binary); 65 //内存写入到磁盘 (必须要转换成char *) 66 fout.write((char *)infs, sizeof(infs)); 67 fout.close(); 68 69 struct info infs2[3]; 70 //二进制方式写(必须要转换成char *) 71 ifstream fin("bin.bin",ios::binary); 72 fin.read((char*)infs2, sizeof(infs2)); 73 fin.close(); 74 for (auto i : infs2) 75 { 76 cout << i.id << i.name << i.price << endl; 77 } 78 system("pause"); 79 } 80 81 //把结构体的信息按文本方式写入到文件 82 void main5() 83 { 84 struct info infs[3] = { { "xiaowang1",1,100 },{ "xiaowang2",2,200 },{ "xiaowang3",3,300 } }; 85 86 //文本文件读写 87 ofstream fout("1.txt", ios::out|ios::app); 88 89 for (auto i : infs) 90 { 91 fout << i.name << i.price << i.id << endl; 92 } 93 94 fout.close(); 95 96 ifstream fin("1.txt"); 97 for (int i = 0; i < 3; i++) 98 { 99 char str[255]{ 0 };100 fin.getline(str, 254);101 cout << str << endl;102 }103 fin.close();104 system("pause");105 }106 107 //文件指针移动到指定位置读seekg108 void main6()109 {110 ofstream fout("hello.txt");111 if (!fout)112 {113 cout << "操作失败";114 }115 116 fout << "123456789abcdefghijklmnopqrstuvwxyz";117 fout.close();118 119 ifstream fin("hello.txt");120 if (fin.fail())121 {122 cout << "操作失败";123 }124 //从开始往后移动9个位置开始读125 fin.seekg(9, ios::beg);126 char ch;127 while (fin.get(ch))128 {129 cout << ch;130 }131 fin.close();132 system("pause");133 }134 135 //追加方式文件指针移动到指定位置写(失效)136 void main7()137 {138 char ch;139 ifstream fin("hello.txt");140 while (fin.get(ch))141 {142 cout << ch;143 }144 fin.close();145 146 //以追加的方式的打开147 ofstream fout("hello.txt",ios::app);148 //从头开始往后移动五个位置(到第五个位置之后)149 //如果是追加移动无效150 fout.seekp(5, ios::beg);151 fout << "ceshiceshi";152 fout.close();153 system("hello.txt");154 system("pause");155 }156 157 //读写方式文件指针移动到指定位置写(seekp)158 void main()159 {160 char ch;161 ifstream fin("hello.txt");162 while (fin.get(ch))163 {164 cout << ch;165 }166 fin.close();167 168 //以读写的方式的打开(会造成重写)169 ofstream fout("hello.txt", ios::in | ios::out);170 //从头开始往后移动五个位置(到第五个位置之后)171 //如果是追加移动无效172 fout.seekp(5, ios::beg);173 fout << "ceshiceshi";174 fout.close();175 system("hello.txt");176 system("pause");177 }

 

转载于:https://www.cnblogs.com/xiaochi/p/8620733.html

你可能感兴趣的文章
linux系统下php安装mbstring扩展的二种方法
查看>>
gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
查看>>
Hadoop-MR实现日志清洗(一)
查看>>
Bootstrap 3之美01-下载并引入页面
查看>>
在Brackets中使用Emmet
查看>>
lodash用法系列(5),链式
查看>>
ASP.NET Web API的安全管道
查看>>
推荐一个好用的 sqlite 管理器 sqliteman 感觉比 navicat 好用
查看>>
第三周学习进度报告
查看>>
使用JSON Web Tokens和Spring实现微服务
查看>>
JS学习笔记 - 运动 - 淘宝轮播图
查看>>
之字形打印矩阵
查看>>
POJ 1004 Financial Management
查看>>
HDU 2011 多项式求和
查看>>
docker network
查看>>
BZOJ3745: [Coci2015]Norma
查看>>
真有效值与有效值概念
查看>>
二叉堆
查看>>
[HDOJ3711]Binary Number(枚举)
查看>>
leetcode-Single Number III-260
查看>>