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