0_7 标准I/O、文件I/O

C

#include "hjcommon.hpp"
#include "unistd.h"

HJ_NS_BEGIN

bool isFileExists(const char *path)
{
	return access(path, 0)==0;
}

long getFileLen(const char *path)
{
	if (!isFileExists(path))
	{
		hj_logln("%s is not exist.", path);
		return -1;
	}
	// 单独用w,便是打开可写文件,只允许写入,如果文件不存在则创建(不会创建父目录,如果父目录不存在,则文件fopen失败),如果存在文件清为0,r为只可读
	// "a+"附加方式打开可读写的文件,文件不存在会创建(不会创建父目录,如果父目录不存在,则文件fopen失败),
	//		若文件存在,数据会被嫁到文件尾后,原先内容会保留(原来的EOF符不保留,单独a,EOF会保留)
	// fgets(buf, 10, fp) 读取一行,如果一行大于等于10个字符,那么只会读取该行9个字符,第10个字符会设为\0;若小于10,buf中读到一行的\n后的字符会设为\0
	// fputs("a line\n", fp) 写入一行
	FILE *fp = fopen(path, "r");
	fseek(fp, 0, SEEK_END); // 文件内部指针指向文件末尾,并偏移0个位置
	long len = ftell(fp); // 获取文件内部指针位置,单位字节

	fpos_t len2;
	fgetpos(fp, &len2); // ftell获取的文件长度最长为long,如果文件长度超过了long的最大值,可以使用 fgetpos 函数获取
	hj_logln("%s 文件长度为 : %ld 个字节, fgetpos=%ld", path, len, len2);
	rewind(fp); // 让文件内部指针回到起始位置

	fclose(fp);
	return len;
}

bool copyFile(const char* inputFile, const char* outputFile)
{
	if (!isFileExists(inputFile))
	{
		hj_logln("%s is not exist.", inputFile);
		return false;
	}

	FILE* pOutput = fopen(outputFile, "w");
	if (!pOutput)
	{
		hj_logln("%s 的父目录不存在.", outputFile);
		return false;
	}
	FILE* pInput = fopen(inputFile, "r");

	// 方式一 一次性读写入
	long inputLen = getFileLen(inputFile);
	char *buf = (char*) malloc(inputLen);
	// size_t fread(void *buffer, size_t size, size_t count, FILE *); 每次读取 size 个字节,总共读 count 次,返回值为实际读取了多少次
	int read = fread(buf, inputLen, 1, pInput);
	hj_logln("read = %d", read); // read = 1
	// size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream); 每次写入 size 个字节,总共写 count 次,返回值为实际写入的次数
	int write = fwrite(buf, inputLen, 1, pOutput); // fwrite 是以二进制的形式写入
	hj_logln("write = %d", read); // write = 1
	free(buf);

	// 方式二 分段读写入
//	int step = 1024 * 1024;
//	char *buffer = (char*) malloc(step);
//	while(!feof(pInput))
//	{
//		int re = fread(buffer, 1, step, pInput);
//		if (re>0)
//			fwrite(buffer, 1, re, pOutput);
//	}
//	free(buffer);

	fclose(pInput);
	fclose(pOutput);
	return true;
}

// 序列化
bool serialize(const char *path, void *data, int len)
{
	FILE* fp = fopen(path, "w");
	if (!fp)
	{
		hj_logln("%s 的父目录不存在.", path);
		return false;
	}
	fwrite(data, len, 1, fp);
	fclose(fp);
	return true;
}

// 反序列化
bool getSerialize(const char *path, void *buf, int len)
{
	if (!isFileExists(path))
	{
		hj_logln("%s is not exist.", path);
		return false;
	}
	FILE* fp = fopen(path, "r");
	fread(buf, len, 1, fp);
	fclose(fp);
	return true;
}

HJ_NS_END

HJ_NS_USING

typedef struct { int age; char name[128]; } Person;

