【发布时间】:2012-04-05 08:15:14
【问题描述】:
/已编辑/ 我是新来的。 我有一个文本文件,内容如下:
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
谁能给我解释一下出了什么问题?
【问题讨论】:
-
我没有看到
array是在哪里声明的?