Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

一個c的讀檔問題

0 views
Skip to first unread message

milc

unread,
Oct 1, 2002, 12:30:48 PM10/1/02
to
請問一下我有一個檔案"test.txt"
內容如下
1
2
3
4
我想用c語言在每一行後加上9
請問該如何寫,也就是
1 9
2 9
3 9
4 9
麻煩講解一下
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: 140.109.22.172

要用功一點了

unread,
Oct 1, 2002, 1:27:40 PM10/1/02
to
※ 引述《milc...@bbs.cs.nthu.edu.tw (milc)》之銘言:
: 請問一下我有一個檔案"test.txt"

: 內容如下
: 1
: 2
: 3
: 4
: 我想用c語言在每一行後加上9
: 請問該如何寫,也就是
: 1 9
: 2 9
: 3 9
: 4 9
: 麻煩講解一下

用 fopen();開檔
然後 fgets();讀資料
接著將讀到的資料改成你要的
覺得可以用 sprintf 或者是 fprintf....
將改過的資料先存在別的檔案或者是戰存的鎮列
到了最後在...恩恩 找到了... rewind();
回到test.txt的開頭 然後寫入

我想到的方法是這樣子啦

#include <stdio.h>

#define BUF_SIZE 1024

int main(void){
FILE *op,*temp;
char buf[BUF_SIZE];
...

/*---- open the file stream ----- */
...

/*---- read the data from 'test.txt. and change the data ----*/
/*---- write the data into temp_file -----------------------*/
...

/*---- copy the file of temp_file to 'test.txt.--------------*/
rewind(op);
...

return 0;

}

--

或許人生是一段整合內在的自己
並尋求人間一點真情的過程吧
--
[1;31mO [1;32mr [1;33mi [1;34mg [1;35mi [1;36mn [1;31m: [1;36m<不良牛牧場> [1;33mzoo.ee.ntu.edu.tw [1;32m(140.112.18.36) [m
[1;32mWelcome to SimFarm BBS [1;36m-- [m [1;31mFrom : [ [m140.112.249.54 [1;31m] [m

0 new messages