17.文件操作

17.1 文件流类

17.2 文件的打开关闭

#include<fstream>

fstream oFile("D:\\dang.dat",ios::out|ios::binary); 或 fstream oFile;

oFile.open("D:\\......",ios::out|ios::binary);

检查是否成功打开

if(!oFile)

cout<<"oFile open error"<<endl;

17.3 文件的读写

(1)文本文件的读写

#include<iostream>
#include<fstream>
using namespace std;
char *a;
int main(){
    ofstream oFile("D:\\dang.txt",ios::out);
    if(!oFile)
    cout<<"ofile open error";
    char p[10];
    for(int i=0;i<5;i++)
    cin>>p[i];
    for(int i=0;i<5;i++)
    oFile<<p[i];
    oFile.close();

    ifstream iFile("D:\\dang.txt",ios::in);
    if(!iFile)
    cout<<"ifile open error";
    char str[10];
    iFile.getline(str,10);
    cout<<str<<endl;
    iFile.close();

    return 0;
}

(2)二进制文件读写

write和read

*   ostream write(const char * buffer,int count);

继承自ostream,将内存中buffer指向的count个字节写入文件

**  istream read(char * buffer,int count);

继承自iostream,从文件中读取count个字节内容存放到buffer指向的内存缓冲区中

#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

class student{
    public:
        char name[20];
        int age;
}; 

int main() {
    //写入几个对象至文件
    student stu;
    fstream oFile("erjinzhi.dat",ios::out|ios::binary);
    if(!oFile)
    cout<<"oFile open error";
    while(cin>>stu.name>>stu.age){
      if(strcmp(stu.name,"end")==0) break;
        oFile.write( (char *) & stu, sizeof(stu) );
        }
       oFile.close();

     //读出刚刚写入的文件
    fstream iFile("erjinzhi.dat",ios::in|ios::binary);
    if(!iFile)
    cout<<"iFile open error";
    while(iFile.read((char*)&stu,sizeof(stu)))
    cout<<stu.name<<" "<<stu.age<<endl;
    iFile.close();

    //改变第三个对象的名字,再读出文件
    fstream make_File("erjinzhi.dat",ios::in|ios::out|ios::binary);
    if(!make_File)
    cout<<"make_FIle open error";
    make_File.seekp(2*sizeof(stu),ios::beg);//定位写指针
    make_File.write("Make",sizeof("Make")+1);
    make_File.seekg(0,ios::beg);//定位读指针
    while(make_File.read((char*)&stu,sizeof(stu)))
    cout<<stu.name<<" "<<stu.age<<endl;
    make_File.close();

    return 0;
}

文件流类put和get成员函数读写(文件拷贝)

在控制台里面执行

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[]){
    if(argc!=3){
        cout<<"File name missing!"<<endl;
        return 0;
    }

    ifstream iFile(argv[1],ios::binary|ios::in);//打开文件用于读
    if(!iFile){
    cout<<"iFile open error"<<endl;
    return 0;
    }
    ofstream oFile(argv[2],ios::binary|ios::in);
    if(!oFile){
    cout<<"oFile open error"<<endl;
    iFile.close();
    return 0;
    }

    char c;
    while(iFile.get(c))
        oFile.put(c);

    oFile.close();
    iFile.close();    

    return 0;
} 

17.4 操作文件读写指针

ostream & seekp(int offset,int mode);

istream & seekg(int offset,int mode);

*  mode 代表读写指针的模式

ios::beg   从文件开始向后移动offset字节,offset为正或0

ios::cur    从当前位置向后(或前)移动offset个字节,offset正负数,0皆可

ios::end   从文件结尾向前offset字节,offset只能是正数或0

** 得到读写指针位置

int tellp();//返回写指针位置

int tellp();//返回读指针位置

时间: 2024-07-31 19:58:59

17.文件操作的相关文章

day 17 文件操作17:14 -17:43

