NDK05_C语言文件读写

NDK开发汇总

一 读文件

int test5() {
	//自己定义的文件地址
	char *path = (char*)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\friends.txt";

	FILE *fp = fopen(path, "r");

	char buff[500];
	while (fgets(buff, 50, fp)) {
		printf("%s", buff);
	}

	fclose(fp);
	system("pause");

	system("pause");
	return 0;
}

二 写文件

int test5() {
	char *path =(char *) "C:\\Users\\PF0ZYBAJ\\Desktop\\files\\write.txt";
	FILE *fp = fopen(path, "w");

	if (fp == NULL) {
		printf("failed。。。。");
		return 0;
	}
	char *text = (char *)"这是我写的文件内容";
	fputs(text, fp);
	fclose(fp);

	return 0;
}

三 读写二进制文件

/////////////////////////////读写二进制文件////////////////
int test5() {
	char * read_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\LogViewPro.exe";
	char * write_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\LogViewPro_w.exe";

	FILE * read_fp = fopen(read_path, "rb");
	FILE * write_fp = fopen(write_path, "wb");

	char buff[50];
	int len = 0;

	while ((len = fread(buff, sizeof(char), 50, read_fp))) {
		fwrite(buff, sizeof(char), len, write_fp);
	}

	fclose(read_fp);
	fclose(write_fp);
	system("pause");
	return 0;
}

四 获取文件大小

int test5() {
	char * read_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\liuyan.png";
	
	FILE * fp = fopen(read_path, "r");
	if (fp == NULL) {
		return 0;
	}

	fseek(fp, 0, SEEK_END);
	long filesize = ftell(fp);

	printf("%ld \n", filesize);

	system("pause");
	return 0;
}

五 Demo

lsn05.cpp

发布了269 篇原创文章 · 获赞 123 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/baopengjian/article/details/104762681
今日推荐