Recent

Author Topic: ‘Absolute path’ and string path  (Read 2717 times)

stephanos

  • Jr. Member
  • **
  • Posts: 81
‘Absolute path’ and string path
« on: August 07, 2025, 11:17:50 pm »
Dear All

I want to open a plain text file called: ThePlayListFile.m3u.  It is located on a HDD of a Kubuntu desktop.  In addition, in the future it will be on a MP3 player attached to the Kubuntu or other Kubuntu computer, and the programme will be on the Kubuntu computer.  So the path will and can be different each time I try to open the file.

It is a GUI programme.  The intention is to use a FileNameEdit set to select only one file.  After the file is selected the whole path/filename is written to a global string (m3uPathFile).  To test it is captured correct, I output the global variable to the caption of a label, success:
“/home/stephanos/MyFiles-Kingston/Delphi-Lazarus/Programmes/Linux/64/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u”

Next I want to open that file, and the following do not work:

Code: Pascal  [Select][+][-]
  1. Memo1.Enabled := True;
  2. m3uContent := ReadFileToString(m3uPathFile); //
  3. Memo1.Lines.Text := m3uContent;
and
Code: Pascal  [Select][+][-]
  1. Memo1.Enabled := True;
  2. fileContent := TStringList.Create;
  3. fileContent.LoadFromFile(m3uPathFile);
  4. m3uContent := fileContent.Text; fileContent.Free;
  5. Memo1.Lines.Text := m3uContent;

The latter gives me an error message:
"Unable to open file "/home/stephanos/MyFiles-Kingston/Delphi-Lazarus/Programmes/Linux/64/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u"  No such file or directory. Press OK to ignore and risk data corruption. Press Abort to kill the program.”

With both methods I can substitute the string for an absolute path:
Code: Pascal  [Select][+][-]
  1. m3uContent := ReadFileToString(/home/stephanos/MyFiles-Kingston/Delphi-Lazarus/Programmes/Linux/64/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u)
or
Code: Pascal  [Select][+][-]
  1. fileContent.LoadFromFile(/home/stephanos/MyFiles-Kingston/Delphi-Lazarus/Programmes/Linux/64/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u)
and it does work

Because the path will change when the mp3 player is plugged into each computer, I have to find the path to the file on each occasion.

I cannot rely on the m3u file being in the same location as the programme because the m3u player does not allow me to execute a Linux programme from the player. So I am confined to this method.

But I cannot use a string containing the path/filename to open the file.
I am using a global variable as I want to use the path part of the string to open an error messages text file which is in the same folder as the m3u file.

I have drawn a blank.

wp

  • Hero Member
  • *****
  • Posts: 13412
Re: ‘Absolute path’ and string path
« Reply #1 on: August 07, 2025, 11:55:46 pm »
I don't see a difference between the path in the variable and the literal arguments in the working code... Is the effect reproducible? I mean: Could it be that in the failed case the file incidentally is locked by another program (the player?)?

jamie

  • Hero Member
  • *****
  • Posts: 7599
Re: ‘Absolute path’ and string path
« Reply #2 on: August 08, 2025, 12:14:27 am »
is it possible you need to use a WIDESTRING/UnicodeString ?

Jamie
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7599
Re: ‘Absolute path’ and string path
« Reply #3 on: August 08, 2025, 01:59:49 am »
I just looked at the source code for that function and I don't see anything wrong with it however; it is using a UTF8 function so is it possible you don't have {$H+} enabled in your source at the top?
The only true wisdom is knowing you know nothing

stephanos

  • Jr. Member
  • **
  • Posts: 81
Re: ‘Absolute path’ and string path
« Reply #4 on: August 08, 2025, 10:59:50 am »
Dear All

Thanks for the replies

{$H+} is present and in red
unit SimpleFormsUnit;
{$mode objfpc}{$H+}

Also, I tried different strings
m3uPathFile : UnicodeString; // same issue
m3uPathFile : WideString;     //Same issue
m3uPathFile : string;            // same issue