int main(int argc, char *argv[])
{
	// 测试文字符串长度
	char en[] = "five";
	char zh[] = "三个字";
	// en.sizeof=5, en.len=4, zh.sizeof=10, zh.len=9    字符数组的 sizeof 会算上 \0 ,字符串的 strlen 不会算上 \0, linux中一个汉字占三个字节
	hj_logln("en.sizeof=%d, en.len=%d, zh.sizeof=%d, zh.len=%d", sizeof en, strlen(en), sizeof(zh), strlen(zh));

	// 文件拷贝
	bool is = copyFile("./res/images/whisper.jpg", "./output/images/whisper.jpg");
	hj_logln("copyFile=%d", is);

	// 序列化
	Person in = { 28, "some黄" };
	const char *sPath = "./output/data/serialize.data";
	is = serialize(sPath, (void*)&in, sizeof(Person));
	if (is)
	{
		Person out;
		getSerialize(sPath, (void*)&out, sizeof(Person));
		hj_logln("in.name=%s, out.name=%s", in.name, out.name);
	}
	return 0;
}

C++

cout
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
cin
在这里插入图片描述

文件I/O
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
ios::beg // 文件起始位置
ios::cur // 当前位置
ios::end // 文件末尾

#include "hjcommon.hpp"
#include <fstream>
#include <strstream> // 字符串流

using namespace std;

HJ_NS_BEGIN

void str_stream()
{
	// 字符串流是对内存的操作,不是文件
	char buf[128] = { 0 };
	char in[24] = "istrstream";
	istrstream strin(in, 20); // 只以 in 中的前 20 个字节作为输入
	strin>>buf; // read 到 buf
	hj_logln("istrstream=%s", buf); // istrstream=istrstream

	ostrstream strout(buf, 16, ios::out); // 只使用 buf 的前 16 个字节
	strout<<123;
	hj_logln("ostrstream=%s", buf); // ostrstream=123rstream
}

long getFileLen(String path)
{
	ifstream input;
	input.open(path, ios::in); // open 返回值数据类型为 void
	if (!input)
	{
		hj_logln("%s is not exists.", path.c_str());
		return -1;
	}
	input.seekg(0, ios::end); // g 系列的函数对应输入, p 系列的函数对应输出
	long len = input.tellg();
	hj_logln("%s 文件长度为 : %ld 个字节", path.c_str(), len);
	input.close();

	return len;
}

bool copyFile(String inputFile, String outputFile)
{
	ifstream input(inputFile, ios::in);
	if (!input)
	{
		hj_logln("%s is not exists.", inputFile.c_str());
		return false;
	}
	ofstream output(outputFile, ios::out);
	if (!output)
	{
		hj_logln("%s 的父目录不存在.", outputFile.c_str());
		input.close();
		return false;
	}

	// 方式一 一次性读写入
	long len = getFileLen(inputFile);
	char *buf = new char[len];
	hj_logln("gcount()=%ld", input.gcount()); // gcount()=0
	input.read(buf, len);
	// gcount 返回输入最后一次读取的字节数
	long read = input.gcount(); // gcount()=138171  文件长度
	hj_logln("gcount()=%ld", read);
	output.write(buf, len);
	delete buf;

	// 方式二 分段读写入
//	int step = 1024 * 1024;
//	char *buffer = new char[step];
//	while (!input.eof())
//	{
//		input.read(buffer, step);
//		long ret = input.gcount();
//		hj_log("%ld, ", ret);
//		if (ret>0)
//			output.write(buffer, ret);
//	}
//	delete buffer;

	input.close();
	output.close();
	return true;
}

HJ_NS_END

HJ_NS_USING

int main(int argc, char *argv[])
{
	str_stream(); // 字符串流

	// 文件拷贝
//	bool is = copyFile("./output/txt/input.txt", "./output/txt/output.txt");
	bool is = copyFile("./res/images/whisper.jpg", "./output/images/whisper.jpg");
	hj_logln("is=%d", is);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值