C文件操作问题
file * fp = fopen() file * fd = ????
我想使用*fd
来编写之前打开的*fp
文件。
我该怎么做?
添加一些,这个问题的关键是使用另一个指针来做到这一点。 看,* fd是不同的指针。 希望我明白这一点。
file* fd = fp;
当然,如果我理解正确的话。
根据您的需要使用fwrite
, fputc
, fprintf
或fputs
。
使用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