/edited/ 我是新来的。我有一个文本文件,内容如下:
6
<cr>
R 0
R 1
R 4
R 36
R 0
R 4
这就是我所拥有的。我想将每一行读入一个数组,以便我可以将该数组转换为一个整数,这样我就可以只打印我以后想要的任何一行的数字。
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
FILE *fr; /*declares file pointer*/
int i, j, num[32];
char array[32][32], input_file[32], line[32];
printf("Enter file: ");
fflush(stdin);
scanf("%s", input_file);
fr = fopen(input_file, "r");
for(i=0;i<32;i++)
for(j=0;j<32;j++){
array[i][j] = \'0';
}
for(i=0;i<32;i++){
line[i] = '\0';
}
if(fr != NULL){
while(fgets(line, sizeof(line), fr) != NULL){
strcpy(array[i],line);
num[i] = atoi(array[i]);
i++;
printf("%d\n", num[i]);
}
}fclose(fr);
else{
perror(input_file);
}
}
我没有收到任何错误,但打印的内容不正确;这是它打印的内容:
-370086
-370086
-370086
-370086
-370086
-370086
-370086
-370086
谁能向我解释出了什么问题?