-1

I am a learning file handling in C. I tried implementing a program but no matter what I do the file pointer is still null.

I checked the spelling, the directory, even tried adding and removing .txt extension but I still don't get what is wrong with this program.

#include <stdio.h>

int main()
{
    FILE *fptr1;
    char filename[100], c;

    printf("Enter the filename to open for reading: ");
    scanf("%s", filename);

    fptr1 = fopen(filename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
    }
    do {
        c = fgetc(fptr1);
        printf("%c", c);
    } while (c != EOF);
    fclose(fptr1);
    return 0;
    }
33
  • 2
    Welcome to StackOverflow. Please share what you've tried so far. Commented Jun 19, 2023 at 14:47
  • 3
    Change the line printf("Cannot open file %s \n", filename); to perror(filename); to get a hint of why it can't be opened. Commented Jun 19, 2023 at 15:08
  • 2
    Btw, scanf("%s", filename); looks like the wrong tool since it will only read a word. If your filename contains whitespaces, it will be cut short. Use fgets(filename, sizeof filename, stdin) instead - and always check the return values from functions that can fail, like scanf and fgets can. Commented Jun 19, 2023 at 15:09
  • 1
    @RupaTS We can't say anything about that because you haven't shown how you read from the file. Commented Jun 19, 2023 at 15:23
  • 2
    you'll also need to add the code that reads and closes the file in an else block. If fptr1 == NULL you print out an error, but then go on and try to read the file anyway. What if you hardcode the file name rather than getting it from user input, same problem? Commented Jun 19, 2023 at 15:45

1 Answer 1

0

I tried your code it works fine , you just have to add (filename.txt) as your input like (myfile.txt)

well i changed some in your code to make it more simpler ,so here is the changed sections

First from this

 if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
    }

to this

 if (fptr1 == NULL)
        printf("Cannot open file %s \n", filename);

Because when you have only one line that execute after the condition is true , you can get ride of the { }

also i changed the ( do while ) to ( while ) Second from this

 do {
        c = fgetc(fptr1);
        printf("%c", c);
    } while (c != EOF);

to this

    while(!feof(fptr1)){
        c = fgetc(fptr1);
        printf("%c",c);
    }

Here the line feof is a function that means the end of file

We are checking if its NOT the end of file ( !feof ) , if thats true then we can read from the file and print the result . the loop ends when c = EOF .

and thats it.

NOTE : your file name should be without spaces ( myfile.txt ) and not (my file.txt) because we are using scanf() function , it stoppes when countering a space ( blank space ) , so keep that in mind.

Sign up to request clarification or add additional context in comments.

2 Comments

The function feof specifies whether a function call to fgetc has already failed due to end-of-file. It does not necessarily specify whether the next call to fgetc will fail. Therefore, your use of feof is wrong, because you will attempt to print one EOF character returned by fgetc. I suggest that you read this for further information: Why is “while( !feof(file) )” always wrong?
For the reason stated in my previous comment, I think the best solution would be while ( (c=fgetc(fptr1)) != EOF ) { printf( "%c", c ); }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.