[Builder 6] Save Label content to .txt file

Pages: 123
Hello.
I want to save Label content to .txt file
1
2
3
4
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    Label1 -> Caption -> SaveToFile ("c/Timer/plik.txt");
}

Error "Pointer to structure required on left side of -> or ->"
When I create a pointer
1
2
3
AnsiString Content = Label1 -> Caption;
AnsiString *ContPoint = &Content;
Label1 -> *ContPoint -> SaveToFile ("c/Timer/plik.txt");

Error "Member identifier expected" and "'ContPoint' is assigned a value that is never used"
What's wrong with it?
Last edited on
AnsiString doesn't have a function to save itself to a file.
Either use a TFileStream or the C++ ofstream class.
You meant sth like this?
1
2
3
    TFileStream Content = Label1 -> Caption;
    TFileStream *ContPoint = &Content;
    Label1 -> *ContPoint -> SaveToFile ("c/Timer/plik.txt");

It's not working. There are still error:
Cannot convert AnsiString to TFileStream
Member identifier expected
ContPoint is assigned a value that is never used
I think the easiest is:
1
2
3
4
5
6
7
8
9
#include <fstream>

ofstream dest("C:\\Timer\\plik.txt");
if (!dest)
{
   // handle error 
}
dest << Label1->Caption.c_str();


Some tutorials about the CBuilder and the strings.

http://www.functionx.com/bcb/topics/strings.htm

http://www.functionx.com/bcb/
Ok, thanks.
The problem is that it doesn't create folder (Timer). Folder has to exist. I have to create the folder before I will try to save something there. How to solve this problem? What is the way that it can make folder by itself? (sorry for my english if it is not understandable).
Ok, but before I will use this function I should check whether folder already exist or not. How to check it?

I create sth like this but it doesn't work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    if (PathFileExists("C:\\Timer"))
    {
       ofstream file("C:\\Timer\\sesja.txt");
    }
    else
    {
       CreateDirectory("C:\\Timer" ,NULL);
       ofstream file("C:\\Timer\\sesja.txt");
    }
    file << Label1->Caption.c_str();      //Improper use of typedef 'file'

    file.close();                         //Improper use of typedef 'file'
}
Last edited on
I think CBuilder has a function called DirectoryExists. I am not certain - I stopped using it many years ago.
Now I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
if(DirectoryExists("C:\\Timer"))
    {
       ofstream file("C:\\Timer\\sesja.txt");
    }
    else
    {
       CreateDirectory("C:\\Timer" ,NULL);
       ofstream file("C:\\Timer\\sesja.txt");
    }
    file << Label1->Caption.c_str();  //Improper use of typedef 'file'

    file.close();    //Improper use of typedef 'file' 

As you can see there are still same error as in earlier code.
1
2
3
4
5
6
7
8
9
10
11
12
13
    ofstream file;
    if(DirectoryExists("C:\\Timer"))
    {
       file.open("C:\\Timer\\sesja.txt");
    }
    else
    {
       CreateDirectory("C:\\Timer" ,NULL);
       file.open("C:\\Timer\\sesja.txt");
    }
    file << Label1->Caption.c_str();

    file.close();
Last edited on
Or, without the 'else'
1
2
3
4
5
6
7
8
9
10
    if (!DirectoryExists("C:\\Timer"))
    {
       CreateDirectory("C:\\Timer" ,NULL);
    }
    
    ofstream file("C:\\Timer\\sesja.txt");
    
    file << Label1->Caption.c_str();

    file.close(); 
Now I wanna send variables from first function to second.
1
2
3
4
5
6
7
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
int one, two, three;
//From this function
//Sth like this?
FormClose(one, two, three)
} 


1
2
3
4
void __fastcall TForm1::FormClose(int onee, int twoo, int threee, TObject *Sender, TCloseAction &Action)
{
//To this function
}
Last edited on
You get most you need, though something seems to be missing in the first function it is required to call the second function :
void __fastcall TForm1::FormClose(int onee, int twoo, int threee, TObject *Sender, TCloseAction &Action)

Could you explain what (TCloseAction &Action) does in your code?
This will not work.
void __fastcall TForm1::FormClose is a predefined event handler. It will not work to just change the signature. This event is called by the VCL when the form is going to be closed.

