In this post I will collect all the different ways of writing data to disc that I encounter. They are illustrated with examples.
C style
The steps are
- open a file with fopen in the right mode
- write into the file using printf
- close the file
If you want to write to binary the “b” in e.g. fopen("test.bin","wb");
is the right choice.
If you want to write to a text file only use “w”. The same applies respectivly to “r”.
Reading and writing to binary file.
You are not loosing precions. But the format is not portable. And there is not extra information about the data avaiable in the file.
Consider for example Example 2. You get the binary file, but you don’t now what the data foramt of each pixel was or what the original size was.
Example 1: save double as binary and read it back in
#include<stdio.h> #include<random> using namespace std; int write_binary_to_file(double rand) { FILE *ptr_myfile; ptr_myfile=fopen("test.bin","wb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } //write count x sizeof(double) into the file. In our case count = 1. fwrite(&rand, sizeof(double), /*count = */ 1, ptr_myfile); fclose(ptr_myfile); } int read_binary_from_file() { FILE *ptr_myfile; ptr_myfile=fopen("test.bin","rb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } double rand; fread(&rand,sizeof(double),1,ptr_myfile); fclose(ptr_myfile); //%.10 sets the precision to 10 digits after the comma. //if you don't want to print the trailing zeros use "%.10g\n" instead. printf("%.10f\n", rand); return 0; } int main() { random_device seed_function; write_binary_to_file(seed_function()); read_binary_from_file(); return 0; }
Example 2: save random image as binary and read it back in
#include<stdio.h> #include<random> using namespace std; int write_image_to_file() { size_t size = 10; unsigned short* image = new unsigned short[size*size]; for(int k = 0; k<size*size; k++) { image[k]= k; } FILE *ptr_myfile; ptr_myfile=fopen("test.bin","wb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } //Writing the whole image at once case A or wirting each nuber separate case B. //Both approaches work. It would be good to test the performance before applying. //case A fwrite(image, size*size*sizeof(unsigned short), /*count =*/ 1, ptr_myfile); //case B //fwrite(image, sizeof(unsigned short), /*count =*/ size*size, ptr_myfile); fclose(ptr_myfile); } int read_image_from_file() { size_t size = 10; unsigned short* image = new unsigned short[size*size]; FILE *ptr_myfile; ptr_myfile=fopen("test.bin","rb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } fread(image, size*size*sizeof(unsigned short), /*count =*/ 1, ptr_myfile); fclose(ptr_myfile); for(int k = 0; k<size*size; k++) { printf("%d\n", image[k]); } return 0; } int main() { write_image_to_file(); read_image_from_file(); return 0; }
C++ style
#include <fstream> #include<iomanip> string name = "rectangle.txt"; fstream f; f << fixed; f << setprecision(3); f.open(name, fstream::out | fstream::trunc); //Empty file f.close(); f.open(name, fstream::out | fstream::app); f << x << "\t\t\t" << y << endl; f.close();
Output/Input Read/Write array/image to txt vs binary fstream vs stdio.h FILE
The fast and the slow way: