切割与合并和增删改查
1.对文件的切割与合并
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> char path[1024] = "C:\\Users\\yincheng01\\Desktop\\test\\切割与合并\\Q.jpg"; //C:\\Users\\yincheng01\\Desktop\\test\\切割与合并\\Q1.exe ////C:\\Users\\yincheng01\\Desktop\\test\\切割与合并\\Q2.exe int getfilesize(char *path) { FILE *pf = fopen(path, "rb"); if (pf==NULL) { return -1; } else { fseek(pf, 0, SEEK_END);//移动尾部 int size = ftell(pf);//当前指针距离文件开头的长度 fclose(pf);//获取文件大小 return size; } } void space(char *path, int n) { char **pathes; pathes = malloc(sizeof(char*)*n);//开辟指针数组 for (int i = 0; i < n; i++) { pathes[i] = malloc(sizeof(char) * 1024); sprintf(pathes[i], "C:\\Users\\yincheng01\\Desktop\\test\\切割与合并\\Q%d.jpg", i + 1); //printf("\n%s", pathes[i]);//显示路径 } FILE *pf = fopen(path, "rb"); if (pf == NULL) { return ; } else { int size = getfilesize(path);//获取文件大小 //100 10 10 //100 8 100/7 14×7+2=100 if (size%n==0) { for (int i = 0; i < n;i++) { FILE *pfw = fopen(pathes[i], "wb");//写入 for (int j = 0; j < size / n;j++) { fputc(fgetc(pf), pfw);//读取一个字符,写入 } fclose(pfw); } } else { for (int i = 0; i < n - 1;i++) { FILE *pfw = fopen(pathes[i], "wb");//写入 for (int j = 0; j < size / (n- 1);j++) { fputc(fgetc(pf), pfw);//读取一个字符,写入 } fclose(pfw); } { FILE *pfw = fopen(pathes[n-1], "wb");//写入 for (int i = 0; i < size%n;i++) { fputc(fgetc(pf), pfw);//读取一个字符,写入 } fclose(pfw); } } fclose(pf); } } void merge(char *newpath, int n) { char **pathes; pathes = malloc(sizeof(char*)*n);//开辟指针数组 for (int i = 0; i < n; i++) { pathes[i] = malloc(sizeof(char) * 1024); sprintf(pathes[i], "C:\\Users\\yincheng01\\Desktop\\test\\切割与合并\\Q%d.jpg", i + 1); //printf("\n%s", pathes[i]);//显示路径 } FILE *pfw = fopen(newpath, "wb"); for (int i = 0; i < n;i++) { int length = getfilesize(pathes[i]); if (length==-1) { return; } else { FILE *pfr = fopen(pathes[i], "rb"); for (int j = 0; j < length;j++) { fputc(fgetc(pfr), pfw);//写入 } fclose(pfr); } } fclose(pfw); } void main() { int num; scanf("%d", &num);//分割为num块 space(path, num); merge("C:\\Users\\yincheng01\\Desktop\\test\\切割与合并\\MMMM.jpg", num); system("pause"); }
2.文本文件的增删改查
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> void main查询() { FILE *pfr = fopen("C:\\Users\\yincheng01\\Desktop\\code\\10.txt", "r"); FILE *pfw = fopen("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "w"); while (!feof(pfr)) { char str[128] = { 0 }; fgets(str, 128, pfr);//读取 char *p = strstr(str, "王鹏飞"); if (p!=NULL) { printf("%s", str); } } fclose(pfr); fclose(pfw); system("pause"); } void main修改() { FILE *pfr = fopen("C:\\Users\\yincheng01\\Desktop\\code\\10.txt", "r"); FILE *pfw = fopen("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "w"); while (!feof(pfr)) { char str[128] = { 0 }; fgets(str, 128, pfr);//读取 char *p = strstr(str, "王鹏飞"); if (p != NULL) { printf("%s", str); fputs( "上联:六问赵学辉有多愁. 下联:恰似跑堂杂役在青楼. 横批: 看别人射\n" , pfw); } else { fputs(str, pfw); } } fclose(pfr); fclose(pfw); remove("C:\\Users\\yincheng01\\Desktop\\code\\10.txt");//删除 rename("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "C:\\Users\\yincheng01\\Desktop\\code\\10.txt");//重命名 system("pause"); } void main删除() { FILE *pfr = fopen("C:\\Users\\yincheng01\\Desktop\\code\\10.txt", "r"); FILE *pfw = fopen("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "w"); while (!feof(pfr)) { char str[128] = { 0 }; fgets(str, 128, pfr);//读取 char *p = strstr(str, "赵学辉"); if (p != NULL) { } else { fputs(str, pfw); } } fclose(pfr); fclose(pfw); remove("C:\\Users\\yincheng01\\Desktop\\code\\10.txt");//删除 rename("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "C:\\Users\\yincheng01\\Desktop\\code\\10.txt");//重命名 system("pause"); } void main() { FILE *pfr = fopen("C:\\Users\\yincheng01\\Desktop\\code\\10.txt", "r"); FILE *pfw = fopen("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "w"); while (!feof(pfr)) { char str[128] = { 0 }; fgets(str, 128, pfr);//读取 char *p = strstr(str, "八问中国男足有多愁"); if (p != NULL) { //fputs("上联:十问王鹏飞几多愁. 下联:恰似一群凤姐上青楼. 横批:只能用手\n", pfw); fputs(str, pfw); fputs("上联:二问中国男足有多愁. 下联:恰似王鹏飞守青楼. 横联: 总是被射\n", pfw); } else { fputs(str, pfw); } } fclose(pfr); fclose(pfw); remove("C:\\Users\\yincheng01\\Desktop\\code\\10.txt");//删除 rename("C:\\Users\\yincheng01\\Desktop\\code\\temp.txt", "C:\\Users\\yincheng01\\Desktop\\code\\10.txt");//重命名 system("pause");
人怂胆子小,手拿大刀跑。