如何在C中将数组写入文件 [英] How to write an array to file in C
本文介绍了如何在C中将数组写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个二维矩阵:
char clientdata[12][128];
将内容写入文件的最佳方法是什么?我需要不断更新此文本文件,以便每次写入文件中的先前数据都会被清除.
What is the best way to write the contents to a file? I need to constantly update this text file so on every write the previous data in the file is cleared.
推荐答案
由于数据的大小是固定的,因此将整个数组写入文件的一种简单方法是使用二进制写入模式:
Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode:
FILE *f = fopen("client.data", "wb");
fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
fclose(f);
这会立即写出整个 2D 数组,覆盖之前存在的文件内容.
This writes out the whole 2D array at once, writing over the content of the file that has been there previously.
这篇关于如何在C中将数组写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文