0

I need to read a file with numbers and then store this numbers in a array, after that I need to remove the repeated numbers presenting in the array and subscribe the file. The problem is I can't even put the numbers on the file in a array of integers, I debbugged the code and the file is really opened, but the while don't work to store the numbers in the array.

The code:

 #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;
}

the file contains: 1 2 3 4 5 6 7 5 8 8 6 3 4 5 6 6 7 7 8 8

Rafael Camara
  • 33
  • 1
  • 6

1 Answers1

0

Now its working, Thank you guys!

#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;
}
Rafael Camara
  • 33
  • 1
  • 6