While open in Kate I also looked to see if there is any evidence that the file is locked, such as a second listing of the file name with a different ending - no sign.  I studied the properties of the file while open in Kate and not open in Kate - no evidence.  I made a new text file and copied the content over - same problem

I read a lot about the four types of string and the different way they take up memory per character.

Any further help needed and appreciated


CM630

  • Hero Member
  • *****
  • Posts: 1641
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: ‘Absolute path’ and string path
« Reply #5 on: August 08, 2025, 12:32:57 pm »
Maybe trying FileExistsUtf8 and FileExists with and without using a variable will give some hint.
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

stephanos

  • Jr. Member
  • **
  • Posts: 81
Re: ‘Absolute path’ and string path
« Reply #6 on: August 09, 2025, 12:10:03 am »
Dear All

Thanks

I used a simple if with FileExists.  When I used the absolute path the file did exist.  When I  used a sting it did not

Code: Pascal  [Select][+][-]
  1. if FileExists(m3uPathFile) then
  2. begin
  3.       Label3.Caption := 'File present';
  4.  end
  5.  else
  6.  Begin
  7.       Label3.Caption := 'File NOT present';
  8.  end;

So I moved the project file to a different location where the path to the target file is:
   "/home/stephanos/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u"
a much shorter path.  However, the result is the same, the file is NOT present.

I am not sure what that means so if anyone can help?

Thanks

wp

  • Hero Member
  • *****
  • Posts: 13412
Re: ‘Absolute path’ and string path
« Reply #7 on: August 09, 2025, 12:22:30 am »
Maybe you have some buffer overrun somewhere which damages the string variable? Place a breakpoint at the line in which the crash happens and determine the current value of the variable.

If not, prepare a small project which demonstrates the issue. I am sure that some of us are eager to test this issue with the compiler.

stephanos

  • Jr. Member
  • **
  • Posts: 81
Re: ‘Absolute path’ and string path
« Reply #8 on: August 09, 2025, 01:17:38 am »
Der All

Than ks

I like the debugger idea however, I have not idea how to do that.  I have looked online but cannot follow the instructions.

Thanks, in anticipation

jamie

  • Hero Member
  • *****
  • Posts: 7599
Re: ‘Absolute path’ and string path
« Reply #9 on: August 09, 2025, 01:47:12 am »
Can you show us the surrounding variables of the string you are using?

what WP commented on sounds plausible. It's possible you are clobbering the string. with a memory overwrite.

Jamie
 
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7599
Re: ‘Absolute path’ and string path
« Reply #10 on: August 09, 2025, 02:00:59 am »
The only other thing I can think of is that you may have that file already opened in a write only mode at the time you are doing this.

 even if you were to share this file, the file pointer could be at the end when you do the load and thus, nothing happens other than an empty file.

 Is it possible when you do the static file name test you are doing this as a standalone process?

Jamie


The only true wisdom is knowing you know nothing

stephanos

  • Jr. Member
  • **
  • Posts: 81
Re: ‘Absolute path’ and string path
« Reply #11 on: August 09, 2025, 01:13:48 pm »
Dear All

Thanks

I did an experiment to test the file already opened idea.  I opened the file in Kate, copied its contents to a new Kate file, saved it with a different name.  Ran the programme and selected the new file.  The string path/name was able to be written into the memo text and was correct.

Next came the is file present check using the same string containing the correct path/filename:  file not present.

Regrading this comment: "Is it possible when you do the static file name test you are doing this as a standalone process?" - I do not know what this means.

Regrading this comment: "Can you show us the surrounding variables of the string you are using?" - I would like to but I do not know what this means or how to do it

Oh dear, much help still needed  sorry

wp

  • Hero Member
  • *****
  • Posts: 13412
