第一种方法:
1.网上流行的一个方法: 打印出每行的文本信息。
01 | #include <stdio.h> |
02 | #include <stdlib.h> |
03 |
04 | int main( void ) |
05 | { |
06 | FILE * fp; |
07 | char * line = NULL; |
08 | size_t len = 0; |
09 | ssize_t read; |
10 |
11 | fp = fopen ( "test.txt" , "r" ); |
12 | if (fp == NULL) |
13 | exit (EXIT_FAILURE); |
14 |
15 | while ((read = getline(&line, &len, fp)) != -1) { |
16 | printf ( "Retrieved line of length %zu :\n" , read); |
17 | printf ( "%s" , line); |
18 | } |
19 | |
20 | if (line) |
21 | free (line); |
22 | exit (EXIT_SUCCESS); |
23 | } |
01 | #include <stdio.h> |
02 | #include <stdlib.h> |
03 |
04 | int get_file_line( char *pInputName, char *pOutputBuf, int cnt) |
05 | { |
06 | FILE * fp; |
07 | int i=0; |
08 | char * line = NULL; |
09 | size_t len = 0; |
10 | ssize_t read; |
11 |
12 | fp = fopen (pInputName, "r" ); |
13 | if (fp == NULL) |
14 | return -1; |
15 |
16 | if (cnt<=0) |
17 | return -2; |
18 | |
19 | while ((read = getline(&line, &len, fp)) != -1) { |
20 | ++i; |
21 | if (i>=cnt) |
22 | break ; |
23 | } |
24 |
25 | if (line) |
26 | { |
27 | memcpy (pOutputBuf,line, strlen (line)); |
28 | free (line); |
29 | return 0; |
30 | } |
31 |
32 | return -3; |
33 | } |
34 |
35 | int main( void ) |
36 | { |
37 | char name[20]= "test.txt" ; |
38 | char buf[256]={0}; |
39 | int j; |
40 | for (j=1;j<8;++j) |
41 | { |
42 | memset (buf,0,256); |
43 | get_file_line(name,buf,j); |
44 | printf ( "[%d] == %s" ,j,buf); |
45 | } |
46 | printf ( "\n" ); |
47 | |
48 | } |
在此处做一个小小的拓展,就是在每行的文本信息中提取我们想要的信息:
01 | #define dIndexName "WWW" |
02 |
03 | #define NAME_LEN_MAX 7 |
04 |
05 | void GetName( void ) |
06 | { |
07 | int i; |
08 | |
09 | char *p=NULL; |
10 | char line[30]={0}; |
11 | char dest_name[20]={0}; |
12 | char pFileName[256]= "test.txt" ; |
13 | |
14 | get_file_line(pFileName,line,1); |
15 |
16 |
17 | printf ( "line === %s \n" ,line); |
18 | if ((p= strstr (line,dIndexName))&&(p= strchr (line, ':' ))) |
19 | { |
20 | memcpy (dest_name,p+1,NAME_LEN_MAX); |
21 | } |
22 |
23 |
24 | |
25 | for (i=0;i<NAME_LEN_MAX;++i) |
26 | { |
27 | printf ( "[%d] === %c \n" ,i,dest_name[i]); |
28 | } |
29 | } |
执行结果,如下所示:
第二种方法: 01 | #include <stdio.h> |
02 | #include <stdlib.h> |
03 | #include <sys/types.h> |
04 | #include <sys/stat.h> |
05 | #include <fcntl.h> |
06 |
07 | typedef unsigned char u8; |
08 |
09 | int get_file_line( char *pInputName, int line) |
10 | { |
11 | char pTempString[256]; |
12 |
13 | u8 *pTempBuffer; |
14 | int tTempFile,tTempFileLen; |
15 |
16 | int i,m,n; |
17 | u8 tTempFlag,tTempCmdFlag; |
18 | { |
19 | tTempFile=open(pInputName,O_RDONLY); |
20 | if (tTempFile>=0) |
21 | { |
22 | tTempFileLen=lseek(tTempFile,0,SEEK_END); |
23 | lseek(tTempFile,0,SEEK_SET); |
24 | tTempCmdFlag=1; |
25 | if ( NULL!=(pTempBuffer=(u8 *) malloc (tTempFileLen)) ) |
26 | { |
27 | if ( tTempFileLen==read(tTempFile,pTempBuffer,tTempFileLen) ) |
28 | { |
29 | m=n=0; |
30 | tTempFlag=0; |
31 | while (n<tTempFileLen) |
32 | { |
33 | if ( tTempFlag ) |
34 | { |
35 | if ( pTempBuffer[n]<0x20) |
36 | { |
37 | if ( tTempFlag ) |
38 | { |
39 | if ( n>m ) |
40 | { |
41 | memcpy (pTempString,pTempBuffer+m,n-m); |
42 | pTempString[n-m]=0; |
43 | printf ( "String1 %s \n" ,pTempString); |
44 | } |
45 | tTempFlag=0; |
46 | } |
47 | } |
48 | } else if ( pTempBuffer[n]>=0x20 ) |
49 | { |
50 | m=n; |
51 | tTempFlag=1; |
52 | } |
53 | n++; |
54 | } |
55 | if ( (n>m)&&(tTempFlag) ) |
56 | { |
57 | memcpy (pTempString,pTempBuffer+m,n-m); |
58 | pTempString[n-m]=0; |
59 | printf ( "String2 %s \n" ,pTempString); |
60 | } |
61 | tTempCmdFlag=0; |
62 | } |
63 | free (pTempBuffer); |
64 | } |
65 | close(tTempFile); |
66 | return tTempCmdFlag; |
67 | } |
68 | } |
69 | return 2; |
70 | } |
71 |
72 | void main( void ) |
73 | { |
74 | char name[20]= "tmp.txt" ; |
75 | get_file_line(name,0); |
76 | |
77 | } |