cNoteReadFile_C语言读取文件内容

 1 #include"stdio.h"
 2 /*
 3 char readScale()
 4 从文本中读出一定范围的字符到数组中
 5 文本名 范围 数组地址 
 6 由函数参数给定
 7 */
 8 /*
 9 note1<存符数组和直接传入,因为传入的是数组的首地址,如:char StorageImformation[]>
10 note2<fscanf()中的读取格式要和文本中的数据格式一致,视情而定,一般是"%c "或"%c\n">
11 note3<fscanf()中读取格式后跟的是读入的地址,一般变量a加取地址符号为&a,读取为数组元素a[i]则地址为&a[i]>
12 note4<适时用fclose(fp)关闭文本>
13 note5<在打印字符或串后加换行符更易区分多次打印>
14 */
15 char readScale(int StartPosition,int EndPosition,char TxtName[],char StorageImformation[])
16 {
17     FILE *fp1;
18     int i=1,j=0,R=0,cache;
19     if((fp1=fopen(TxtName,"r"))==NULL)
20     {
21         printf("    Warnning from readScale: Fail to open %s!\n",TxtName);
22         goto end;
23     }
24     while(i<StartPosition)
25     {
26         fscanf(fp1,"%c ",&cache);
27         i++;
28         printf("cache= %c\n",cache);
29     }
30     for(i=StartPosition;i<=EndPosition;i++)
31     {
32         fscanf(fp1,"%c ",&StorageImformation[j]);
33         j++;
34         printf("read in: %c\n",StorageImformation[j-1]);
35             
36     }
37     fclose(fp1);
38     R=1;    
39 end:return(R);
40 }
41 void main()
42 {
43     char S[50];
44     char fileName[]={"1.txt"};
45     readScale(1,16,fileName,S);
46     putchar('\n');
47     puts(S);
48     //getchar();
49 }

 .\1.txt :

1 2 3 4 5 6 7 8 9 0 a b c d e f

转载于:https://www.cnblogs.com/lzqzldz/p/6298163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值