ding-dong
asked on
Data Types
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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;
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;
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.Varia nt 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.
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.Varia
..I'll do some tests.
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(questio n);
Redit.lines.Append(answer) ;
use RichEdit with Memo:
i := 0;
repeat
if length(trim(StrPas(PCHAR(R edit.lines [i])))) > 0 then
Memo.lines.Append(Redit.li nes[i]);
inc(i);
until i = Redit.lines.Count;
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(questio
Redit.lines.Append(answer)
use RichEdit with Memo:
i := 0;
repeat
if length(trim(StrPas(PCHAR(R
Memo.lines.Append(Redit.li
inc(i);
until i = Redit.lines.Count;
ASKER
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.
Im trying to store a string in a text file thats more than 255 chars in length.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Hi,
I can't give you further explanation, please replace letter-strings with your data and test the solution.
regards,
steve
I can't give you further explanation, please replace letter-strings with your data and test the solution.
regards,
steve
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...
- 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...
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
any results? something not clear?
meikl ;-)
meikl ;-)
ASKER
Well im having a play around with kretzschmar's code at the moment... ill report back soon!
ASKER
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!
I'll split points, thanks!
ASKER
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