?
Solved

Data Types

Posted on 2007-10-06
19
Medium Priority
?
289 Views
Last Modified: 2010-04-05
I need to store a large string in a file and a string is limited to 255 chars. I could need upto a 1000.  What data type could I use to store the string? It also needs to be compatible with the memo component.
0
Comment
Question by:ding-dong
  • 7
  • 4
  • 3
  • +3
19 Comments
 
LVL 13

Assisted Solution

by:rfwoolf
rfwoolf earned 160 total points
ID: 20027011
It depends on the database language. Each kind supports different data types. Each data type subsequently has different limitations, e.g. a LongInteger in paradox might have different limits to a LongInteger on FoxPro

In Paradox for example, there is a Memo field which stores 255 characters in the database itself, and the rest stored in a separate accompanying file, I think labeled SOMETHING.MB (this is for efficiency purposes - you really don't want to store zillions of characters directly in the database).

What database language are you using?
0
 

Author Comment

by:ding-dong
ID: 20027067
Well I did post this in the delphi section, didnt I?

Anyway, im using delphi own files (text files). Heres a link that kind of explains how their used: http://www.delphibasics.co.uk/Article.asp?Name=Files
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20027104
Oh I see, you're not using a proper 'database' per se, you're using flat-files to store your data. That's not a relational database but depending on what your 'data storage needs' it should be fine.

According to my understanding, on the Win32 platform, the compiler interprets "string" (when it appears without a bracketed number after it) as AnsiString. AnsiString has a maximum length of ~2^31 - much more than 255 characters.
A "shortstring" however only has 255 characters, but that string type is only kept for back-ward compatability, in other words by saying 'string' you're automatically creating an AnsiString.

You can find more info if you search in delphi for 'string - data types'.

So try just using data types of just 'string' and see if it will let you store more than 255 characters.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ding-dong
ID: 20027112
Nope it doesn't work. When you define a file it wont let you leave it without a length in brackets.

Heres the code you suggested for my file:

  faq = record
    question: string[255]; //more than enough chars
    answer: string;
    end;
    faqs = file of faq;

And this is the error I get:
[Error] Unit11.pas(14): Type 'faq' needs finalization - not allowed in file type

However if i used this code, the project builds with no errors:

  faq = record
    question: string[255]; //more than enough chars
    answer: string[255];
    end;
    faqs = file of faq;
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20027141
Hmm I'm busy trying to figure this out myself.
According to the delphi help file on records, it (somewhere) says
"The types must not be long strings, dynamic arrays, variants (that is, Borland.Vcl.Variants.Variant types), or interfaces, nor can they be structured types that contain long strings, dynamic arrays, variants, or interfaces; but they can be pointers to these types."
..I'll do some tests.
0
 
LVL 6

Expert Comment

by:bokist
ID: 20027157
Try with TRichEdit, something like this:
var
  Redit : TRichEdit;
begin
   Redit := TRichEdit.Create(Form1);
   Redit.Width := 500;
   Redit.Visible := False;
   Redit.Parent := Form1;
   Redit.MaxLength := $7FFFFFF0;
   Redit.lines.Clear;
   Redit.lines.Append(question);
   Redit.lines.Append(answer);
use RichEdit with  Memo:
    i := 0;
    repeat
          if length(trim(StrPas(PCHAR(Redit.lines[i])))) > 0 then
             Memo.lines.Append(Redit.lines[i]);
          inc(i);
    until i = Redit.lines.Count;

0
 

Author Comment

by:ding-dong
ID: 20027249
Ok, but I cant store the string thats more than 255 chars so theres not much need for that.

Im trying to store a string in a text file thats more than 255 chars in length.
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20027291
why couldn't you? The trouble is I think you should be using a txt file instead of a RichEdit - but other than that bokist's solution should work for you.
0
 
LVL 6

Accepted Solution

by:
bokist earned 280 total points
ID: 20027315
Hi,

if you are using simple text file, here is the example:
var
   f : TextFile;
   FileName :string;
   Redit : TRichEdit;
begin
   Redit := TRichEdit.Create(Form1);
   Redit.Width := 10000;
   Redit.Visible := False;
   Redit.Parent := Form1;
   Redit.MaxLength := $7FFFFFF0;
   Redit.lines.Clear;
   Filename := 'C:\some_file.csv';
   if FileExists(FileName)  then  DeleteFile(FileName);
   AssignFile(f, FileName);
   if FileExists(FileName) = False   then   Rewrite(f)
   else                                                     Append(f);
   Redit.lines.Append('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
                                 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' +
                                 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc' +
                                 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' +
                                 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' +
                                 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' +
                                 'gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg' +
                                  'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh' +
                                  'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii' +
                                  'jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj' +
                                  'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk' +
                                  'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm' +
                                  'nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn');
  Writeln(f, Redit.lines.text);
  CloseFile(f);
and here is the file:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn



0
 

Author Comment

by:ding-dong
ID: 20027319
Ok, maybe I didnt understand what he was doing when I read it, and I still dont get it now if that apparently would work, could someone explain how it works?
0
 
LVL 6

Expert Comment

by:bokist
ID: 20027341
Hi,  
I can't give you further explanation, please replace letter-strings with your data and test the solution.
regards,
 steve
0
 
LVL 7

Expert Comment

by:Cynna
ID: 20027771
I think you'll have to explain what you need a bit more...

- Do you want to store a single string record? In that case a simple WriteLn(F, VeryLongString) will do (and a lot of other ways, tipically TStringList approach)
- Or are you storing several long strings? If so, are they stored in memory (i.e. array) before you save them? Or do you want to write them "as they come", or...?
- How does TMemo come into play? If you are just using Memo, then simple Memo1.Lines.SaveToFile saves complete memo contens to file, while Memo1.Lines.LoadFromFile loads it back to memo.

As you can see, there are lot of ways to save/load strings, but this depends on your exact need.
Maybe if you explain what exactly are you trying to do (not just "I want to save a long string"), we could propose more options...
0
 

Author Comment

by:ding-dong
ID: 20027792
Sorry ill try to clear it up. Well im trying to store a large string into a record. This is what I have so far:

faq = record
    question: string[255]; //more than enough chars
    answer: string[255]; //this is the problem
    end;
    faqs = file of faq;

The answer part needs to be more than 255 chars which is the maximum its allowing me to have.

Tmemo comes into play when I display the string in my application, it takes the string from the record and displays it there. I can't save and load this as the file is meant to contain more than one record.

Strings are written to the record "as they come", when the user enters them.
0
 
LVL 7

Assisted Solution

by:Cynna
Cynna earned 280 total points
ID: 20027941
OK, this pair of functions will write(append) and read the  "Tfaq" record to/from file. When reading, you can specify arbitrary record number, but you have to mind not to read more records then you have written.
I also post a demo which assumes you have Button1 and Memo1 placed on a form. Just copy/paste the following code, and click Button1.


type TFaq= record Q, A: String; end;

procedure WriteFAQ(FileName: String; FAQ: TFaq);
var F: TextFile;
begin
  AssignFile(F, FileName);
  if FileExists(FileName) then Append(F) else Rewrite(F);
  try
    // Eliminate CR/NL chars in text:
    FAQ.A:=StringReplace(FAQ.A, #10, Chr(1), [rfReplaceAll]); FAQ.A:=StringReplace(FAQ.A, #13, Chr(2), [rfReplaceAll]);
    FAQ.Q:=StringReplace(FAQ.Q, #10, Chr(2), [rfReplaceAll]); FAQ.Q:=StringReplace(FAQ.Q, #13, Chr(2), [rfReplaceAll]);
    WriteLn(F, FAQ.Q); WriteLn(F, FAQ.A);
  finally
    CloseFile(F);
  end;
end;

function ReadFAQ(FileName: String; RecordNo: Integer; var FAQ: TFaq): Boolean;
var F: TextFile;
    s: String;
    i:Integer;
    procedure FixCRNL;
    begin // Restore CR/NL chars
      s:=StringReplace(s, Chr(1), #10,  [rfReplaceAll]); s:=StringReplace(s, Chr(2), #13,  [rfReplaceAll]);
    end;
begin
  Result:=FALSE; FAQ.Q:=''; FAQ.A:='';
  AssignFile(F, FileName); if not FileExists(FileName) then Exit;
  Reset(F);
  try
      for i:=1 to RecordNo-1 do begin // Move sequentially to the record "RecordNo"
          if Eof(F) then Exit;
          ReadLn(F, s); ReadLn(F, s);
      end;
      ReadLn(F, s); FixCRNL; FAQ.Q:=s;
      ReadLn(F, s); FixCRNL; FAQ.A:=s;
      Result:=TRUE;
  finally
    CloseFile(F);
  end;
end;

//------- DEMO -----------------

procedure TForm1.Button1Click(Sender: TObject); // Demo - needs Button1 and Memo1 on the form
var i: Integer;
    FAQRecord: TFaq;
begin
  // Write 10 questions and answers, one by one:
  for i:=1 to 10 do begin
      // fill FAQRecord with some text:
      FAQRecord.Q:='Question '+IntToStr(i)+': is this OK?'; FAQRecord.A:='Answer to question no.'+IntToStr(i)+' can be arbitrarilly long now...';
      // Add it to FAQs.txt file
      WriteFAQ('FAQs.txt', FAQRecord);
  end;
  // Read 3. question/answer and display it in Memo1
  if not ReadFAQ('FAQs.txt', 3, FAQRecord) then begin
     Memo1.Lines.Add('Error reading the FAQs.txt file!'); Exit;
  end;
  with Memo1.Lines do begin
       Add('');
       Add('3. Q: '+FAQRecord.Q);
       Add('3. A: '+FAQRecord.A);
  end;
end;
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 280 total points
ID: 20028149
a small sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const AnswerMaxSize = 4000; //adjust this to your MaxLenbth

type
  tfaq = record
          question : String[255];
          answer : array[0..AnswerMaxSize] of char;  //answer is an array if char
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    faq : tfaq;
    faqs : file of tfaq;
    procedure FaqToControls;
    procedure ControlsToFaq;
    procedure setAnswer(aAnswer : String);
    function getAnswer : String;

  public
    { Public-Deklarationen }
  end;




var
  Form1: TForm1;

implementation

{$R *.dfm}

//Conversion
procedure TForm1.setAnswer(aAnswer : String);
var
  p : PChar;
begin
  strpcopy(PChar(@faq.answer),copy(aAnswer,1,AnswerMaxSize-1));
end;

function TForm1.getAnswer : String;
begin
  result := String(PChar(@Faq.answer));
end;

//display
procedure TForm1.FaqToControls;
begin
  memo1.lines.clear;
  memo1.lines.add(faq.question);
  memo2.lines.clear;
  memo2.lines.add(getAnswer);
end;

//edited back
procedure TForm1.ControlsToFaq;
begin
  faq.question := memo1.Text;
  setAnswer(memo2.text);
end;


procedure TForm1.FormCreate(Sender: TObject);
const cFileName = 'c:\faqs.dat';
begin
  assignfile(faqs,cFileName);
  if fileexists(cFileName) then
    reset(faqs)
  else
    rewrite(faqs);
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
  closeFile(faqs);
end;

//read first entry
procedure TForm1.Button1Click(Sender: TObject);
begin
  seek(faqs,0);
  if not(eof(faqs)) then
  begin
    read(faqs,faq);
    FaqToControls;
  end
  else
    showmessage('Sorry, nothing stored yet');
end;

//Display next entry
procedure TForm1.Button2Click(Sender: TObject);
begin
  if not(eof(faqs)) then
  begin
    read(faqs,faq);
    FaqToControls;
  end
  else
    showmessage('Sorry, Last entry');
end;

//Save on End of File
procedure TForm1.Button3Click(Sender: TObject);
begin
  ControlsToFaq;
  seek(faqs,filesize(faqs));
  write(faqs,faq);
end;

end.

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 20033064
any results? something not clear?

meikl ;-)
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 20033539
    Hi!

Why not use Streams ? http://www.torry.net/pages.php?id=520

Regards,
   Tomas Helgi
0
 

Author Comment

by:ding-dong
ID: 20034782
Well im having a play around with kretzschmar's code at the moment... ill report back soon!
0
 

Author Comment

by:ding-dong
ID: 20044932
Ok. I did it a different way. I saved the path to a file in the answer field of the record, and used memo1.lines.savetofile and loadfromfile to save more than 255 chars which is a nice workaround.

I'll split points, thanks!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question