C

[C/C++] 多筆資料 二進制檔案 讀取 -使用fread

前言


此篇介紹如何對二進制檔做多筆資料的讀取,另外,若想要知道做寫入,可以參考「C/C++多筆資料的二進制檔案寫入-使用fwrite」

實作方式


首先,先讓我們來看看二進制的檔案寫入所提供的函式吧,參考C++ Reference,如下圖:

fread的函式參數,其實要填入的參數與fread一致,它的第一個參數是你所要讀取到的變數記憶體位置(也可以是陣列或結構),第二個參數是此變數的資料型態bytes大小,第三個參數是一次讀取的筆數(假若是5,就代表寫入5筆),最後是你所使用的FILE檔案指標。

那麼以下讓我們來要如何做多筆資料的讀取呢?也是和前一篇fwrite一樣也是透過尋訪方式一筆一筆讀取,如下:

product_t p_list[10];//宣告

[C/C++]多筆資料 二進制檔案 寫入-使用fwrite

前言


當我們在做C或C++語言的檔案讀寫時,時不時可能都會用到二進制的檔案讀取或寫入,而有時候我們可能會一次寫入多筆資料,如陣列,或是結構的陣列等,那麼這時候我們需要如何確保可以一次全部寫入,並一次全部讀取呢?

實作方式


此篇會先介紹寫入的部分,而若想知到讀取的部分,可以到另一篇「C/C++多筆資料的二進制檔案讀取-使用fread」來了解。

首先,先讓我們來看看二進制的檔案寫入所提供的函式吧,參考C++ Reference,如下圖

節錄至:C++ Reference-fwrite

Parallel algorithm to solve a Hamiltonian Path problem variation (Travelling Baseball Fan) (Bradley Kuszmaul)

The included source code implements a variation of the Hamiltonian Path problem, called the Travelling Baseball Fan Problem, as described in the included problem description text file. The included write-up gives an overview of Cilk++ and some of the tools available for Cilk programming. The serial algorithm is a recursive search of all paths. To this basic algorithm, two heuristics have been added to reduce search time: remaining-degree and remaining-city.

Parallel algorithm to 3-D Convex Hull Problem (Bradley Kuszmaul)

The included code and white paper provides a parallel solution for the 3-D Convex Hull problem, as described in the included problem description text file. Parallelism is achieved using Cilk++. Possible points for the convex hull are found by repeatedly selecting four points from the input set and finding the largest volumes of the formed tetrahedrons (using matrix determinants). An O(n^4) algorithm confirms those points that are actually on the convex hull from the possible points previously found.

Parallel algorithm to Bounded Knapsack Problem (matteocilk.com)

The included code and solution write-up provides a parallel solution for the bounded knapsack problem, as described in the included problem description text file. The included write-up gives an overview of a branch-and-bound technique (as opposed to a dynamic programming algorithm) to enumerate possible solutions. The three heuristics that are implemented in the code (linear-programming relaxation, minimum-weight heuristic, and dominator heuristic) are described. The algorithm is parallelized using Cilk++ in order to launch recursive search tasks.

S’abonner à C