2009-09-02 85 views
0

這樣好嗎?我的filepointer會在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) 
} 
+0

語言是什麼呢? – 2009-09-02 15:03:59

回答

1

如果您打開文件的附加句柄,那麼否,您的上一個句柄將不受影響。就像你在記事本上打開文件兩次一樣,你將光標移動到第一個實例的一個部分,但是光標不會在另一個實例中移動。

(不好的例子,我知道...)

1

這是不好的形式,儘管它可能在技術上是正確的。 一個更好的辦法是

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 
} 

如果你的語言支持try-finally construct我強烈建議使用 即:

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 
}