file * fp = fopen()
file * fd = ????
Run Code Online (Sandbox Code Playgroud)
我想用来*fd写*fp以前打开过的文件.
我该怎么做?
添加一些,这个问题的关键是使用另一个指针来做到这一点.看,*fd是不同的指针.希望我明白这一点.
使用fwrite,fputc,fprintf,或fputs,这取决于你所需要的.
有了fputc,你可以把char:
FILE *fp = fopen("filename", "w");
fputc('A', fp); // will put an 'A' (65) char to the file
Run Code Online (Sandbox Code Playgroud)
有了fputs,你可以放一个char数组(字符串):
FILE *fp = fopen("filename", "w");
fputs("a string", fp); // will write "a string" to the file
Run Code Online (Sandbox Code Playgroud)
随着fwrite你也可以写二进制数据:
FILE *fp = fopen("filename", "wb");
int a = 31272;
fwrite(&a, sizeof(int), 1, fp);
// will write integer value 31272 to the file
Run Code Online (Sandbox Code Playgroud)
随着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
Run Code Online (Sandbox Code Playgroud)