gpt4 book ai didi

正确地将文件内容按行存储到数组中,稍后打印数组内容

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:29 25 4
gpt4 key购买 nike

我在读取数组内容时遇到了一些问题。我不确定我是否正确存储了它,因为每一行的结果都是“1304056712”。

#include <stdio.h>
#include <stdlib.h>

#define INPUT "Input1.dat"

int main(int argc, char **argv) {
int data_index, char_index;
int file_data[1000];

FILE *file;
int line[5];
file = fopen(INPUT, "r");

if(file) {
data_index = 0;

while(fgets(line, sizeof line, file) != NULL) {
//printf("%s", line); ////// the line seems to be ok here
file_data[data_index++] = line;
}

fclose(file);
}

int j;
for(j = 0; j < data_index; j++) {
printf("%i\n", file_data[j]); // when i display data here, i get '1304056712'
}

return 0;
}

最佳答案

我想你需要说些类似的话

file_data[data_index++] = atoi(line);

关于正确地将文件内容按行存储到数组中,稍后打印数组内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com