enamel
enamel

Reputation: 81

updating file pointer via other function

#include<stdio.h>

void hello(FILE * fp)
{ 
    if( ( fp = fopen("log","r") ) == NULL)
        printf("%s", "Error opening file");
}

void main()
{
    char p;
    FILE *sf=fopen("prac.txt","r");
    hello(sf);

    p=fgetc(sf);
    printf("%c",p);
}

I wanted to change the file pointer sf to point to file log via hello function but printf is still printing the content of prac.txt file.

Upvotes: 1

Views: 1175

Answers (5)

Vatine
Vatine

Reputation: 21288

If you want to change what the FILE* points to, you need to pass a FILE**. Before you change it, you need to ensure that any file it happens to be pointing to is closed. This also relies on you always setting FILE* variables to NULL after fclose (this, alas, does not happen automatically), so there's a decent chance careless use of this function would call fclose on an already-closed FILE*. But this is probably still better than willfully leaking file descriptors and not flushing files.

void hello(FILE **fp)
{
  // This is actually a horrible test. And in general, this is not
  // something you should do, but it is better than leaking open
  // file descriptors, so, yeah, 
  if (*fp != NULL) {
    fclose(*fp);
    *fp = NULL;
    printf("Closed file.");
  }
  if( (*fp = fopen("log","r") == NULL) {
    printf("%s", "Error opening file");
  }
}

Upvotes: 3

ani627
ani627

Reputation: 6067

#include<stdio.h>
// Use double pointer since fp is getting address of a pointer variable
void hello(FILE ** fp)
{ 
    // If the file is already pointing to some file then close that file
    if(*fp != NULL)
    {
        fclose(*fp); 
        *fp = NULL;
    }
    if( ( *fp = fopen("log","r") ) == NULL)
        printf("%s", "Error opening file");
}

int main()
{
    char p;
    FILE *sf=fopen("prac.txt","r");
    // While sending the file pointer you need to send the address of the file pointer
    hello(&sf);

    p=fgetc(sf);
    printf("%c",p);

    return 0;
}

Upvotes: 0

Aymanadou
Aymanadou

Reputation: 1220

you should close the file before reopning it

#include<stdio.h>
void hello(FILE ** fp)
{ 
 fclose(*fp);
if( ( *fp = fopen("log","r") ) == NULL)
  printf("%s", "Error opening file");    
}

void main()
{
 char p;
 FILE *sf=fopen("prac.txt","r");
 hello(&sf);

 p=fgetc(sf);
 printf("%c",p);
}

Upvotes: 0

Fefux
Fefux

Reputation: 974

In void hello(FILE *fp), fp only exists in the scope of the function hello (the value of the pointer is copied when you call the function and it's destroyed at the end of the func).

This works:

#include<stdio.h>

FILE* hello(FILE * fp)
{ 
    fclose(fp);
    if( ( fp = fopen("log","r") ) == NULL) {
        printf("%s", "Error opening file");
        return NULL;
    }
    return fp;
}

void main()
{
    char p;
    FILE *sf=fopen("prac.txt","r");
    sf = hello(sf);

    if (sf != NULL) 
    {
        p=fgetc(sf);
        printf("%c",p);
    }
}

Upvotes: 2

Thomas Pinetz
Thomas Pinetz

Reputation: 7148

This is because you copy arguments. You do not override the content of the file pointer only the local copy of the address.

The file pointer itself is not updated. To archive this either pass a pointer to a pointer to the function and dereference accordingly or return the new file pointer and override the old one. Anyways care for closing the file.

Upvotes: 0

Related Questions