-1

I'm trying to load the text from a text file to a synmemo by using

procedure TForm1.btn7Click(Sender: TObject);
begin
  if dlgOpen1.Execute then
    synm1.Lines.LoadFromFile(dlgOpen1.Files.Text);
end;

But as soon as I select a file i get this error:

Cannot open file "C:\Users\adria\Desktop\New Text Document.txt
". The filename, directory name, or volume label syntax is incorrect.

Component: https://github.com/TurboPack/SynEdit

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Newb101
  • 127
  • 3
  • 12
  • Looks like that file does not exist ......... – David Heffernan Feb 14 '17 at 16:56
  • 2
    Does the file exist, or are you trying to create a new one? It doesn't matter here but you should really be using dlgOpen1.FileName to open the selected file. dlgOpen1.Files is designed to handle a number of files, not just one. – Dsm Feb 14 '17 at 16:56
  • Does the `Files.Text` property contain the value you think it does? Use your debugging skills to inspect its value and confirm that your program is behaving the way you expect it to. – Rob Kennedy Feb 14 '17 at 17:02
  • thx to @Dsm I used dlgOpen1.FileName instead – Newb101 Feb 14 '17 at 17:18
  • The real lesson here though is nothing to do with the specifics, but can be found in @Rob's comment. The single most common solution for questions asked here is for the asker to improve their debugging skills. – David Heffernan Feb 14 '17 at 17:49
  • @JohnKugelman, please be careful when replacing images with text. In this case, your text conversion concealed a clue about the actual problem. – Rob Kennedy Feb 14 '17 at 19:34
  • Precisely, @John. Thanks! – Rob Kennedy Feb 14 '17 at 20:44
  • For crying out loud, if every question that could be answered by the vague directive that the OP should "improve their debugging skills" was removed from StackOverflow there wouldn't be much left. – Deltics Feb 14 '17 at 20:47
  • @Deltics If people worked harder to learn how to debug, they'd be able to solve more problems themselves. Or do you think that debugging is not an important skill? You think it's better to do the fishing rather than teach how to fish. And what are you talking about with regards "removed"? Who said anything about removal? I honestly don't think you understand this site at all. If you don't like how the site works, bring it up on meta. Or go somewhere else that better fits your needs. Alternatively, stay here, learn how the site works, and continue writing excellent answers! – David Heffernan Feb 15 '17 at 07:10

1 Answers1

1

The problem is in the use of the Files property of the dialog to access the selected filename.

The Files property is a list of strings intended for use when you have enabled multiple selection in the dialog and need to process more than one filename selected by the user.

The Text property of string list returns a formatted representation of all entries in that list with each entry delimited by an EOL character (or characters).

You might expect that where only a single file is involved that this Textproperty would contain only the name of that file. But in fact it also includes an EOL character. i.e. the filename you are trying to open by using this technique is actually:

'C:\Users\adria\Desktop\New Text Document.txt'#13#10

There was actually a clue to this in the way that the message was being displayed, with the closing quotes on a separate line as a result of that EOL.

The correct way to work with the selected filename depends on whether you are supporting multiple selection or single.

In the case of single selection (your case here) the simplest approach is to use the Filename property of the dialog:

if dlgOpen1.Execute then
  synm1.Lines.LoadFromFile(dlgOpen1.Filename);

For multiple selection you would use the Files property, but access each filename by index in the list:

if dlgOpen1.Execute then
  for i := 0 to Pred(dlgOpen1.Files.Count) do
  begin
    // Do something with each dlgOpen1.Files[i] ...
  end;
Deltics
  • 22,162
  • 2
  • 42
  • 70