gpt4 book ai didi

c - 如何将文件的编号存储在整数数组中

转载 作者:行者123 更新时间:2023-11-30 16:42:04 24 4
gpt4 key购买 nike

我需要读取一个包含数字的文件,然后将这些数字存储在一个数组中,之后我需要删除数组中出现的重复数字并订阅该文件。问题是我什至无法将文件上的数字放入整数数组中,我调试了代码并且文件确实打开了,但同时无法将数字存储在数组中。

代码:

 #include <stdio.h>

int main(void) {
int c;
int radica[50];
int i=0;

// open file
FILE *myFile = fopen("input.txt","r");//for ideone//fopen("input.txt", "r");
// if opening file fails, print error message and exit 1
if (myFile == NULL) {
perror("Error: Failed to open file.");
return 1;
}
rewind(myFile);

do{
fscanf(myFile,"%1d",&radica[i]); //Storing the number into the array
i++;
}while(feof(myFile));

// close file
fclose(myFile);
//printing the numbers
for(int j = 0; j < i; j++){
printf("%d\n", radica[j]);
}
return 0;
}

文件包含:1 2 3 4 5 6 7 5 8 8 6 3 4 5 6 6 7 7 8 8

最佳答案

现在可以使用了,谢谢大家!

#include <stdio.h>

int main(void) {
int c;
int radica[50];
int i=0;

// open file
FILE *myFile = fopen("input.txt","r");//for ideone//fopen("input.txt", "r");
// if opening file fails, print error message and exit 1
if (myFile == NULL) {
perror("Error: Failed to open file.");
return 1;
}

do{
fscanf(myFile,"%1d",&radica[i]); //Storing the number into the array
i++;
}while(c=getc(myFile)!=EOF);

// close file
fclose(myFile);
//printing the numbers
for(int j = 0; j < i; j++){
printf("%d\n", radica[j]);
}
return 0;
}

关于c - 如何将文件的编号存储在整数数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46043871/

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