C Promgramming Read 野

2007-08-09 2:14 am
有冇方法run完第一個function之後 run第二個function果時由頭read過d char
以下係program:

void fun1(char *fp)
{
ch1 = fgetc(fp);
while (ch1 != EOF)
{
....
}
fclose(fp);
}

void fun2(char *fp)
{
同fun1一樣......
}

main()
{
...
}

回答 (2)

2007-08-10 6:09 pm
✔ 最佳答案
What you use need is rewind() :

void fun1(char *fp) {
ch1 = fgetc(fp);
while (ch1 != EOF)
{
....
}
// DO NOT close the file, otherwise rewind() does not work.
}

void fun2(char *fp) {
rewind(fp);
// start read at the beginning
}

main() {
...
fun1(fp);
fun2(fp);
fclose(fp);
}
2007-08-10 11:12 am
function2 唔好 pass 個 file ref.~
係function2 內 再開個file,就可以重頭再read char 多一次﹗

void fun2()
{
FILE * fp2;
fp2 = fopen ("myfile.txt","r")

ch2 = fgetc(fp2);
while (ch2 != EOF)
{
....
} // end of while loop
fclose(fp2);

} // end of function 2
參考: 自己


收錄日期: 2021-04-23 17:14:28
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070808000051KK04514

檢視 Wayback Machine 備份