我需要读取带有数字的文件,然后将这些数字存储在一个数组中,之后我需要删除数组中出现的重复数字并订阅该文件。问题是我甚至无法将数字放在整数数组中的文件中,我对代码进行了调试,文件确实打开了,但是这些数据不能用于存储数组中的数字。
代码:
#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
答案 0 :(得分:0)
现在它的工作,谢谢你们!
#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;
}