Re: ‘Absolute path’ and string path
« Reply #12 on: August 09, 2025, 02:32:23 pm »
I like the debugger idea however, I have not idea how to do that.
Find the line which generates the error. At this line, click in the gutter (the part of the code editor containing the line numbers, a bit left of them) so that a small red circle appears before the line number, and the line gets a red background(you may have to try several times if you do not hit the exact place) - this indicates that you created a breakpoint on this line. Now run the project and work as usual. When the program reaches the line with the breakpoint, the execution will stop, and the code editor with the breakpoint will be loaded. Now move the mouse over the variable (m3uPathFile), a popup window will appear which displays the current value of this variable. Check it carefully - it should be equal to the correct file name that you had assigned to it, but it could be possible that part of it has been overwritten by dangling pointers, memory overrun etc. Press F9 to continue the application again. If you don't need the breakpoint any more click on the read circle a second time so that it disapplears (along with the red background of the line).

A quick precaution against such ugly things is often to compile and run the appliation with all compiler checks enabled: Go to "Project" > "Project Options" > "Compiler options" > "Debugging" and check all boxes under "Checks and assertion". This maybe will crash you application when some other variable illegally writes to the memory allocated for m3uPathFile. From the code line where this happens you might learn why this occured.
 

old_DOS_err

  • Jr. Member
  • **
  • Posts: 60
Re: ‘Absolute path’ and string path
« Reply #13 on: August 09, 2025, 04:55:00 pm »
Hi,

Don't know if I've got your base problem corrrect, but I often search for files. A simple method, not the fastest, but reliable. Use FindAllFiles on the top directory, this will store all the files to a TStringList, say file_list. Run a For loop on file_list use file_list.Count - 1 (TStringList starts at 0, so check Count first, if 0 then a problem). Do a string breakdown, RPos( '/', ) will get the position of the last divider, use Copy to split the string. Now you have just the filename which you can compare to the desired file.

I don't use Unix, just Windows, but the pascal code should be the same. If you want a code demo of that I can do a very basic program. I'm assuming that it's just the location that changes, if there are files with the same name, that will get more complex.

Phil

stephanos

  • Jr. Member
  • **
  • Posts: 81
Re: ‘Absolute path’ and string path
« Reply #14 on: August 09, 2025, 08:50:08 pm »
Dear All

Thanks again to all. 

Phil, thanks for the info.  The task is to select a file, and then open it.  Selecting has worked but something goes wrong when storing the path/filename into a global string.

I have experimented with the breakpoint and I am not sure if I have discovered something.  But let me explain.  Please see file TheCode.png.  I will make reference to it and the line numbers.  I placed a single breakpoint at specific lines and ran the code.  And I did that multiple times.  The image that I took has the line number in the title of the file name.

TheCode.png  - Once I had completed a breakpoint on line 430, I commented out line 429.
The path/filename is a shorter one "/home/stephanos/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u".

So here we go.
Attempt 1 - See image m3uPathFile-debug427.jpg
I think it demonstrates that the global variable has not yet received the data from DialogFiles.Text.  After pressing F9 it will and does
Attempt 2 - See image m3uPathFile-debug428.jpg
I think it demonstrates that the path/filename has been saved somewhere.  The path/filename is mentioned twice.  In addition, after pressing F9 lines 429 and 430 are executed and the whole correct path/filename is output to the label’s caption and written to the memo text.
Attempt 3 - See image m3uPathFile-debug430.jpg
It demonstrates that the correct path/filename has some addition appended.  What I do not understand is how that addition appears after the closing
‘home/stephanos/PlayListV40-V2-TimeLimit-Current-RunFromPC/ThePlayListFile.m3u’#$0A
Attempt 4 - See images m3uPathFile-Debug433-B4F9.jpg and m3uPathFile-Debug433-AfterF9
These seem to explain the problem.  What is being looked for is a file:
      ThePlayListFile.m3u’#$0A
Which of course does not exist.  The output of the then and else are consistent with the file not being present.

Have we got to the heart of the mater?  What is the solution?  I  have learned how to use a breakpoint and step through the programme.  I assume you can set more than one?

Thanks all and wait to hear

 

TinyPortal © 2005-2018