T.T.T.
T.T.T.

Reputation: 34523

Can I assign the same file pointer a second file?

function()
{
 FILE *ptr;
 ptr = fileopen(file1.txt)
 fprint(ptr, some text) //print to file 1

     if(second file needed)
     {
        ptr = fileopen(file2.txt) //open a second file, assign to same file pointer
        fprint(ptr, some text) //print to file 2  not working here? 

     }

}

EDIT:
Not printing to second file...However, fprint() does not return a negative value.

Upvotes: 2

Views: 2316

Answers (3)

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

Yes all pointers are simply variables that hold a memory address. At first your pointer holds the first memory address that fileopen (I guess you probably meant fopen though?) returns. You can put a different memory address in it later.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340188

You can do that, but the problem is that you've lost a way to access the 1st opened file (even if just to close it).

Upvotes: 2

jer
jer

Reputation: 20236

Yes you can, however, you should ensure that the first file is closed before doing so.

Upvotes: 5

Related Questions