C++C++11C++14C++17Code SnippetLearn C++Syntax

Windows File Operations in Modern C++

There are a lot ways to operate on files in C and C++. In C programming is enough to use FILE, fopen(), fclose() operations. In C++ FileOpen().was enough to operate before. In Modern C++ because of multiplatform operating systems, global languages and for the other benefits of usage File Streams are better to operate better and more friendly. There is a good information about Using File Streams in docwiki of Embarcadero.

Let’s look at some of modern methods in C++ on read & write operations.

Saving List of Unicode Strings to a File (UTF8, UTF16, UTF32) in C++ (Writing Texts)
It is easy to use TStringLists which supports UnicodeStrings in default to save or load your text document in all languages. If you are new to UnicodeStrings please see this article about Unicode Strings in C++. This save & load methods are very useful on most of TStringList formed properties (Memo1->Lines, ListView1->Items etc.) of many components.

Loading List of Unicode Strings from a File in C++ (ReUTF8, UTF16, UTF32) in C++ (Reading Texts)

Reading File Lines in C++ by using Stream Reader (Text Reading)
In modern C++ we can easily read text files by using TStreamReader as in example below. This example reads each line to a UnicodeString, and it shows message when it finds “the” word.

Reading File Lines in C++ by using Stream Reader with OpenDialog (Text Reading)
This code below using OpenDialog component to select file and searching “the” word in all lines, and counting lines that has at least one this word.

Writing & Reading Data Files in C++ by using File Stream (Binaries, Structure Blocks etc.)

Stream classes have several methods for reading and writing files including any kind of data. These methods are distinguished by whether they


close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap

Worth reading...
Unicode Strings in C++ On Windows