栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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个字符,在最后加一个’’字符。

(2)写入字符串

fputs(const char *,FILE *);

格式化读写函数

fscanf()
fprintf()

#define _CRT_SECURE_NO_WARNINGS
#include
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类实现文件的操作
需要包含头文件

#include

使用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
#include
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 okn";
		myFile.close();
	}
	system("pause");
	return 0;
}

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

#include
#include
#include
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::write和ifstream::read来读写二进制文件。

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
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(&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++》 博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号