开发平台(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.假设我想修改里面资料后再写入档案该怎么做?
请老手赐教! 谢谢