Posts

Showing posts with the label Null Terminated

C++: Store Read Binary File Into Buffer

Answer : I just want to mention that there is a standard way to read from a binary file into a buffer. Using <cstdio> : char buffer[BUFFERSIZE]; FILE * filp = fopen("filename.bin", "rb"); int bytes_read = fread(buffer, sizeof(char), BUFFERSIZE, filp); Using <fstream> : std::ifstream fin("filename.bin", ios::in | ios::binary ); fin.read(buffer, BUFFERSIZE); What you do with the buffer afterwards is all up to you of course. Edit: Full example using <cstdio> #include <cstdio> const int BUFFERSIZE = 4096; int main() { const char * fname = "filename.bin"; FILE* filp = fopen(fname, "rb" ); if (!filp) { printf("Error: could not open file %s\n", fname); return -1; } char * buffer = new char[BUFFERSIZE]; while ( (int bytes = fread(buffer, sizeof(char), BUFFERSIZE, filp)) > 0 ) { // Do something with the bytes, first elements of buffer. // For exa...