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.
1 2 3 4 5 6 7 8 |
TStringList *strlist = new TStringList; strlist->Add("Hello"); strlist->Add("How are you?"); ... strlist->SaveToFile(L"D://test.txt"); strlist->SaveToFile(L"D://test2.txt", TEncoding::UTF8); |
Loading List of Unicode Strings from a File in C++ (ReUTF8, UTF16, UTF32) in C++ (Reading Texts)
1 2 3 |
TStringList *strlist = new TStringList; strlist->LoadFromFile(L"D://test.txt"); |
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.
1 2 3 4 5 6 7 8 9 10 11 |
TStreamReader *SReader; UnicodeString us; SReader = new TStreamReader(OpenDialog1->FileName, TEncoding::UTF8); do { us=SReader->ReadLine(); if(us.Pos(L"the")) ShowMessage(L"Word found in this line"); } while(!SReader->EndOfStream); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TStreamReader *SReader; UnicodeString us; int count=0; OpenDialog1->FileName=""; OpenDialog1->Execute(); if(OpenDialog1->FileName.Length()>0) { SReader = new TStreamReader(OpenDialog1->FileName, TEncoding::UTF8); do { us=SReader->ReadLine(); if(us.Pos(L"the")>0) count++; } while(!SReader->EndOfStream); } ShowMessage(IntToStr(count)+L" Lines Found."); |
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
- Return the number of bytes read or written.
- Require the number of bytes to be known.
- Raise an exception on error.
- http://docwiki.embarcadero.com/RADStudio/Sydney/en/Using_Streams_to_Read_or_Write_Data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <vcl.h> #include <windows.h> #include <tchar.h> #include <stdio.h> struct st_dat // let's define a default structure { int X, Y; }; struct st_dat dat; // let's create a structure //--- WRITE A STRUCTURE BLOCK --- void write_file(String str) { TFileStream* fs; try { fs = new TFileStream(str, fmCreate | fmOpenReadWrite); // Open File Stream if(fs) { fs->Write(&dat, sizeof(dat)); // Write structure block in its size } } __finally { fs->Free(); } } //--- READ A STRUCTURE BLOCK --- void read_file(String str) { TFileStream* fs; try { fs=new TFileStream(str, fmOpenRead ); if(fs) { fs->Read(&dat, sizeof(dat)); } } __finally { fs->Free(); } } int _tmain(int argc, _TCHAR* argv[]) { dat.X=960; // let's add some data dat.Y=540; write_file(L"D:\\test.dat"); read_file(L"D:\\test.dat"); std::cout << dat.X << "," << dat.Y << '\n'; //ShowMessage(sizeof(dat.X)); getchar(); return 0; } |