C文件操作问题

file * fp = fopen() file * fd = ???? 

我想使用*fd来编写之前打开的*fp文件。

我该怎么做?

添加一些,这个问题的关键是使用另一个指针来做到这一点。 看,* fd是不同的指针。 希望我明白这一点。

 file* fd = fp; 

当然,如果我理解正确的话。

根据您的需要使用fwritefputcfprintffputs

使用fputc ,你可以放一个char

 FILE *fp = fopen("filename", "w"); fputc('A', fp); // will put an 'A' (65) char to the file 

使用fputs ,你可以放一个char数组(字符串):

 FILE *fp = fopen("filename", "w"); fputs("a string", fp); // will write "a string" to the file 

使用fwrite您还可以编写二进制数据:

 FILE *fp = fopen("filename", "wb"); int a = 31272; fwrite(&a, sizeof(int), 1, fp); // will write integer value 31272 to the file 

使用fprintf您可以编写格式化数据:

 FILE *fp = fopen("filename", "w"); int a = 31272; fprintf(fp, "a's value is %d", 31272); // will write string "a's value is 31272" to the file