Reading and writing binary files in C++ is one of the technical skills for C++ developers. Binary files are files that store data in a binary format, which means that each byte in the file represents a value or a sequence of bits. Binary files are different from text files, which store data as characters that can be read by humans. Binary files are more efficient and compact than text files, and they can store any type of data, such as images, audio, video, or executable code.
To read and write binary files in C++, you need to use the std::fstream class, which inherits from both std::istream and std::ostream. This class allows you to create file stream objects that can perform input and output operations on binary files. To open a binary file, you need to specify the ios::binary flag as one of the mode parameters in the open() method. For example:
std::fstream file;
file.open("data.bin", std::ios::in | std::ios::out | std::ios::binary);
This code opens a file named data.bin for both reading and writing in binary mode. If the file does not exist, it will be created. You can also use the constructor of std::fstream to open a file directly:
std::fstream file("data.bin", std::ios::in | std::ios::out | std::ios::binary);
To read data from a binary file, you can use the read() method, which takes two parameters: a pointer to a buffer where the data will be stored, and the number of bytes to read. For example:
char buffer[10];
file.read(buffer, 10);
This code reads 10 bytes from the file and stores them in the buffer array. You can also use the extraction operator (>>) to read data from a binary file, but this is not recommended because it may skip some bytes or interpret them as whitespace characters.
To write data to a binary file, you can use the write() method, which takes two parameters: a pointer to a buffer where the data is stored, and the number of bytes to write. For example:
char buffer[10] = "Hello";
file.write(buffer, 10);
This code writes 10 bytes from the buffer array to the file. You can also use the insertion operator (<<) to write data to a binary file, but this is not recommended because it may add some formatting characters or convert some data types.
To close a binary file, you can use the close() method or let the destructor of std::fstream do it automatically when the file stream object goes out of scope. For example:
file.close();
This code closes the file and releases any resources associated with it.
Reading and writing binary files in C++ is a useful skill for C++ developers because it allows them to manipulate any kind of data in an efficient and flexible way. However, it also requires careful attention to details such as the size and alignment of data types, the endianness of the system, and the error handling of file operation
In this article, we will show you how to read text and binary files using C++ standard library classes and functions
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Text File license.txt
https://learn-tech-tips.blogspot.com
zidane
huuvi168@gmail.com
learntechtips
learn-tech-tips
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:CMyReadFile.cpp source code!
/* ****************************************************************
* Developer: Zidane (huuvi168@gmail.com)
* Last Modified: 2015/09/30
* ***************************************************************/
CString CMyReadFile::readTextFile (LPCTSTR lpszFilePath)
{
if(!lpszFilePath)
return L"";
FILE* fInput = _wfopen(lpszFilePath, L"rb");
if(!fInput)
return L"";
BYTE bBuffer[MAX_PATH] = {0};
int nLen = sizeof(bBuffer)/sizeof(bBuffer[0]);
if(fread(bBuffer, sizeof(BYTE), nLen, fInput) == 0)
{
fclose(fInput);
return L"";
}
CString csRet(bBuffer);
fclose(fInput);
return csRet;
}
Call function:
// Get Current folder -> get current
wchar_t szPathToExe[MAX_PATH];
GetModuleFileNameW(NULL, szPathToExe, MAX_PATH);
PathRemoveFileSpec(szPathToExe);
// Get license file
CString szPathToFile = (CString)szPathToExe + L"\\license.txt";
// Call readTextFile function
CString szContent = CMyReadFile::readFileText(szPathToFile);