1

I want to pass the file pointer as an argument to view function. And then I want to get the output the data on file from the view function. But every time telling me file not found.

#include<stdio.h>
void view(FILE *file){
    char c;
    file=fopen(file,"r");
    if(file==NULL){
        printf("file not found");
    }
    else{
        while(!feof(file)){
       c=fgetc(file);
       printf("%c",c);
    }
    fclose(file);
}
    }



int main(){

FILE *file;
file=fopen("hello.txt","w");
char s[]="hello world";

fprintf(file,"%s",s);
fclose(file);
printf("Enter 1 to read file");
int n;
scanf("%d",&n);
if(n==1)
view(file);

return 0;

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Ishan Ahmed
  • 105
  • 11
  • 2
    Doesn't the compiler shout at you? Because think a little more about the call to `fopen` you have in `view`: `file=fopen(file,"r")` Does it really make sense to pass a `FILE *` as the file-name to `fopen`? The compiler should really have complained about that. – Some programmer dude Apr 12 '21 at 10:48
  • I didn't get any warning – Ishan Ahmed Apr 12 '21 at 10:50
  • 1
    On a couple of unrelated notes, please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) And remember that [`fgetc`](https://en.cppreference.com/w/c/io/fgetc) returns a **`int`**, which is important for the comparison against `EOF` which you should have instead of that `feof`. – Some programmer dude Apr 12 '21 at 10:50
  • 1
    [All the big three compilers](https://godbolt.org/z/6Ka4o6qs3) emit warnings about it. You should always treat warnings as errors that must be fixed. If you really didn't get any warning, then what compiler are you using? What version of it? – Some programmer dude Apr 12 '21 at 10:54
  • 2
    Why do you want to pass a `FILE` pointer if you close the file before the call? A pointer to a file musn't be used for anything after it was closed. Passing it is worthless. You should pass file name instead. – Gerhardh Apr 12 '21 at 10:54
  • Can I declare FILE *file in global scope?? – Ishan Ahmed Apr 12 '21 at 11:05
  • 2
    Yes you can but it wouldn't help with the problem you have in your code. Perhaps you meant to pass the file *name* as argument? Then the argument variable should be named differently, and be of the type `const char *` instead. And you need to define `FILE *file` in the function itself (global variables are bad). – Some programmer dude Apr 12 '21 at 11:06

2 Answers2

3

Your error is here:

file=fopen(file,"r");

Use something like this:

file=fopen("file name","r");
Evgeny
  • 1,072
  • 6
  • 6
1

As already stated in the comments, and in this answer, the fopen argument is wrong, you pass a pointer to file when you should pass the file path.

Other than that you could refactor your code so that you wouldn't have to close and reopen the file:

void view(FILE *file)
{
    // the file is already opened, no need to reopen it
    int c;
   
    while ((c = fgetc(file)) != EOF) // better read routine
    {                                // you could also use fgets to read the whole line
        printf("%c", c);
    }
    fclose(file);
}
int main()
{
    FILE *file;
    file = fopen("hello.txt", "w+"); // open to write and read
    int n;
    char s[] = "hello world";

    if (file == NULL)
    {
        printf("file not found"); // perror("fopen"); would be better
        return EXIT_FAILURE; // #include <stdlib.h>
    }

    fprintf(file, "%s", s);

    printf("Enter 1 to read file: ");

    if (scanf("%d", &n) == 1)
    {       
        if (n == 1)
        {
            rewind(file); // reset file pointer offset to the beginning
            view(file);
        }
    }
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
anastaciu
  • 23,467
  • 7
  • 28
  • 53