As title states, I'm trying to read numbers from an external text file and assign them to an array. I'm able to manipulate the array and write the result to another file if I manually create the array, but as is, the `fscanf()` is pulling garbage from the text file. This is what I currently have, I've been reading other posts here and I've changed it like 12 times now but I think I'm getting farther from the solution. 

```
filename = argv[1];
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
        printf("\n     Unable to open input file.\n\n");
        helpmssg();
        exitmssg();
}
while (getc(fp) != EOF){
        if (fscanf(fp,"%d", &c) == 1) {
                arr[i] = c;
                i++;
        }
}

```

Ideally this should scan the file specified as a command line argument for a number, assign it to the first spot of the array, and repeat until the end of file. Any help is greatly appreciated.

Text file: 

```
12
23
99
66
47
2
-111
8

```

Per request adding whole main function:

```
void main(int argc,char** argv) {
char* filename;
char* outputfile;
char conf;
int arr[1000] = {0};
int n,a=0,d=0,h=0,i=0,c=0;
welcome();
        if (argc > 4 || argc < 3) {
                printf("\n     Invalid number of arguments.\n\n");
                helpmssg();
                exitmssg();
        }
        else if (argc == 4) {
                for (int i=3;i<argc;i++) {
                        if (argv[i][0] == '-') {
                                for(int z=1;z<strlen(argv[i]);z++) {
                                        if (argv[i][z] == 'a') {
                                                a=1;
                                        }
                                        else if (argv[i][z] == 'd') {
                                                d=1;
                                        }
                                        else if (argv[i][z] == 'h') {
                                                h=1;
                                        }
                                        else {
                                                        printf("\n     Invalid options in 3rd argument.\n\n");
                                                        helpmssg();
                                                        exitmssg();
                                        }
                                }
                        }
                        else {
                                printf("\n     3rd argument shoule begin with '-'\n\n");
                                helpmssg();
                                exitmssg();
                        }
        }       }
        filename = argv[1];
        FILE *fp;
        fp = fopen(filename, "r");
        if (fp == NULL) {
                printf("\n     Unable to open input file.\n\n");
                helpmssg();
                exitmssg();
        }
  while ((c=getc(fp)) != EOF){
                ungetc (c,fp);
                if (fscanf(fp,"%d\n", &c) == 1) {
                        arr[i] = c;
                        i++;n++;
                }
        }

//      int arr[] = {64, 34, 25, 12, 22, 11, 90};
        sort(arr, n,d);
        printf("Sorted array: \n");
        printArray(arr, n);

        outputfile = argv[2];
        fp = fopen(outputfile, "r");
                if (fp != NULL ) {
                        printf("\n     Output file already exists, would you like to overwrite it?: Y/N  \n\n");
                        scanf("%c", &conf);
                                if (conf == 'N' || conf == 'n') {
                                        helpmssg();
                                        exitmssg();
                                } else {printf("     Overwritting...\n\n");}
                }
        fclose(fp);
        fp = fopen(outputfile, "w");
                for (i=0; i < n; i++) {
                        fprintf(fp,"%d\n",arr[i]);
                }
        exitmssg();
}

```