Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am working with a text file on Delphi 7 and I need to fix errors in each line of the text file, but I am not sure what code to use.
I already have the code to open and display the text file in a rich edit but I don´t know where to go from there.
AssignFile(myFile, 'Test.txt');
Reset(myFile);
while not Eof(myFile) do
begin
ReadLn(myFile, text);
Richedit.lines.add(myFile);
end;
CloseFile(myFile);
end;
You really should not use the 1970's assignfile/reset etc calls anymore.
The following code will work:
RichEdit1.Lines.LoadFromFile(Filename);
When saving the file you do:
RichEdit1.Lines.SaveToFile(Filename);
If you want to examine the lines of text before injecting (so to spreak) them into the edit control do this:
var
SL: TStringList;
i: integer;
begin
SL:= TStringList.Create;
try
SL.LoadFromFile(Filename);
for i:= 0 to SL.Count -1 do begin
//Fixup is a function that reads the line and returns a corrected line.
SL[i]:= FixUp(SL[i]);
end;
RichEdit1.Lines.Assign(SL);
finally
SL.Free;
end;
end;
If you want to make sure the file is a pure text file, use a TMemo instead.
It works the same as above with TRichEdit.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 26 days ago.
Improve this question
According to the Parameter hints in Delphi 11.1 the MessageDlg should support custom button names defined in an array of strings in the last parameter.
e.g.
I cannot get this to work. I declared a constant 3 element array of string but the compiler claims there is no overloaded version of MessageDlg with this format.
Can anyone show me how this should work or is it an error in the parameter help.
no problem it works fine
var
CustomButtonCaptions: array of string;
begin
SetLength(CustomButtonCaptions , 5 );
CustomButtonCaptions[0] := 'Button-1';
CustomButtonCaptions[1] := 'Button-2';
CustomButtonCaptions[2] := 'Button-3';
CustomButtonCaptions[3] := 'Button-4';
CustomButtonCaptions[4] := 'Button-5';
case MessageDlg('MSG',TMsgDlgType.mtConfirmation,mbYesAllNoAllCancel,0,TMsgDlgBtn.mbClose,CustomButtonCaptions) of
0:
begin
end;
end;
end;
// Or
MessageDlg('MSG',TMsgDlgType.mtConfirmation,mbYesAllNoAllCancel,0,TMsgDlgBtn.mbClose, ['Button-1','Button-2','Button-3','Button-4','Button-5']);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I use Delphi 2009. I receive a text as String that looks like 'Визгунов (ранний) {VHS}'. Using online decoders, I was able to determine that it's actually Win-1251 codepage.
What should I do to restore it back to normal, in other words to make it readable again?
var s: string;
rbs: RawByteString;
begin
rbs := Utf8ToAnsi('Визгунов (ранний) {VHS}');
SetCodePage(rbs, 1251, false);
s := string(rbs); // s = 'Визгунов (ранний) {VHS}'
end;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to save an ADO connection string in an ini file, but it isn't being created. I've used the IniFiles and Forms units.
Here is the code I'm using in a data module.
procedure TDataModule2.DataModuleCreate(Sender: TObject);
var
FileIni : TIniFile;
begin
FileIni := TIniFile.Create(ExtractFilePath(Application.ExeName)+'atur.ini');
ADOConnection1.Connected:=False;
providerpath:='';
ADOConnection1.IsolationLevel:= ilReadCommitted;
providerpath:=FileIni.ReadString('Connection','CN',providerpath);
ADOConnection1.ConnectionString:= providerpath;
ADOConnection1.LoginPrompt:=False;
FileIni.Free;
end;
end.
The .ini file is not created on disk with TIniFile.Create(...);
if the file not exists ! the file will be created with the first
FileIni.writeString
FileIni.WriteBool
FileIni.WriteInteger
etc.
test if exists the .ini file, when not, then write important default values to the new file.
Do not write empty .ini file. which is just as useless as a non-existent file.
procedure TDataModule2.SetIniDefaults;
begin
FileIni.writeString('Connection','CN','defaultproviderpath');
// other defaults
end;
procedure TDataModule2.DataModuleCreate(Sender: TObject);
FileIni : TIniFile;
myIniPath:string;
begin
myIniPath := ExtractFilePath(Application.ExeName)+'atur.ini';
FileIni := TIniFile.Create(myIniPath);
providerpath:='DefaultValue'; // important default value
if NOT FileExists(myIniPath) then
begin
SetIniDefaults;
// after above line the file is created.
end;
....
providerpath:=FileIni.ReadString('Connection','CN',providerpath);
....
end;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im using a IF code in a IF code :
if Label1.Caption=Label2.Caption then
begin
Form1.Close;
Form2.Show;
end
if Label2.Caption=Label3.Caption then
begin
Form1.Close;
Form2.Show;
end
end;
I keep getting a error of missing operator of semicolon
Could someone please how to make a simple IF in an IF with more lines(begin/end) ?
You need to separate statements with a semi-colon.
begin
if ... then
begin
....
end; // <--- add missing semi colon
if ... then
begin
....
end; // <-- semi-colon not needed, but looks silly if omitted
end;
The two if statements in your code need to be separated. Pascal syntax requires statements to be separated by semi-colons. The official language guide has comprehensive coverage of this area of the language's syntax: http://docwiki.embarcadero.com/RADStudio/en/Declarations_and_Statements
One final point. You indicate twice in the question that the second if is nested inside the first. That is not so. They are separate statements, one after the other. So if both conditions evaluate to True you will call Form1.Close twice, and Form2.Show twice. It seems likely that's not your intention, but I'm not in a position to guess what your true intent is.
I think you mean to want this:
if Label1.Caption = Label2.Caption then
if Label2.Caption = Label3.Caption then
begin
Form1.Close;
Form2.Show;
end;
Which can be shortened into:
if (Label1.Caption = Label2.Caption) and (Label2.Caption = Label3.Caption) then
begin
Form1.Close;
Form2.Show;
end;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this code in Visual Basic that loads text from a file. I need to translate it into Delphi, but can't figure out how.
Open txtFile.Text For Binary As #1
file = Space(LOF(1))
Get #1, , file
Close #1
How do I get this code to work in Delphi?
If your Delphi is not too old, you can do this:
uses IOUtils;
...
S := TFile.ReadAllText('MyFileName.txt');
Don't invent your own solutions when the RTL provides something that's already good enough!
VB's LOF() returns a long integer that represents the length of a file. In this case, it's being used to allocate a string that is LOF spaces in size, and LOF() is returning the length of whatever filename is in txtFile.Text (which is probably an edit control).
Delphi doesn't need such techniques to simply load a text file into memory. The roughly equivalent code in Delphi if you're just wanting to get the file contents into a string:
function LoadTextFromFile(const FileName: string): string;
var
SL: TStringList;
begin
Result := '';
SL := TStringList.Create;
try
SL.LoadFromFile(FileName);
Result := SL.Text;
finally
SL.Free;
end;
end;
Using it:
var
MyText: string;
begin
MyText := LoadTextFromFile('C:\Temp\MyFile.txt');
// Do something with text
end;
It's even easier if you want to display the text for the user - just drop a TEdit, TMemo and a TButton on your form, double-click the button to create an OnClick event, and use code like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.LoadFromFile(Edit1.Text);
end;
For the purists out there, this is a close literal equivalent:
function GetStringFromFile(const FileName: string): AnsiString;
var
MS: TMemoryStream;
begin
Result := '';
MS := TMemoryStream.Create;
try
MS.LoadFromFile('D:\Temp\TestText.txt');
SetString(Result, PAnsiChar(MS.Memory), MS.Size);
finally
MS.Free;
end;
end;
You can use the TFileStream to open the file, specified in the TxtFile.text property
the '#1' is mean to be the file handle but it not neeed because you re using an OOP approach.
Open txtFile.Text For Binary As #1 mean to open the file not in text mode (as opening in the notepad) btu as binary so nothing could be translated to ascii characters. All will be read ad bytes not as characters
LOF(1) seems to return the length of the file (handle 1) and 'Space' will generate a string with the same number of space characters (#32) as the length of the files and assign it to variable 'file', then it will close the file handle.
So as an example:
var
FileContents: AnsiString; // Or an 'array of Byte' instead
Stream: TFileStream;
begin
Stream := TFileStream.Create(txtFile.Text, fmOpenRead);
try
SetLength(FileContents, Stream.Size);
if Length(FileContents) > 0 then
Stream.ReadBuffer(Pointer(FileContents)^, Stream.Size);
finally
Stream.Free;
end;
// Use FileContents as needed...
end;
Disclaimer notice:
I have not tested it yet; I'm not at my development computer right
now. So use it on your own risk.
What you have to determine is if you will read the data as bytes or
as characters.
The example assumes you're reading an ANSI text file
The example assumes you're reading a not so
large file, if it is large you have to read it in blocks.
The error handler is just an example how to manage an error
For more info, check TFileStream (and related classes) usage in the Delphi help documentation.