zl程序教程

您现在的位置是:首页 >  后端

当前栏目

数据文件——之将文本文件定位到所需字符

定位字符 数据文件 文本文件
2023-09-11 14:22:20 时间

代码:

 1 //This is c program code!                                                                                           
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_20.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 6   * 创建时间: *** :2020年11月22日的下午01:12
 7   * 文档用途: *** :数据结构与算法分析-c语言描述
 8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
 9   * 修订时间: *** :2020年第46周 11月22日 星期日 下午01:12 (第327天)
10   * 文件描述: *** :自行添加
11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
12 #include <stdio.h>
13 
14 void closeState(FILE *fPtr){
15     int clo = fclose(fPtr);
16     if(clo == -1){
17         puts("file-closing failed.");
18     }
19     if(clo == 0){
20         puts("file-closing successfully.");
21     }
22 }
23 
24 int  main(int argc, char **argv)
25 {
26     FILE *fPtr = fopen("pune.txt", "r");
27     if(fPtr != NULL){
28         puts("File pune.txt opened successfully.");
29         
30         puts("Let loc denotes current file position.");
31         int loc = ftell(fPtr);
32         printf("Now value of loc is %d\n", loc);
33 
34         printf("Les us read a single character and it is: ");
35         int ch = fgetc(fPtr);
36         putchar(ch);
37         printf("\n");
38 
39         loc = ftell(fPtr);
40         printf("Now value of loc is %d\n", loc);
41 
42         fseek(fPtr, 8, 0);
43         puts("Statement \" fseek(fptr, 8, 0);\" executed");
44         loc = ftell(fPtr);
45         printf("Now value of loc is %d\n", loc);
46 
47         fseek(fPtr, 3, 1);
48         puts("Statement \" fseek(fptr, 3, 1);\" executed");
49         loc = ftell(fPtr);
50         printf("Now value of loc is %d\n", loc);
51 
52         fseek(fPtr, -5, 1);
53         puts("Statement \" fseek(fptr, -5, 1);\" executed");
54         loc = ftell(fPtr);
55         printf("Now value of loc is %d\n", loc);
56 
57         fseek(fPtr, -3, 2);
58         puts("Statement \" fseek(fptr, -3, 2);\" executed");
59         loc = ftell(fPtr);
60         printf("Now value of loc is %d\n", loc);
61 
62         fseek(fPtr, 0, 2);
63         puts("Statement \" fseek(fptr, 0, 2);\" executed");
64         loc = ftell(fPtr);
65         printf("Now value of loc is %d\n", loc);
66 
67         puts("Now let us perform a read operation.");
68         ch = fgetc(fPtr);
69         printf("Value read is %d\n", ch);
70         loc = ftell(fPtr);
71         printf("Now value of loc is still %d\n", loc);
72 
73         fseek(fPtr, 0, 0);
74         puts("Statement \" fseek(fptr, 0, 0);\" executed");
75         loc = ftell(fPtr);
76         printf("Now value of loc is %d\n", loc);
77         puts("That's all.");
78 
79         closeState(fPtr);
80     }else{
81         puts("File-opening failed.");
82     }
83 
84     return 0;
85 }