Josh Morrison
Josh Morrison

Reputation: 7636

C file operation question

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 different pointer. Wish i made this clear.

Upvotes: 1

Views: 327

Answers (2)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

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

Upvotes: 5

Moo-Juice
Moo-Juice

Reputation: 38825

file* fd = fp;           

If I understand you correctly, of course.

Upvotes: 5

Related Questions