2011-01-26 121 views
1
file * fp = fopen() 
file * fd = ???? 

我想用*fd來編寫*fp之前打開的文件。C文件操作問題

我該怎麼辦呢?

添加一些,這個問題的關鍵是使用另一個指針來做到這一點。請參閱* fd是不同的指針。我希望我明確這一點。

+1

http://www.cprogramming.com/tutorial/cfileio.html – marcog 2011-01-26 18:30:04

+0

這是不是很清楚你想要做什麼。 – sth 2011-01-26 18:32:09

回答

5
file* fd = fp;   

如果我理解正確的話,當然。

5

使用fwrite,fputc,fprintffputs,這取決於你需要什麼。

隨着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