c++ input file問題

2007-05-05 11:03 am
c++讀取file(input)
點先可以讀到每一個char或者int?
同埋可唔可以話俾部電腦聽從第幾行開始搵起或者凈係搵果行?
回復後,唔該定時定後check下e個知識-.-因為我可能仲有問題
ty
更新1:

我試過用get(); 但係唔知有冇用錯 我用左get(save1,loadname); 佢話唔得get undeclared (first use this function) 我用dev-c++

更新2:

我已經搵到點讀char 但係仲未識點讀int同埋從第幾行開始搵起

更新3:

順便係到問埋output file時, 點先可以由end of file開始寫? 好似個log咁樣

更新4:

To: gao_gai_garhk 唔係好明fscanf(); 可唔可以俾source code我

回答 (2)

2007-05-05 7:07 pm
✔ 最佳答案
To read a integer, the simplest way is to use fscanf() :

e.g. fscanf(your_file_pointer, "%d", your_int_var);

But for C++, there is no way to to search on specific row, but there is a way to start the file at specific byte offset, e.g. to start with the offset of 20 bytes after starting:

fseek ( your_file_pointer, 20, SEEK_SET);

For appending a file, just set the openning mode of the file to append mode :

e.g. your_file_pointer = fopen(filename, "a+");

// "w+" is opening a output file and start write at start, while "a+" is opening a output file and start write at the end

2007-05-10 01:16:01 補充:
fscanf is the same as scanf :e.g. :FILE* my_file = fopen("data.txt", "r ");int a;fscanf(my_file, "%d", &a);...fclose(my_file);
2008-01-21 9:32 pm
kan bu ming ~_~


收錄日期: 2021-04-23 16:56:44
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070505000051KK00516

檢視 Wayback Machine 備份