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 ...
随机推荐
- 【Python】Python3纯代码极简教程
#!/usr/bin/python3 ''' Python3.6.x简单教程 示例.注释 交互式和脚本式编程 变量类型 数字(Number) 字符串(String) 列表(Li ...
- 垂直居中—3行CSS3代码
方法一: .element { position: relative; top: 50%; transform: translateY(-50%); } 这用用的好处了,无论是块级元素还是行内元素,都 ...
- mybatis-generator插件执行报错:Cannot resolve classpath entry
记录一个小问题 使用了mybatis-generator插件自动生成实体类,DAO,Mapper,在执行时报错.报错信息如下 Failed to execute goal org.mybatis.ge ...
- SQL命令入门。
1.创建数据库:create database ***: 2.删除数据库:drop database ***: 3.创建数据库的时候设置一些参数选项. create database MyDatab ...
- WPF BackGroundWord 异步加载更新进度条示例
<Window x:Class="AsynchronousLoading.MainWindow" xmlns="http://schemas.microsoft.c ...
- Html5游戏开发-图形与动画(一)
最近研究了一下出来了很久的HTML5,总结了一下,准备来个系列,文中也许有很多问题,欢迎大家指正. Canvas介绍 canvas用于在网页中绘制图形的一个元素,具体内容请查看 -> HTML5 ...
- MyBatis:Pagehelper分页
对于分页插件这里选择查询所有用户的信息,以列表返回 前端只需输入分页数的数据既可 service实现类也很方便,甚至我都开始有点喜欢上这种Example的SQL形式了. 最后页面调用url的json信 ...
- js高级:event,事件冒泡,事件捕获
1.事件 浏览器客户端上客户触发的行为都称为事件 所有的事件都是天生自带的,不需要我们去绑定,只需要我们去触发. 通过 obj.事件名=function(){} 事件名:onmouseover 鼠标悬 ...
- MyBatis进阶使用——动态SQL
MyBatis的强大特性之一就是它的动态SQL.如果你有使用JDBC或者其他类似框架的经验,你一定会体会到根据不同条件拼接SQL语句的痛苦.然而利用动态SQL这一特性可以彻底摆脱这一痛苦 MyBati ...
- adb命令中的keyevent事件
电话键 KEYCODE_CALL: 拨号键 KEYCODE_ENDCALL: 挂机键 KEYCODE_HOME: 按键Home KEYCODE_MENU: 菜单键 KEYCODE_BACK: 返回键 ...