文件操作方式

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、C语言

一、fseek

文件的随机读写(fseek / lseek)

C语言文件操作(三) —— 文件的随机读写(fseek / lseek)_文件随机读写_仲夏夜之梦~的博客-CSDN博客

函数原型:

int fseek(FILE* stream, long offset, int origin);

第一个参数:文件指针

第二个参数:相对于起始位置的偏移量。

offset < 0,从起始位置起,向左移动 | offset | 个单位
offset > 0,从起始位置起,向右移动 | offset | 个单位


第三个参数:起始位置。可选值有三个

可选值解析
SEEK_CUR当前文件指针指向的位置
SEEK_END文件末尾
SEEK_SET返回值:成功返回0,失败返回一个非零值。

返回值:成功返回0,失败返回一个非零值。

二、文件操作模式

提示:考察了文件读写操作的几种模式。

r代表read的简写,+代表可读可写,w代表write,b代表bit二进制位,t代表text。

r 打开只读文件,该文件必须存在。

r+ 打开可读可写的文件,该文件必须存在(这里的写文件是指将之前的文件覆盖。

rt 打开只读文本文件,该文本必须存在。

rt+ 读写打开一个文本文件,允许读和写,该文件必须存在(这里的写文件是指将之前的文件覆盖。

rb 只读打开一个二进制文件,,该文本必须存在。

rb+ 读写打开一个文本文件,允许读和写,该文件必须存在(这里的写文件是指将之前的文件覆盖。

w 打开只写文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件。

w+ 打开可读写文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件(这里的读文件,同样需要使用rewind()函数)。

wt 打开只写文本文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件。

wt+ 打开可读写文本文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件。

wb 打开只写二进制文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件。

wb+ 打开可读写文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件。

a以附加的方式打开只写文件,若文件不存在,则建立文件,存在则在文件尾部添加数据,即追加内容。

a+以附加的方式打开可读写文件,不存在则建立文件,存在则写入数据到文件尾(这里的读文件,同样需要使用rewind()函数,但是写文件不需要rewind()函数,a是追加)。

at二进制数据的追加,不存在则创建,只能写。

at+读写打开一个文本文件,允许读或在文本末追加数据(这里的读文件,同样需要使用rewind()函数,但是写文件不需要rewind()函数,a是追加)。

ab二进制数据的追加,不存在则创建,只能写。

ab+读写打开一个二进制文件,不存在则创建,允许读或在文本末追加数据(这里的读文件,同样需要使用rewind()函数,但是写文件不需要rewind()函数,a是追加)。

详见博文:http://blog.sina.com.cn/s/blog_155aff35b0102wxby.html

C语言打开文件详解-腾讯云开发者社区-腾讯云

文件操作方式demo:

第一次写的demo

#include <fstream>

int main()
{
    ifstream in;
    ofstream out;
    char c;
    out.open("D:/APP/codeBlock/testDel2/test.txt", ios::in);
    out << "this is test string";
    out.close();
    in.open("D:\\APP\\codeBlock\\testDel2\\test.txt", ios::out);
    while(in.get(c))
    {
        printf("%c",c);
    }

    return 0;
}

实现文件读写,这个是C++的

下面写c语言的

#include <stdlib.h>
#include <stdio.h>
#define N 100

int main()
{
    FILE *fp;

    fp = fopen("D:/APP/codeBlock/testDel2/test.txt", "wt");
    fprintf(fp, "this is second string");
    fprintf(fp, "this is third string");
    fclose(fp);

    fp = fopen("D:/APP/codeBlock/testDel2/test.txt", "rt");
    char str[N+1] = {'\0'};



    while(fgets(str, N, fp) != NULL)
    {
        printf("%s", str);
    }
    fclose(fp);
}

//一开始fp = fopen("D:/APP/codeBlock/testDel2/test.txt", "rw");写不进数据
//追加写a+没实现

上述实现C语言读写,未实现追加读写,未做空指针保护。

#include <stdlib.h>
#include <stdio.h>
#define N 100

int main()
{
    FILE *fp;

    fp = fopen("D:/APP/codeBlock/testDel2/test.txt", "a+");
    fseek(fp, 0L, SEEK_END); // 移动文件指针到文件末尾
    fputs("this is fourth string", fp);
    fclose(fp);

    fp = fopen("D:/APP/codeBlock/testDel2/test.txt", "rt");
    char str[N+1] = {'\0'};


    while(fgets(str, N, fp) != NULL)
    {
        printf("%s", str);
    }
    fclose(fp);
}

实现追加读写。a+当文件不存在时,创建文件。

二、c++

一、ifstream、ofstream、fstream

1、ofstream是从内存到硬盘;

2、ifstream是从硬盘到内存

2、ifstream类支持文件的输入,ofstream类支持文件的输出操作,fstream类支持文件的输入输出操作,它们的定义在头文件<fstream>中。

下面写一个demo测试一下

新建一个txt文件,文件名叫test.txt,路径在E盘根目录

内容为:

test file
hello world
this is third line!

再写代码:

//这边写一个c++代码用ifstream读txt文件的代码
//首先是磁盘上有一个txt文件,创建一个E:\\test.txt

#include <iostream>

#include <fstream>

using namespace std;

int main()
{
    string filePath= "E:\\test.txt";
    ifstream inFile;
    inFile.open(filePath.c_str());
    if(!inFile.is_open())
    {
        return false;
    }

    string line;
    while(getline(inFile, line))
    {
        if(line.empty())
        {
            continue;
        }
        cout << line << endl;
    }

    inFile.close();

    return 0;
}


//test file
//hello world
//this is third line!

验证成功! 

三、另一种文件操作方式

ios::ate和ios::app在C++文件中的区别_std::ios::app_flying_coder的博客-CSDN博客

四、书籍知识点

1、demo代码

#include <iostream>
#include <fstream>					// 文件操作必需的头文件
using namespace std;
int  main()
{
	fstream  file1;					// 定义一个fstream类的对象用于读
	file1.open("Ex_DataFile.txt", ios::in);	
	if (!file1)		
	{
		cout<<"Ex_DataFile.txt不能打开!\n";
		return -1;
	}
	fstream  file2;					// 定义一个fstream类的对象用于写
	file2.open("Ex_DataFileBak.txt", ios::out | ios::trunc);	
	if (!file2)	
	{
		cout<<"Ex_DataFileBak.txt不能创建!\n";
		file1.close();	return -2;
	}
	char ch;
	while (!file1.eof())	
	{
		file1.read(&ch, 1);
		cout<<ch;	
		file2.write(&ch, 1);
	}
	file2.close();					// 不要忘记文件使用结束后要及时关闭
	file1.close();
	return 0;	
}

//运行条件,要在代码同级文件夹下创建Ex_DataFile.txt和Ex_DataFileBak.txt,代码功能是将Ex_DataFile.txt文件中数据写入Ex_DataFileBak.txt文件中。

第二个demo代码

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
class CStudent
{
public:
	CStudent(char* name, char* id, float score = 0);
	void print();
	friend ostream& operator<< ( ostream& os, CStudent& stu );
	friend istream& operator>> ( istream& is, CStudent& stu );
private:
	char strName[10];				// 姓名
	char strID[10];					// 学号
	float fScore;					// 成绩
};
CStudent::CStudent(char* name, char* id, float score)
{
	strncpy(strName, name, 10);
	strncpy(strID, id, 10);
	fScore = score;
}
void CStudent::print()
{
	cout<<endl<<"学生信息如下:"<<endl;
	cout<<"姓名:"<<strName<<endl;
	cout<<"学号:"<<strID<<endl;
	cout<<"成绩:"<<fScore<<endl;
}
ostream& operator<< ( ostream& os, CStudent& stu )
{
	os.write(stu.strName, 10);
	os.write(stu.strID, 10);
	os.write((char *)&stu.fScore, 4);
	return os;
}
istream& operator>> ( istream& is, CStudent& stu )
{
	char name[10];
	char id[10];
	is.read(name, 10);
	is.read(id, 10);
	is.read((char*)&stu.fScore, 4);
	strncpy(stu.strName, name, 10);
	strncpy(stu.strID, id, 10);
	return is;
}
int  main()
{
	CStudent stu1("MaWenTao","99001",88);
	CStudent stu2("LiMing","99002",92);
	CStudent stu3("WangFang","99003",89);
	CStudent stu4("YangYang","99004",90);
	CStudent stu5("DingNing","99005",80);
	fstream file1;
	file1.open("student.dat",ios::out|ios::in|ios::binary|ios::trunc);
	file1<<stu1<<stu2<<stu3<<stu4<<stu5;
	CStudent* one = new CStudent("","");
	const int size = sizeof(CStudent);
	file1.seekp(size*4);	file1>>*one;		one->print();
	file1.seekp(size*1);	file1>>*one;		one->print();
	file1.seekp(size*2, ios::cur);
	file1>>*one;		one->print();
	file1.close();
	delete one;
	return 0;	
}


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值