Recent

Author Topic: User written content in text file writer.  (Read 3285 times)

magicpants

  • Newbie
  • Posts: 3
User written content in text file writer.
« on: June 18, 2015, 06:11:58 pm »
Hello.

I have a school project to do with Lazarus, but I have a problem.

I have a Survey, so I want to store the information somehow, so I thought about creating a text file and storing it there, it wasn't that hard, until I wanted to write the information that user entered in the text file... (how do I do that?)

Code: [Select]
program project1;

{$mode objfpc}{$H+}

uses
  Sysutils,
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, Unit1
  { you can add units after this };

{$R *.res}

var
  f:textfile;

begin
  RequireDerivedFormResource := True;
  Application.Initialize;
  Application.CreateForm(TfrmSurvey, frmSurvey);
  Application.Run;
  assignfile(f, 'Submited.txt');
  rewrite(f);
  close(f);

  //

  writeln(f, 'Hello') + edtName;

end.     

I want to enter information that user entered via tEdit. It doesn't seem to work, what's wrong? I tried to use + edtName.Text; but it didn't work either.

Thank you!

P.S. Is it possible that the text file would be written when certain assigned button is clicked, and not when the application is closed?


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: User written content in text file writer.
« Reply #1 on: June 18, 2015, 07:03:48 pm »
Don't add code to your project's .lpr file.
Use the survey unit.
One very simple way to enter and save the data as text would be to use a memo control. Drop a memo on that form, called MemoSurvey. Add a Save button, and in the OnClick event of the Save button add some variation of this to suit your case:

Code: [Select]
procedure  TfrmSurvey.ButtonSaveClick(Sender: TObject);
begin
  MemoSurvey.Lines.SaveToFile('surveydata.txt');
end;

magicpants

  • Newbie
  • Posts: 3
Re: User written content in text file writer.
« Reply #2 on: June 18, 2015, 07:37:13 pm »
Thank you for your answer.

But I get an error when I try to plug this code in.

Code: [Select]
unit1.pas(248,13) Error: identifier idents no member "Lines"

I plug in:

Code: [Select]
frmSurvey.Lines.SaveToFile('surveydata.txt');
What's up with that 'Lines'?

Michl

  • Full Member
  • ***
  • Posts: 226
Re: User written content in text file writer.
« Reply #3 on: June 18, 2015, 09:22:12 pm »
I hope, this page is helpful for you: http://wiki.freepascal.org/TMemo#Save_and_load
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: User written content in text file writer.
« Reply #4 on: June 18, 2015, 11:29:22 pm »
Thank you for your answer.

But I get an error when I try to plug this code in.

Code: [Select]
unit1.pas(248,13) Error: identifier idents no member "Lines"

I plug in:

Code: [Select]
frmSurvey.Lines.SaveToFile('surveydata.txt');
What's up with that 'Lines'?

you need
Code: [Select]
  frmSurvey.MemoSurvey.Lines.SaveToFile('surveydata.txt');
Replace MemoSurvey with the name you've given to your memo (if you use the default it will be Memo1).

magicpants

  • Newbie
  • Posts: 3
Re: User written content in text file writer.
« Reply #5 on: June 20, 2015, 09:00:54 am »
Thank you guys  :)

Although, I have a question again. Is it possible to make the TMemo only edit the text file, not to save it? That I could save answers from multiple people in one .txt file? Or maybe that if there's already a file for example 'survey.txt' it would create 'survey1.txt' and so on.

Thank you :)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: User written content in text file writer.
« Reply #6 on: June 20, 2015, 11:51:54 am »
As you extend the functionality of your project, you need more and more the features provided by a relational database.
So you have a design decision: do I start using a database (programmed by someone else - so I will need to learn how to program and use it), or do I roll my own database-type functionality to store and retrieve increasingly complex data?
Either route will be a useful learning experience.

 

TinyPortal © 2005-2018