文本文件的增删查改

 1/* 增 */

#define _CRT_SECURE_NO_WARNINGS // 关闭安全检查 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 void main() 6 { 7 char path[100] = "C:\\Users\\yakun\\Desktop\\10.txt"; // 文件路径 8 char temppath[100] = { 0 }; // 临时文件的路径 9 10 FILE *pf = fopen(path, "r"); // 根据path路径,按照读的方式打开文件 11 if (pf == NULL) 12 { 13 printf("文件打开失败\n"); 14 return; 15 } 16 // tmpnam()函数创建一个独特的文件名并保存在name中 17 tmpnam(temppath); // 创建一个临时文件 18 19 FILE *ptemp = fopen(temppath, "w"); 20 if (ptemp == NULL) 21 { 22 printf("临时文件创建失败\n"); 23 return; 24 } 25 char str[120]; 26 char findstr[30] = "西班牙人"; 27 28 // 从原来的文件读取字符串,按行读取,能读就一直读下去 29 while (fgets(str, 120, pf)) 30 { 31 char *p = strstr(str, findstr); // 从str里面查找findstr 32 33 if (p == NULL)// 说明没有找到 34 { 35 fputs(str, ptemp);// 正常写入临时文件 36 } 37 else 38 { 39 // 找到要修改的字符串,把修改好的字符串填充上去 40 fputs("火辣辣的情歌,火辣辣的妹子吆\n", ptemp); 41 fputs(str, ptemp); 42 } 43 } 44 45 46 fclose(pf); // 关闭文件 47 fclose(ptemp); 48 49 remove(path);// 删除原来的文件 50 51 rename(temppath, path);// 将临时文件命名为原来的文件
 1 /**/
 2 
 3 #define _CRT_SECURE_NO_WARNINGS    // 关闭安全检查
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <string.h>
 7 
 8 // 删除步骤:新建一个临时文件,挨个读取字符串,然后写入;遇到要删除
 9 //的,就不写入;然后删除原来的文件,将临时文件重命名(可以处理大文件)
10 void main()
11 {
12     char path[100] = "C:\\Users\\yakun\\Desktop\\10.txt";    // 文件路径
13     char temppath[100] = { 0 };    // 临时文件的路径
14 
15     FILE *pf = fopen(path, "r"); // 根据path路径,按照读的方式打开文件
16     if (pf == NULL)
17     {
18         printf("文件打开失败\n");
19         return;
20     }
21     // tmpnam()函数创建一个独特的文件名并保存在name中
22     tmpnam(temppath);    // 创建一个临时文件
23 
24     FILE *ptemp = fopen(temppath, "w");
25     if (ptemp == NULL)
26     {
27         printf("临时文件创建失败\n");
28         return;
29     }
30     char str[120];
31     char findstr[30] = "欲射不能";
32 
33     // 从原来的文件读取字符串,按行读取,能读就一直读下去
34     while (fgets(str, 120, pf))
35     {
36         char *p = strstr(str, findstr); // 从str里面查找findstr
37 
38         if (p == NULL)// 说明没有找到
39         {
40             fputs(str, ptemp);// 正常写入临时文件
41         }
42         else 
43         {
44         // 否则,找到要删除的字符串,就不需要写入
45             
46         }
47     }
48 
49     
50     fclose(pf);    // 关闭文件
51     fclose(ptemp);
52     
53     remove(path);// 删除原来的文件
54     
55     rename(temppath, path);// 将临时文件命名为原来的文件
56     
 1 /**/
 2 
 3 #define _CRT_SECURE_NO_WARNINGS    // 关闭安全检查
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <string.h>
 7 
 8 void main()
 9 {
10     char path[100] = "C:\\Users\\yakun\\Desktop\\10.txt";    // 文件路径
11     FILE *pf = fopen(path, "r"); // 根据path路径,按照读的方式打开文件
12     if (pf == NULL)
13     {
14         printf("文件打开失败\n");
15     }
16     else
17     {
18         // 文件打开成功
19         char str[100];
20         char findstr[50] = "你好"; //需要查找的内容
21         // 当fgets的返回值为0,就到了文件末尾,能读就一直读下去
22         while (fgets(str, 100, pf)) // 从文件流读取字符串,最大长度为100
23         {
24             // 查询
25             char *p = strstr(str, findstr);
26 
27             if (p != NULL)
28             {
29                 printf("找到                %s", str);    // 屏幕输出字符串
30             }
31             else
32             {
33                 printf("%s", str);
34             }
35         }
36     }
37     
38     fclose(pf);    // 关闭文件
39 
40     system("pause");
41 }
 1 /**/
 2 
 3 #define _CRT_SECURE_NO_WARNINGS    // 关闭安全检查
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <string.h>
 7 void main()
 8 {
 9     char path[100] = "C:\\Users\\yakun\\Desktop\\10.txt";    // 文件路径
10     char temppath[100] = { 0 };    // 临时文件的路径
11 
12     FILE *pf = fopen(path, "r"); // 根据path路径,按照读的方式打开文件
13     if (pf == NULL)
14     {
15         printf("文件打开失败\n");
16         return;
17     }
18     // tmpnam()函数创建一个独特的文件名并保存在name中
19     tmpnam(temppath);    // 创建一个临时文件
20 
21     FILE *ptemp = fopen(temppath, "w");
22     if (ptemp == NULL)
23     {
24         printf("临时文件创建失败\n");
25         return;
26     }
27     char str[120];
28     char findstr[30] = "一群羊";
29 
30     // 从原来的文件读取字符串,按行读取,能读就一直读下去
31     while (fgets(str, 120, pf))
32     {
33         char *p = strstr(str, findstr); // 从str里面查找findstr
34 
35         if (p == NULL)// 说明没有找到
36         {
37             fputs(str, ptemp);// 正常写入临时文件
38         }
39         else
40         {
41             // 找到要修改的字符串,把修改好的字符串填充上去
42             fputs("原来是你,大叔,你的节操掉了\n", ptemp);
43 
44         }
45     }
46 
47 
48     fclose(pf);    // 关闭文件
49     fclose(ptemp);
50 
51     remove(path);// 删除原来的文件
52 
53     rename(temppath, path);// 将临时文件命名为原来的文件
 1 /* 文件中检索字符串 */
 2 
 3 #define _CRT_SECURE_NO_WARNINGS  // 关闭安全检查
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 
 7 void main()
 8 {
 9     char str[30] = "ptemp";    // 要查找的字符串
10     char path[100] = "C:\\Users\\yakun\\Desktop\\1.c";
11 
12     char CMD[150];
13     sprintf(CMD, "find  /n %s %s", str, path);//格式化搜索指令
14     
15     system("CMD");// 执行命令
16 
17 
18     system("pause");
19 }

 

posted @ 2018-03-09 14:06  NoThx  阅读(239)  评论(0编辑  收藏  举报