C 文件操作
2020-08-07 本文已影响0人
zcwfeng
读写操作
#include "stdio.h"
void readFileTest();
void writeFileTest();
void copyTest();
//TODO 文件读写
int main(int argc,char **argv) {
// readFileTest();
// writeFileTest();
// copyTest();
printf("argc = %d\n",argc);
if(argv){
printf("argv=%c\n",argv);
}
if(argv < 3) return 0;
FILE *pr = fopen(argv[1], "r");
if(pr == NULL) return 0;
FILE *pw = fopen(argv[2], "w");
if(pw == NULL) return 0;
if (pw) {
while (1){
char c = fgetc(pr);
if(c == EOF){
break;
}
fputc(c,pw);
}
fclose(pw);
pw = NULL;
}
fclose(pr);
pr = NULL;
return 0;
}
void readFileTest() {
FILE *p = fopen("../src/test/a.txt", "r");
if (p) {
printf("success\n");
while (1) {
char c = fgetc(p);
if (c == EOF) {
break;
}
printf("%c", c);
}
fclose(p);
p = NULL;
} else {
printf("fail\n");
}
}
void writeFileTest() {
printf("success\n");
FILE *p = fopen("../src/test/a.txt", "a");
if (p) {
fputc('0',p);
fclose(p);
p = NULL;
}
}
void copyTest() {
printf("copy open success\n");
FILE *pr = fopen("../src/test/a.txt", "r");
if(pr == NULL) return;
FILE *pw = fopen("../src/test/a1.txt", "w");
if(pr == NULL) return;
if (pw) {
while (1){
char c = fgetc(pr);
if(c == EOF){
break;
}
fputc(c,pw);
}
fclose(pw);
pw = NULL;
}
fclose(pr);
pr = NULL;
}
文件加解密思路:
#include "stdio.h"
void file_opt();
void file_tpo();
int main(int argc,char **argv) {
// file_opt();
// file_tpo();
printf("加解密,0加密,1解密\n");
if(argv<4) return 0;
FILE *pr = fopen(argv[1],"r");
if(pr == NULL) return 0;
FILE *pw = fopen(argv[2],"w");
if(pw){
char key = argv[3][0];
printf("key:%d\n",key);
char c = getc(pr);
while (c!=EOF) {
if(key == '0'){
c++;
}else {
c--;
}
putc(c, pw);
c = fgetc(pr);
}
fclose(pw);
pw = NULL;
}
fclose(pr);
pr = NULL;
return 0;
}
void file_opt() {
printf("\n");
FILE *pr = fopen("../src/test/a.txt", "r");
if (pr == NULL) return;
FILE *pw = fopen("../src/test/b.txt", "w");
if (pw) {
while (1) {
char c = fgetc(pr);
if (c == EOF) {
break;
}
c++;
fputc(c, pw);
}
fclose(pw);
pw = NULL;
}
fclose(pr);
pr = NULL;
}
void file_tpo() {
printf("\n");
FILE *pr = fopen("../src/test/b.txt", "r");
if (pr == NULL) return;
FILE *pw = fopen("../src/test/c.txt", "w");
if (pw) {
while (1) {
char c = fgetc(pr);
if (c == EOF) {
break;
}
c--;
fputc(c, pw);
}
fclose(pw);
pw = NULL;
}
fclose(pr);
pr = NULL;
}
小例子
文本内容,简单的公示,算出结果,并且讲解写回去
1+2=
2*3=
4-1=
5/2=
#include "stdio.h"
#include "stdlib.h"
int calc(int a, char b, int c);
void calResult();
void calResultCall();
int main(int argc, char **argv) {
// calResult();
calResultCall();
return 0;
}
void calResultCall() {
FILE *pr = fopen("../src/test/cal.txt", "r");
if (pr) {
char * array = calloc(100, sizeof(char));
char * tmp = array;// 当前位置
int index = 0;
while(1){
if(feof(pr)) break;
char buf[100] = {0};
fgets(buf, sizeof(buf), pr);
int a = 0, c = 0;
char b = 0;
sscanf(buf,"%d%c%d=",&a,&b,&c);
int result = calc(a,b,c);
// 缓存起来
sprintf(tmp,"%d%c%d=%d\n",a,b,c,result);
array = realloc(array,100 * (index+2));
tmp = array + 100*(index + 1);
index++;
printf("%d%c%d=%d\n",a,b,c,result);
}
fclose(pr);
// 写回去
pr = fopen("../src/test/cal.txt", "w");
int i;
tmp = array;
for (i = 0; i < index; ++i) {
fputs(tmp,pr);
tmp += 100;
}
free(array);
pr = NULL;
}
}
void calResult() {
FILE *pr = fopen("../src/test/cal.txt", "r");
if (pr) {
while(1){
if(feof(pr)) break;
char buf[100] = {0};
fgets(buf, sizeof(buf), pr);
int a = 0, c = 0;
char b = 0;
sscanf(buf,"%d%c%d=",&a,&b,&c);
int result = calc(a,b,c);
printf("%d%c%d=%d\n",a,b,c,result);
}
fclose(pr);
pr = NULL;
}
}
int calc(int a, char b, int c) {
switch (b) {
case '+':
return a + c;
case '-':
return a - c;
case '*':
return a * c;
case '/':
return a / c;
}
return 0;
}
拷贝文件工具实例
#include "stdio.h"
#include "stdlib.h"
#include "sys/stat.h"
#define BLOCK_SIZE 1024 * 64//定义阈值每次最多读取
int main(int argc, char **argv) {
printf("\n");
if (argc < 3) {
return 0;
}
FILE *pr = fopen(argv[1], "rb");
if (pr == NULL) return 0;
FILE *pw = fopen(argv[2], "wb");
// 获取文件大小
struct stat st = {0};
stat(argv[1], &st);
int size = st.st_size;
printf("size= %d\n", size);
if (size > BLOCK_SIZE) {
size = BLOCK_SIZE;
}
char *buf = calloc(1, size);
unsigned int index = 0;
while (!feof(pr)) {
// 按照block读写
int res = fread(buf, 1, size, pr);
fwrite(buf, 1, res, pw);
index++;
}
free(buf);
fclose(pw);
fclose(pr);
printf("index=%d",index);
return 0;
}
-> 测试
gcc a file_demo4.c -o cp.exe
./cp.exe ../src/test/demo.mp4 ../src/test/temp.mp4
fflush 方法慎用
#include "stdio.h"
#include "stdlib.h"
int main(int argc,char **argv) {
FILE *pr = fopen("../src/test/flush.txt", "w");
if (pr) {
while(1){
printf("please input:\n");
char buf[100] = {0};
scanf("%s",buf);
if(strcmp(buf,"exit") == 0) {
break;
}
fputs(buf,pr);
// fflush(pr);// 除非及时保存一般不用
}
fclose(pr);
pr = NULL;
}
return 0;
}