C语言读取.txt文本每行字符十进制数据,转int型十进制
先给出需要读取的文本数据:
给出实现代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<stdlib.h> 5 6 int main() 7 { 8 char c_number; 9 int vector_num = 0; 10 int vector_value = 0; 11 int flag_negative = 0; 12 13 14 int _person1[128] = {0}; 15 FILE *fp; //定义所读文件的FILE 16 17 if((fp = fopen("out1.txt","r")) == NULL) 18 { 19 perror("fail to read"); 20 exit(1); 21 } 22 23 while((c_number=getc(fp))!=EOF) 24 { 25 26 if(c_number=='\n') 27 { 28 29 if(flag_negative != 1) 30 { 31 ; 32 } 33 else 34 { 35 vector_value = -vector_value; 36 } 37 _person1[vector_num++] = vector_value; 38 39 40 printf("vector_num = %d,vector_value = %d\r\n",vector_num,_person1[vector_num-1]); 41 42 flag_negative = 0; 43 vector_value = 0; 44 } 45 else 46 { 47 if(c_number != '-') 48 { 49 vector_value *= 10; 50 vector_value += (c_number - '0'); 51 52 } 53 else 54 { 55 flag_negative = 1; 56 } 57 58 } 59 60 61 } 62 63 return 0; 64 }
实现结果
天空飘来五个字,那都不是事