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;  //为了增加'\0',字符串的结束符
24     
25     content = (char *) malloc(longArr);
26     
27     fread(content,longContent,1,fp);
28     
29     //因为数组的下标示从0开始的,所以最后一个下标应该是数组长度-1,也就是文本的长度 
30     content[longContent] = '\0';
31 
32     
33     fclose(fp);
34 
35     puts(content);
36     
37     free(content);
38 }

 

转载于:https://www.cnblogs.com/hanyouchun/p/4177911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值