C++ Builder Tutorials

C++Builder - Database tutorial, part 4

At last, we shall load / save our articles database from / to a text file.

Start by downloading the source code Articles4.zip and next unzip it to folder \CppProjects\Articles4.


Saving to a file

We added a variable ArticlesFile that shall contain the name of a text file.
At the start of the application, we set this variable to folder of the exe-file plus "Articles.txt".
Next, the function LoadArticles tries to load from this file (see the FormCreate handler).

Of course, the file specified in ArticlesFile will not be found when you run your program for the first time.
We'll use our application to add some articles. So, let's firstly develop a function that saves to a file.

The function SaveArticles() is called right before our program terminates (see the FormDestroy handler).
Here's how SaveArticles works:

  • Create a temporary TStringList, named SL
  • for every item in stringlist Articles, we assemble a line S formatted as follows:  ID;Name;PriceString;StockString
    For example:
    0001;chair;25.50;80
  • Add S to SL
  • Save SL to a file
void TForm1::SaveArticles()
{
  int index;
  String S;
  TStringList *SL;
  SL = new TStringList;
  for (index = 0; index < Articles->Count; index++) {
    TArticle *Art = static_cast(Articles->Objects[index]);
    S = Articles->Strings[index] + ";";
    S = S + Art->Name + ";";
    S = S + FormatFloat("0.00", Art->Price) + ";";
    S = S + IntToStr(Art->Stock);
    SL->Add(S);
  }
  SL->SaveToFile(ArticlesFile);
  delete SL;
}

Loading from a file

Now, let's have a look at LoadArticles().
Here's how it works:

  • Create a temporary TStringList, named SL
  • Load SL from a file
  • For every item in SL:
    • Create an object named Art
    • Read the item of SL into variable S
    • Find the position of the first ";" in S
    • In ID, store a substring of S, starting at position 1 and ending right before the ";"
    • Delete the first part of S
    • Repeat for the article name, but store it in Art->Name
    • Repeat for the price, convert to float and store in Art->Price
    • Repeat for the stock amount, convert to an integer and store in Art->Stock
  • Finally, update the grid and free SL

Cleaner code for updating the user interface

To UpdateUI, we added DBModeNew as a "default parameter", as can be seen in Unit1.h:
  void UpdateUI(String Stat, String DBModeNew = "B");

In Unit1.cpp, we see:

void TForm1::UpdateUI(String Stat, String DBModeNew)
{
  stStatus->Caption = Stat;
  DBMode = DBModeNew;
  // more statements...
}

This simplifies the code for calling UpdateUI: if only one parameter is specified, DBModeNew is set to the default value "B" (for "Browsing"). Example in btnCancelClick:
UpdateUI(" No article added or modified ");

Function UpdateGrid has been improved by adding a parameter RowNo, to indicate which row of the grid must be highlighted:

void TForm1::UpdateGrid(int RowNo)
{
  int index;
  Grid->RowCount = Articles->Count + 1;
  for (index = 0; index < Articles->Count; index++)
  {
    TArticle *Art = static_cast<tarticle*>(Articles->Objects[index]);
    Grid->Cells[0][index + 1] = Articles->Strings[index];
    Grid->Cells[1][index + 1] = Art->Name;
    Grid->Cells[2][index + 1] = FormatFloat("0.00", Art->Price);
    Grid->Cells[3][index + 1] = IntToStr(Art->Stock);
  }
  if (RowNo > (Grid->RowCount - 1))
    RowNo = 0;
  Grid->Row = RowNo;
}

Table of contents

  1. Text Database
  2. DB Browser
  3. Edit and Delete
  4. Load / Save
  5. Validating