tags:

views:

41

answers:

2

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)
}
+1  A: 

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...)

nasufara
+1  A: 

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
}
C. Ross