What do you actually want to do? I am sure there is an easy way.
Generally I want to make a program which will calculate the time we spend on computer:
1.During one session - I already did
2.In total (sum up our all sessions) - I try to do that
void __fastcall TForm1::Timer1Timer - calculate time
void __fastcall TForm1::FormClose - create file .txt and save the time therein
I want to send variables from 1st function to 2nd function and save them in the .txt file. At the second time program has to add 1st time + 2nd time to show up total time we spend on computer. To add variables they need to be int type (in this case) - not string.
You can create variables in your form class so all member functions can access them.
In your timer event you set the values and in the form close event you save them to disk.
No need to pass the variables around.
I created this...
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
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    s++;

    AnsiString hrs, mins, secs;

    hours = s / 3600;
    hrs = IntToStr (hours);
    if (hours < 10) hrs = "0"+hrs;

    minutes = (s - hours * 3600) / 60;
    mins = IntToStr (minutes);
    if (minutes < 10) mins = "0"+mins;

    seconds = s - hours * 3600 - minutes * 60;
    secs = IntToStr (seconds);
    if (seconds < 10) secs = "0"+secs;

    Label1 -> Caption = hrs+":"+mins+":"+secs;

    HrsAll = hours + HrsAll;
    MinsAll = minutes + MinsAll;
    SecsAll = seconds + SecsAll;
}

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    AnsiString hrs, mins, secs;

    hrs = IntToStr (HrsAll);
    if (HrsAll < 10) hrs = "0"+hrs;

    mins = IntToStr (MinsAll);
    if (MinsAll < 10) mins = "0"+mins;

    secs = IntToStr (SecsAll);
    if (SecsAll < 10) secs = "0"+secs;

    if (!DirectoryExists("C:\\Timer"))
    {
       CreateDirectory("C:\\Timer", NULL);
    }

    ofstream file1("C:\\Timer\\sesja.txt");

    file1 << Label1->Caption.c_str();

    file1.close();

    ofstream fileAll("C:\\Timer\\lacznie.txt");

    fileAll << hrs.c_str() << ":" << mins.c_str() << ":" << secs.c_str();

    fileAll.close();
}

... and it doesn't work. Focus on "sum up/in total" part.
1.Start program for 5 seconds. Close
1.1.In .txt file I see "00:00:15"
2.Start program 2nd time for 5 seconds. Close
2.1.In .txt file I see "00:00:15". Same as 1st time. It should be 00:00:30.

I don't remember for how long I start program earlier but I saw "00:00:210". Interesting.
Could you tell me why?
Your code in the Timer1Timer() function is calculating the sum of a series of integers.

1+2+3+4+5 = 15

1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20 = 210
@Edit
I have new code and it doesn't work. Actually it works, but it doesn't create new .ini file.
.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
 
#include <IniFiles.hpp> //TIniFile
 
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  TIniFile *IniFile;
  IniFile = new TIniFile("D:\\timer.ini");
  seconds = IniFile->ReadInteger("Timer","seconds",0);
  delete IniFile;
 
  Label1->Caption = FormatSeconds(seconds);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  seconds++;
 
  Label1->Caption = FormatSeconds(seconds);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnClose(TObject *Sender, TCloseAction &Action)
{
  TIniFile *IniFile;
  IniFile = new TIniFile("D:\\timer.ini");
  IniFile->WriteInteger("Timer","seconds",seconds);
  delete IniFile;
}
//---------------------------------------------------------------------------
 
AnsiString TForm1::FormatSeconds(int sec)
{
  int hrs, mins, secs;
  AnsiString retVal;
 
  hrs = sec / 3600;
  mins = (sec - hrs * 3600) / 60;
  secs = sec - hrs * 3600 - mins * 60;
 
  retVal.printf("%02d:%02d:%02d",hrs,mins,secs);
  return retVal;
}
//--------------------------------------------------------------------------- 


.h
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
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include &lt;Forms.hpp&gt;
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
  TLabel *Label1;
  TTimer *Timer1;
  void __fastcall Timer1Timer(TObject *Sender);
  void __fastcall OnClose(TObject *Sender, TCloseAction &Action);
private:    // User declarations
  int seconds;
 
  AnsiString FormatSeconds(int sec);
public:        // User declarations
  __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif 


Why?
Try a different location. Some Windows versions don't allow to write files in the root folder of a drive.
Pages: 123