如何使用fwrite()

text
阅读 47 收藏 0 点赞 0 评论 0

fwrite_c.txt
Very similar to fread():

int arr[10];
fwrite(arr, sizeof(int), 10, ptr);

So have some useful information stored in the array arr[10], then use fwrite() to put the information in arr[10] into file with the pointer "ptr".

* Also need to be aware that the file can only be opened for "w" or "a", or else will be an error.

** be aware that the last parameter is file pointer, not file name.

Also can dynamically allocate memory space, then put useful information into that memory space, and write those information to file using fwrite():

double* arr2 = malloc(sizeof(double) * 80);
fwrite(arr2, sizeof(double), 80, ptr);

Also can put the information stored in a variable into the file using fwrite(), use the "&" to get the memory address for the variable:

char c;
fwrite(&c, sizeof(char), 1. ptr);



评论列表


问题


面经


文章

微信
公众号

扫码关注公众号