开发者

C file operation question

开发者 https://www.devze.com 2023-02-06 17:40 出处:网络
file * fp = fopen() file * fd = ???? I want to use *fd to write file which *fp opened before. How can I do it?
file * fp = fopen()
file * fd = ????

I want to use *fd to write file which *fp opened before.

How can I do it?

Add some, the key of this question is use another pointer to do it. See, *fd is the differ开发者_开发百科ent pointer. Wish i made this clear.


file* fd = fp;           

If I understand you correctly, of course.


Use fwrite, fputc, fprintf, or fputs, depending on what you need.

With fputc, you can put a char:

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

With fputs, you can put a char array (string):

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

With fwrite you could also write binary data:

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

With fprintf you could write formatted data:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消