C/C++ fwrite 用法與範例

本篇 ShengYu 介紹 C/C++ fwrite 的用法與範例,C/C++ 可以使用 fwrite 將文字寫入到檔案裡,在 fwrite 函式的引數裡可以指定要寫入幾個 bytes 字元,fwrite 除了可以將文字寫入到檔案裡以外也能將文字寫入到標準輸出上,詳見本篇範例。

C/C++ 要使用 fwrite 的話需要引入的標頭檔 <stdio.h>,如果要使用 C++ 的標頭檔則是引入 <cstdio>
fwrite 函式原型為

1
size_t fwrite(const void * ptr, size_t size, size_t count, FILE * stream);

ptr:指向一塊元素陣列的指標,該元素陣列將會被寫入到檔案裡
size:要寫入的每一個元素大小(單位為 byte)
count:要寫入的元素數量
stream:指向 FILE 物件的指標

以下 C/C++ fwrite 的用法介紹將分為這幾部份,

  • C/C++ fwrite 將文字寫入到檔案的基本用法
  • C/C++ fwrite 將文字寫入到標準輸出
  • C/C++ fwrite 寫入二進制檔
  • C/C++ fwrite 將 struct 資料寫入二進制檔

那我們開始吧!

C/C++ fwrite 將文字寫入到檔案的基本用法

這邊介紹 C/C++ fwrite 寫入文字檔的基本用法,在用 fwrite 來寫入文字檔前要先 fopen 開檔成功才能對檔案寫入,fopen 開檔回傳 NULL 表示開檔失敗,如果不是 NULL 表示開檔成功。

接著使用 fwrite 進行寫入文字,我們宣告的 buffer 有 1024 大小,但是裡面只有 24 的字元需要寫入到檔案裡,所以這邊 fwrite 第三個引數填入 24,fwrite 會回傳寫入了多少 bytes,沒有意外的話就是跟我們帶入的大小一樣,也就是 numwritten 預期會是 24,寫完後最後 fclose 關檔,

cpp-fwrite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// g++ cpp-fwrite.cpp -o a.out
#include <stdio.h>

int main() {
FILE *fp;
char buffer[1024] = "hello world\n123\n456\n789\n";
size_t numwritten;

fp = fopen("output.txt", "w");
if (fp == NULL) {
printf("failed to open the file.\n");
return 1; // EXIT_FAILURE
}

numwritten = fwrite(buffer, sizeof(char), 24, fp);
printf("write %zu bytes\n", numwritten);
fclose(fp);
return 0;
}

結果輸出如下,

1
write 24 bytes

輸出 output.txt 的內容如下,

1
2
3
4
hello world
123
456
789

C/C++ fwrite 將文字寫入到標準輸出

C/C++ fwrite 除了對檔案寫入文字以外也可以用來對標準輸出進行寫入文字,範例如下,標準輸出為 stdout 就不需要像檔案一樣開檔了,

cpp-fwrite2.cpp
1
2
3
4
5
6
7
8
9
10
11
// g++ cpp-fwrite2.cpp -o a.out
#include <stdio.h>

int main() {
char buffer[32] = "hello world\n";
size_t numwritten;
numwritten = fwrite(buffer, sizeof(char), 12, stdout);
printf("write %zu bytes\n", numwritten);

return 0;
}

標準輸出如下,

1
2
hello world
write 12 bytes

C/C++ fwrite 寫入二進制檔

這邊介紹 C/C++ fwrite 寫入二進制檔,假如只寫入單純一筆 int 的資料的話,範例如下,

cpp-fwrite3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// g++ cpp-fwrite3.cpp -o a.out
#include <stdio.h>

int main() {
int num = 5;

FILE *fp = fopen("output.bin", "wb");
if (fp == NULL) {
printf("failed to open the file.\n");
return 1; // EXIT_FAILURE
}

size_t numwritten = fwrite(&num, sizeof(int), 1, fp);
printf("write %zu bytes\n", numwritten);
fclose(fp);
return 0;
}

hexdump output.bin 輸出如下,

1
2
3
$ hexdump output.bin
0000000 05 00 00 00
0000004

假如要寫入 int 陣列的資料的話,範例如下,

cpp-fwrite4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// g++ cpp-fwrite4.cpp -o a.out
#include <stdio.h>

int main() {
int arr[3] = {1, 2, 3};

FILE *fp = fopen("output.bin", "wb");
if (fp == NULL) {
printf("failed to open the file.\n");
return 1; // EXIT_FAILURE
}

size_t numwritten;
numwritten = fwrite(&arr, sizeof(int), 3, fp);
// or
/*for (int i = 0; i < 3; i++) {
numwritten += fwrite(&arr[i], sizeof(int), 1, fp);
}*/
printf("write %zu bytes\n", numwritten);
fclose(fp);
return 0;
}

hexdump output.bin 輸出如下,

1
2
3
$ hexdump output.bin
0000000 01 00 00 00 02 00 00 00 03 00 00 00
000000c

上例中也可以搭配用 for 迴圈的方式寫入,

1
2
3
for (int i = 0; i < 3; i++) {
numwritten += fwrite(&arr[i], sizeof(int), 1, fp);
}

要用 fread 讀取二進制檔資料的話可以看看這篇

C/C++ fwrite 將 struct 資料寫入二進制檔

這邊示範 C++ 用 fwrite 將 struct 資料寫入二進制檔,寫入 struct 資料前先寫入 struct 的筆數,這樣在讀取二進制檔案時可以方便先知道要讀取幾筆 struct 資料,

cpp-fwrite5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// g++ cpp-fwrite5.cpp -o a.out
#include <stdio.h>

struct Student {
int id;
char name[4];
int age;
};

int main() {
struct Student st[3] = {
{1, "ab", 18},
{2, "cd", 19},
{3, "ef", 20},
};
int count = 3;

FILE *fp = fopen("output.bin", "wb");
if (fp == NULL) {
printf("failed to open the file.\n");
return 1; // EXIT_FAILURE
}

fwrite(&count, sizeof(int), 1, fp); // 寫入筆數
fwrite(st, sizeof(struct Student), 3, fp);
fclose(fp);
return 0;
}

hexdump output.bin 輸出如下,

1
2
3
4
5
$ hexdump output.bin
0000000 03 00 00 00 01 00 00 00 61 62 00 00 12 00 00 00
0000010 02 00 00 00 63 64 00 00 13 00 00 00 03 00 00 00
0000020 65 66 00 00 14 00 00 00
0000028

要用 fread 將二進制檔的資料讀取到 struct 裡的話可以看看這篇

以上就是 C/C++ fwrite 的用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 C++ 相關技術,可以參考看看下面的文章,
C/C++ 新手入門教學懶人包
C/C++ fopen 用法與範例
C/C++ fread 用法與範例
C/C++ fgets 用法與範例
C/C++ fputs 用法與範例
C/C++ fclose 用法與範例