0

I am having trouble opening and parsing a file in C. As of now I am trying to open a file and print out the current line then the next until the program reaches the end of the file. However, the file pointer always returns NULL and does not read the file.

This is my function:

int file_parse() {
    char filename[100];
    printf("Enter the filename: ");
    scanf("%s", filename); //Get filename
    printf("\nYou want to encrypt or decrypt with file: %s", filename); //Confirm filename

    int origin[27];
    int final[27];

    FILE *fp;

    if ((fp = fopen(filename, "r")) != NULL) {
        char current_line[250];

        while (!feof(fp)) {
            fgets(current_line, 250, fp);
            printf("The current line is: %s", current_line);
        }

        fclose(fp);

    } else {
        printf("\nError! Cannot open the specified file.");
        exit(EXIT_FAILURE);
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

0 Answers0