import _import_ 以字符串形式导入 # f=open('陈粒',encoding='utf-8')# data=f.read()# print(data)# f.close()//默认是读取r模式,只能读取 f.readable() 是否可读取 data=f.readlines()一次读取一行print(data)f.close() 原文地址:https://www.cnblogs.com/yikedashuyikexiaocao/p/9205015.html

C/C++文件操作

1 基于C的文件操作 在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作 2 一.流式文件操作 3 4 1.fopen() 5 FILE *fopen(const char *filename,const char *mode) 6 "r" 以只读方式打开文件 7 "w" 以只写方式打开文件 8 "a" 以追加方式打开文件 9 "r+" 以读/写方式打开文件,如无文件出错 10 "w+&quo

python第三节函数,文件操作

文件操作#以读的方式打开文件# f=open('c.txt',encoding='utf-8')# print(f)# data = f.read() #以读的方式打开# print(data)# print(f.closed) #判断文件是否是关闭状态# print(f.encoding)#输出文件编码# print(f.name)   #输出文件名称# print(f.readable())#判断文件是否是读模式# print(f.readline(),end='')#一次只读一行,end去

C语言入门(二十五)文件操作

文件操作  一.标准文件的读写 1.文件的打开fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程序就可用此FILE指针来实现对指定文件的存取操作了.当使用打开函数时,必须给出文件名.文件操作方式(读.写或读写),如果该文件名不存在,就意味着建立(只对写文件而言,对读文件则出错),并将文件指针指向文件开头.若已有一个同名文件存在,则删除该文件,若无同名文件,则建立该文件,并将文件指针指向文件开头. fopen(char *f

python成长之路第三篇(4)_作用域,递归,模块,内置模块(os,ConfigParser,hashlib),with文件操作

打个广告欢迎加入linux,python资源分享群群号:478616847 目录: 1.作用域 2.递归 3.模块介绍 4.内置模块-OS 5.内置模块-ConfigParser 6.内置模块-hashlib 7.with文件操作 代码执行环境默认为3.5.1 一.作用域 (1)什么是作用域,官方来说作用域就是就是有不同的命名空间,就像下面的代码一样,a=1它的作用域是全局的,也在全局的命名空间当中,当函数 action中的a=2它就在局部的作用域中,也在局部的命名空间当中. a = 1def

C语言4——文件操作

1.文件操作 int main(){ FILE *p=fopen("D:\\temp\\a.txt","w");//用写的方式打开一个文件 //w的意思是如果文件不存在就建立一个,如果文件存在就覆盖 fputs("hello world",p);//向文件中写入一个字符串 fclose(p);//关闭文件 } int main(void){ char s[1024]={0}; FILE *p=fopen("D:\\temp\\a.txt

Java文件操作系列[2]——使用JXL操作Excel文件

由于java流无法实现对Excel文件的读写操作,因此在项目中经常利用第三方开源的组件来实现.支持Excel文件操作的第三方开源组件主要有Apache的POI和开源社区的JXL. 总体来说,二者的区别是:JXL较为轻量级,如果是对Excel文件的简单操作,建议使用JXL:POI的功能相当强大,但同时处理问题也相当的繁琐. 1.准备工作 [必需]下载JXL的jar包:jxl.jar [非必需]JXL API  (提取密码:zgqj) 2.一些必要的说明 主要是对Excel结构的说明: Excel后

Python 第三天 文件操作

文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开 文件句柄 = file('文件路径', '模式') 注:python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open. open会自己在Python中找. 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读,也

python 文件操作总结

文件操作对编程语言的重要性不用多说,如果数据不能持久保存,信息技术也就失去了意义.按照本人经验,IO也是蛮头疼的一件事,因为不会用得太多,所以总是记不住API,每次都要重新google就会打断思路,还不一定每次都快速得到正确的文章. 本文内容包括: 文件的读写操作 文件的各种系统操作 存储对象 基于字符read & write 最基本的文件操作当然就是在文件中读写数据.这也是很容易掌握的.现在打开一个文件以进行写操作: 1. fileHandle = open ( 'test.txt', 'w'