Jitesh Dani
Jitesh Dani

Reputation: 385

File Pointer Position

Is this okay ? Will my filepointer get messed up in XYZ()?

function XYZ()
{
    fopen(myFile)
    // some processing
    // I am in the middle of the file
    Call ABC()   
}

ABC ()
{
    fopen(myFile)
    //do some processing
    fclose (myFile)
}

Upvotes: 0

Views: 117

Answers (2)

C. Ross
C. Ross

Reputation: 31848

This is bad form, even though it may technically correct. A better way would be

function XYZ()
{
    handle = fopen(myFile)
    // some processing
    // I am in the middle of the file
    Call ABC(handle) 
    fclose(handle)  
}

ABC (handle)
{
    //do some processing on handle
}

And if your language supports a try-finally construct I highly recommend using that ie:

function XYZ()
{
    handle = fopen(myFile)
    try 
    {
       // some processing
       // I am in the middle of the file
       Call ABC(handle) 
    }
    finally
    {
       fclose(handle)  
    }
}

ABC (FilePtr handle)
{
    //do some processing on handle
}

Upvotes: 1

user153498
user153498

Reputation:

If you open an additional handle to the file, then no, your previous handle will be unaffected. Its like you open the file twice on Notepad, you move the cursor to one part in the first instance, but the cursor won't move in the other instance.

(Bad example, I know...)

Upvotes: 1

Related Questions