C++文件操作(一)

/*
	一、数据的层次:位 字节 域/记录
	
	二、顺序文件:将所有的记录顺序输入文件。一个有限字符构成的顺序字符流。
	
	三、C++标准库中:ifstream(文件读取) ofstream(文件写入) fstream(文件读取写入)
	
	四、使用/创建文件的基本流程:1. 打开文件 2.读/写文件 3.关闭文件
	
	五、建立顺序文件
		#include <fstream>
	
		ofstream outFile("clients.dat",ios::out|ios::binary);
		ios::out 输出到文件,删除原有内容
		ios::app 输出到文件,保留原有内容,总是在尾部添加
		ios::binary 以二进制文件格式打开文件

		ofstream fout;
		fout.open("test.out",ios::out|ios::binary);
		if(!fout){cerr<<"File open error!"<<endl;}

	六、文件的读写指针(标识文件操作的当前位置,指针在哪里,读写操作就在哪里进行)
		对于输入文件,有一个读指针
		对于输出文件,有一个写指针
		对于输入输出文件,有一个读写指针

		ofstream fout("a1.out",ios::app);
		long location = fout.tellp();
		location = 10L;
		fout.seekp(location);
		fout.seekp(location,ios::beg); //从头数location
		fout.seekp(location,ios::cur); //从当前位置数location
		fout.seekp(location,ios::end); //从尾部数location,location可以是负值

		ifstream fin("a1.in",ios::in);
		long location = fin.tellg();
		location = 10L;
		fout.seekg(location);
		fout.seekg(location,ios::beg); //从头数location
		fout.seekg(location,ios::cur); //从当前位置数location
		fout.seekg(location,ios::end); //从尾部数location,location可以是负值

	七、二进制文件读写
		int x = 10;
		fout.seekp(20,ios::beg);
		fout.write((const char*)(&x),sizeof(int));

		fin.seekg(0,ios::beg);
		fin.read((char *)(&x),sizeof(int));
		二进制文件读写,直接写二进制数据,记事本看未必正确

		显示关闭文件
		.close()
*/
#pragma warning(disable:4996)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class CStudent{
public:
	char szName[20];
	int nScore;
};
//
//int main()
//{
//	CStudent s;
//	ofstream OutFile("StuScores.dat", ios::out | ios::binary);
//	while (cin >> s.szName >> s.nScore){
//		if (stricmp(s.szName, "exit") == 0)
//			break;
//		OutFile.write((char*) &s, sizeof(s));
//	}
//	OutFile.close();
//	return 0;
//}
//
//
//int main()
//{
//	CStudent s;
//	ifstream inFile("StuScores.dat", ios::in | ios::binary);
//	if (!inFile){
//		cout << "error" << endl;
//		return 0;
//	}
//	while (inFile.read((char*) &s, sizeof(s))){
//		cout << s.szName << " " << s.nScore << endl;
//	}
//	inFile.close();
//	return 0;
//}
//

int main()
{
	CStudent s;
	fstream iofile("StuScores.dat", ios::in | ios::out | ios::binary);
	if (!iofile){
		cout << "error" << endl;
		return 0;
	}
	iofile.seekp(2 * sizeof(s), ios::beg);
	iofile.write("Mike", strlen("Mike") + 1);
	iofile.seekg(0, ios::beg);
	while (iofile.read((char*) &s, sizeof(s)))
		cout << s.szName << " " << s.nScore << endl;
	iofile.close();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dev C++是一个集成开发环境(IDE),用于C和C++编程。它提供了许多功能和工具,包括文件操作。下面是一些关于Dev C++文件操作的介绍: 1. 打开文件:使用`fopen`函数可以打开一个文件。例如,要打开一个名为"example.txt"的文本文件,可以使用以下代码: ```c FILE *file = fopen("example.txt", "r"); ``` 这将返回一个指向文件的指针。 2. 关闭文件:使用`fclose`函数可以关闭一个已打开的文件。例如,要关闭之前打开的文件,可以使用以下代码: ```c fclose(file); ``` 3. 读取文件内容:使用`fscanf`函数可以从文件中读取内容。例如,要从文件中读取一个整数,可以使用以下代码: ```c int num; fscanf(file, "%d", &num); ``` 4. 写入文件内容:使用`fprintf`函数可以向文件中写入内容。例如,要向文件中写入一个字符串,可以使用以下代码: ```c fprintf(file, "Hello, World!"); ``` 5. 检查文件结尾:使用`feof`函数可以检查是否已到达文件的结尾。例如,要检查是否已到达文件的结尾,可以使用以下代码: ```c if (feof(file)) { printf("已到达文件结尾\n"); } ``` 6. 检查文件是否成功打开:在打开文件时,可以检查返回的文件指针是否为NULL,以确定文件是否成功打开。例如,可以使用以下代码: ```c if (file == NULL) { printf("无法打开文件\n"); } ``` 这些是Dev C++中文件操作的基本介绍。你可以使用这些函数来读取和写入文件内容。如果你有其他关于Dev C++文件操作的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值