c++文件的读写
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++文件的读写的更多相关文章
- C#对于文件的读写
C#文件的读写操作 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// 写入txt文件 /// </summary> ...
- java filechannel大文件的读写
java读取大文件 超大文件的几种方法 转自:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/ java 读取一个 ...
- c# txt文件的读写
在公司实习,任务不太重,总结一下c#关于txt文件的读写,以便以后有用到的时候可以查看一下.如果有写得不完整的地方还请补充 说明:本人C#水平可能初级都谈不上,高手轻喷,参考:http://www.c ...
- C++中关于文件的读写
在C++的学习过程中,我们时常要用到对文件的操作,下面我们讲一下文件的读写. 首先,读.也就是把已有的文件读到控制台上,那么如何操作呢?首先要将文件操作的输入输出流包含进去. <fstream& ...
- php高并发状态下文件的读写
php高并发状态下文件的读写 背景 1.对于PV不高或者说并发数不是很大的应用,不用考虑这些,一般的文件操作方法完全没有问题 2.如果并发高,在我们对文件进行读写操作时,很有可能多个进程对进一文件 ...
- INI 文件的读写操作
在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...
- Java程序员的日常—— Properties文件的读写
在日常的Java程序开发中,Properties文件的读写是很常用的.经常有开发系统通过properties文件来当做配置文件,方便用户对系统参数进行调整. 那么本片就来简单的介绍下,如何使用Prop ...
- Android 对 properties文件的读写操作
-. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...
- C++学习48 对ASCII文件的读写操作
如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...
- Objective-C 【从文件中读写字符串(直接读写/通过NSURL读写)】
———————————————————————————————————————————从文件中读写字符串(直接读写/通过NSURL读写) #import <Foundation/Foundati ...
随机推荐
- JAVA课程设计——一个简单的教务人事管理系统
大三上学期期末总结,没错,上学期,写在下学期新学期开始,哈哈哈. 上学期学习了面向对象程序设计,课程设计的题目使用JAVA语言完成一个简单的教务人事管理系统,能够实现访问数据库的登录验证,分别按部门和 ...
- 动手实践Mybatis插件
前言 Mybatis的插件开发过程的前提是必须要对Mybatis整个SQL执行过程十分熟悉,这样才能正确覆盖源码保证插件运行,总的来说Mybatis的插件式一种侵入式插件,使用时应该十分注意. 在之前 ...
- “多个单核CPU”与“单个多核CPU”哪种方式性能较强?
多个单核CPU: 成本更高,因为每个CPU都需要一定的线路电路支持,这样对主板上布局布线极为不便.并且当运行多线程任务时,多线程间通信协同合作也是一个问题.依赖总线的传输,速度较慢,且每一个线程因为运 ...
- 集成腾讯位置服务到webapi
经纬度转换为详细地址信息 参考文档:http://lbs.qq.com/webservice_v1/guide-gcoder.html 首先申请key,如果使用的是服务端请求webservice AP ...
- 从零开始学安全(十一)●IP地址
127 都是本机地址 ip DE 类网段 都是广播网段 它并不指向特定的网络 用不上
- sql语句求百分比
此sql语句包括了两个聚合函数做除法求百分比,并保留两位小数,直接输出字符串形式的百分比.以及对case when在聚合函数的应用. SELECT ss.SS_NAME,SS_ID, COUNT(ea ...
- 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 ...
- 【Java每日一题】20170207
20170206问题解析请点击今日问题下方的“[Java每日一题]20170207”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...
- Verification and validation
Verification Verification is the process to make sure the product satisfies the conditions imposed a ...
- Advanced redirection features
here are three types of I/O, which each have their own identifier, called a file descriptor: standar ...