開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev c++
問題(Question):
現在有兩個檔案read.txt跟write.txt,
想將read的資料抓出修改後寫入write
餵入的資料(Input):
read.txt:A123456789張阿三10
B123456789李阿四15
C123456789王老五45
就上面三行,要寫入從write.txt的下一行開始
錯誤結果(Wrong Output):
A123456789張阿三10
B123456789李阿四15
C123456789王老五45C123456789王老
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
#define read "E:\\read.txt"
#define write "E:\\write.txt"
typedef struct{
char ID[10];
char name[6];
char age[2];
}DATA;
int main(void)
{
FILE *readPtr, *writePtr;
DATA data;
readPtr = fopen(read, "rb");
writePtr = fopen(write, "ab");
if (!readPtr) {
printf("read.txt fail to open...\n");
}
else if(!writePtr) {
printf("write.txt fail to open...\n");
}
else{
while(!feof(readPtr)){
fread(&data, sizeof(DATA), 1, readPtr);
fwrite(&data, sizeof(DATA), 1, writePtr);
fprintf(writePtr,"\0"); //為了換行
}
}
fclose(readPtr);
fclose(writePtr);
system("pause");
return 0;
}
補充說明(Supplement):
新手求助:
問題1.為什麼會多出"C123456789王老"?
2.如果我想把裡面資料儲存,我這樣定義struct怎麼把讀出來的存進去?
3.假設我想修改裡面資料後再寫入檔案該怎麼做?
請老手賜教! 謝謝