C和C++文件操作学习笔记

C语言版

打开文件

FILE *fp=fopen(const char*,const char*);//
//第一个参数为打开路径,第二个参数为打开模式。

若打开成功fopen()会返回一个结构体指针,否则返回NULL

使用fopen()函数打开的文件会先将文件复制到缓冲区。注意:所下达的读取或写入动作,都是针对缓冲区进行存取而不是磁盘,只有当使用fclose()函数关闭文件时,缓冲区中的数据才会写入磁盘。

文本打开模式
r”:只能从文件中读数据,该文件必须先存在,否则打开失败
w”:只能向文件写数据,若指定的文件不存在则创建它,如果存在则先删除它再重建一个新文件
a”:向文件增加新数据(不删除原有数据),若文件不存在则打开失败,打开时位置指针移到文件末尾
r+”:可读/写数据,该文件必须先存在,否则打开失败
w+”:可读/写数据,用该模式打开新建一个文件,先向该文件写数据,然后可读取该文件中的数据
a+”:可读/写数据,原来的文件不被删去,位置指针移到文件末尾

打开二进制文件的模式与打开文本文件的含义是一样的,不同的是模式名称里面多一个字母’b’,以表示以二进制形式打开文件。

关闭文件

fclose(FILE *);

关闭成功返回值0,否则返回非0值。

判断文件是否读取完毕

feof(FILE *);

未完返回0,否则返回其他值。
从文件读取单个字符

fgetc(FILE *);

将单个字符写入文件

fputc(char ,FILE *);

字符串存取函数
(1) 读入字符串

fgets(char *,int,FILE *);

参数int为要求得到的字符个数,但实际上只从文件中读取了n-1个字符,在最后加一个’\0’字符。

(2)写入字符串

fputs(const char *,FILE *);

格式化读写函数

fscanf()
fprintf()

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
const char* in = "C:\\Users\\asus\\Desktop\\try.txt";
int main() {
	FILE* fp = fopen(in, "w");
	char ch = 'c';
	if (fp != NULL) {
		printf("文件打开成功\n");
		fprintf(fp, "% c", ch);
		fclose(fp);
	}
	return 0;
}

二进制文件操作
fread(void *,int size,int count,FILE*)
fwrite(const void *,int size,int count,FILE*)

fread(buffer, size, count, fp);
fwrite(buffer, size, count, fp);
buffer :对fread()来说是读入数据的存放的地址,fwrite()是输出数据存放的地址
size :是读写数据时,单笔数据的大小
count:是读写数据的笔数
fp :文件指针。

C++版

使用std::fstream类实现文件的操作
需要包含头文件<fstream>

#include<fstream>

使用open()和close()打开和关闭文件
open()函数的使用:
填写两个参数:文件路径和文件读取方式
打开文件
方法一:

fstream myFile;
myFile.open("abc.txt",ios_base::in|ios_base::out|ios_base_trunc);
if(myFile.is_open()){
	//进行读写操作
}

方法二:使用构造函数

fstream myFile("abc.txt",ios_base::in|ios_base::out|ios_base::trunc);

打开文件流的模式:

ios_base::app//附加到现有文件的末尾,而不是覆盖它。
ios_base::ate//切换到文件末尾,但可以在文件的任何位置写入数据。
ios_base::trunc//覆盖存在的文件
ios_base::binary//创建而进制文件(默认是文本文件)
ios_base::in//以只读方式打开文件
ios_base::out//以只写方式打开文件

使用open()创建文本文件并使用运算符<<写入文本

#include<iostream>
#include<fstream>
using namespace std;
const char* f = "C:\\Users\\asus\\Desktop\\try.txt";
int main() {
	fstream myFile;
	myFile.open(f,ios_base::in|ios_base::out);
	if (myFile.is_open()) {
		cout << "myFile has opened" << endl;
		myFile << "lcj is ok\n";
		myFile.close();
	}
	system("pause");
	return 0;
}

一定记住打开文件后要关闭。
open()和运算符>>读取文本文件

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const char* f = "C:\\Users\\asus\\Desktop\\try.txt";
int main() {
	fstream myFile;
	myFile.open(f,ios_base::in|ios_base::out);
	if (myFile.is_open()) {
		cout << "myFile contains:" << endl;
		string x;
		while (myFile.good()) {
			getline(myFile, x);
			cout << x;
		}
		myFile.close();
	}
	system("pause");
	return 0;
}

读写二进制文件
文件的打开和关闭与之前的文本文件差别不大。重要的是打开文件时使用
ios_base::binary,通常使用ofstream::writeifstream::read来读写二进制文件。

#define _CRT_SECURE_NO_WARNINGS
#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
using namespace std;
const char* file = "C:\\Users\\asus\\Desktop\\mybin.bin";
struct Human {
	Human() {};
	Human(const char* inName, int inAge, const char* inDOB) :age(inAge) {
		strcpy(name, inName);
		strcpy(DOB, inDOB);
	}

	int age;
	char name[20], DOB[20];
};

int main() {
	Human Input("Curry", 30, "March 1988");
	ofstream fsOut("file", ios_base::out | ios_base::binary);
	if (fsOut.is_open()) {
		cout << "Writing one object of Human to a binary file" << endl;
		fsOut.write(reinterpret_cast<const char*>(&Input), sizeof(Input));
		fsOut.close();
	}
	ifstream fsIn("file", ios_base::in | ios_base::binary);
	if (fsIn.is_open()) {
		Human someperson;
		fsIn.read((char*)&someperson, sizeof(someperson));
		cout << "Reading information from binary file: " << endl;
		cout << "Name: " << someperson.name << endl;
		cout << "Age: " << someperson.age << endl;
		cout << "Date of Birth: " << someperson.DOB << endl;
		fsIn.close();
	}
	return 0;
}

参考:《21天学通C++》 博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值