c 读取文本

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define max 10
 5 #define min 2
 6 
 7 void main(int argc,char *argv[])
 8 {
 9     char *content;
10     int longContent=0;
11     int longArr;
12     FILE *fp;
13     if( (fp=fopen("a.txt","r+")) ==NULL )
14     {
15        puts("open error");
16        exit(1);
17     }
18     //文本的大小 = 最后的字节数 
19     fseek(fp,0,SEEK_END);
20     longContent = ftell(fp);
21     rewind(fp);//定位到文本开始 
22     
23     longArr = longContent+1;  //为了增加'',字符串的结束符
24     
25     content = (char *) malloc(longArr);
26     
27     fread(content,longContent,1,fp);
28     
29     //因为数组的下标示从0开始的,所以最后一个下标应该是数组长度-1,也就是文本的长度 
30     content[longContent] = '';
31 
32     
33     fclose(fp);
34 
35     puts(content);
36     
37     free(content);
38 }
原文地址:https://www.cnblogs.com/hanyouchun/p/4177911.html