c++文件操作

1,c语言文件操作

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Student
{
	int id;
	char name[20];
};
class File 
{
	FILE* fp;
	public:
	bool open(const char* fileName, const char* mod) {
		fp = fopen(fileName, mod);
		if (fp == NULL) {
			cout << "open fail" << endl;
		//	abort();
			return false;
		}
		return true;
	}
	int get() {
		if (fp)
			return fgetc(fp);
		else 
			return -1;
	}
	char* get(char* s, int n) {
		return fgets(s, n, fp);
	}
	int put(char c) {
		return fputc(c, fp);
	}
	int put(const char* s) {
		return fputs(s, fp);
	}
	size_t read(Student& s) {
		return fread(&s, sizeof(Student), 1, fp);
	}
	size_t write(const Student& s) {
		return fwrite(&s, sizeof(Student), 1, fp);
	}
	bool close() {
		if (fp != NULL)
			return !fclose(fp);
		else
			return false;
	}
};

2,c++文件操作

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main()
{
	//*************初始化**********//
	fstream f1; //没有绑定任何文件的对象
	fstream f2("file"); //绑定文件file 读写
//	fstream f3("file", ios::in);//以mode方式打开
		//	ios::in | ios::binary  以多种方式打开	
		//mode 如下:
		//ios::in	读
		//ios::out  写, 如果没有文件会创建,如果有文件清空
		//ios::app	以读/写方式打开, 追加
		//ios::binary 二进制
		//ios::trunc  打开文件并清空
		//ios::ate	  打开一个已存在的文件并定位到文件末尾
	//***********打开,关闭***********//
	f1.open("file");
//	f1.open("file", mode);
	
	f1.close();
	f1.is_open();//判断是否打开文件
	//**********单字节IO操作*********//
	/*
	char ch = f2.get();
	cout << "get() " << ch << endl;
	f2.get(ch);
	cout << "get(ch) " << ch << endl;
	//向文件里写一个字符
	f2.put('z');
	
	//----不常用----//
	f2.putback('x'); //把ch放回流里
	f2.unget();		//不获得字符,指针向前移动一个字符
	ch = f2.peek();//把一个字节以整型方式返回,不从流中删除
	cout << "peek " << ch << endl;
	while((ch = f2.get()) != EOF) {
		cout << ch << ", ";
	}
	*/
	//*********多字节IO操作********//
	char buf[1024];
			//  n    ch
	f2.get(buf, 10, '\n');	//读取最多n-1个字符, 或者到ch结束, ch还保存在流中
	f2.getline(buf, 10, '\n');//ch丢弃了
	f2.read(buf, 10);//读n个字节,不考虑\0
	
	int i = f2.gcount();	//返回上次读取的字节个数
	cout << i << endl;
	f2.write(buf, 10);//写n个字符到文件里
	//----不常用----//
			//n   ch
	f2.ignore(10, ' '); //读取并忽略最多n个字符,或者是到ch结束,包括ch
	char ch;	
	//*********流随机访问***********//
	int a = f2.tellg();	//返回输入流的位置
	int b = f2.tellp(); //返回输出流的位置
	//fseek(fp, SEEK_SET); SEEK_CUR SEEK_END
	f2.seekg(12); //直接定位到据开始12的位置
//	f2.seekp(43); //
	cout << f2.tellg() << endl;
	
	f2.seekg(12, ios::beg);
	cout << f2.tellg() << endl;
	f2.seekp(12, ios::end);
			//ios::cur
			//ios::end
	cout << f2.tellg() << endl;
	int main(int argc, char* argv[]) {
		argv[1], argv[2] 	
	}
	*/
	return 0;
}

3,c++实现cp功能

/****************执行:./a.out file1 file2****************************************/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
	if (argc != 3) {
		cout << "参数错误" << endl;
		return 0;
	}
	if (strcmp(argv[2], ".") == 0) { //点表示当前路径,故不需要cp
		return 0;
	}
	if (strcmp(argv[1], argv[2]) == 0) {
		cout << "目标文件和源文件名字相同" << endl;
		return 0;
	}
	fstream f1(argv[1], ios::in);
	if (!f1.is_open()) {
		cout << "没有这个文件: " << argv[1] << endl;
		return 0;
	}
	fstream f2(argv[2], ios::out);
	
	char buf[1024];
	int len = 0; 
	//从file1读出file2写入
	while(1) {
		f1.read(buf, 1024); 
		len = f1.gcount();
		f2.write(buf, len);
		if (len < 1024)
			break;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上花开缓缓归以

你的鼓励将是我创作的最大动力,

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值