Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] How to read text binary file using C++

Wednesday 30 September 2015
|
Read: Completed in minutes

[Tips] How to read text binary file using C++

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





How to read text binary file using C++ - Webzone Tech Tips Zidane




Text File license.txt


         https://learn-tech-tips.blogspot.com
         zidane
         huuvi168@gmail.com
         learntechtips
         learn-tech-tips
     
     




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);
     


Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about How to read text binary file using C++ , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips Zidane, all things tech tips web development  - I am Zidane, See you next time soon ✋✋✋✋


🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com