Page 1 of 1

how to get text from a file on a server

Posted: Wed Jul 22, 2020 5:54 pm
by karmacomposer
I have tried 10 ways to sunday using the file: command, the url command, the ftp:// command, etc.

I am simply trying to read a number from a text file located on a web server without having to download it at all.

How is this done?

I am sure it is easy, but for some odd reason I just cannot get the coding right.

Thank you.

Mike

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 7:21 pm
by dunbarx
Hi.

Generically, something like this does not work:

Code: Select all

   open file yourFile for read
   read from file yourFile until EOF
   close file yourFile
Are you sure you know the pathName to the file? If not, you can use the "open file" command to get that information directly.

Craig

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 7:47 pm
by FourthWorld
Being on a web server limits your options, since HTTP is designed to deliver whole resources and provides no built-in means to extract specific portions of a resource.

If this is your own server, you could consider having your client call an LC Server script which reads the desired portion of the file and returns only that.

If it's not a server you manage your options will be more limited, and you may even need to just download the whole thing - how large is it?

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 8:04 pm
by karmacomposer
File is tiny. 1k to 2k.

Right now it’s on a server I control but in the future I will not have explicit access. No way to read it?

It’s just a simple text file.

Mike

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 8:06 pm
by karmacomposer
dunbarx wrote:
Wed Jul 22, 2020 7:21 pm
Hi.

Generically, something like this does not work:

Code: Select all

   open file yourFile for read
   read from file yourFile until EOF
   close file yourFile
Are you sure you know the pathName to the file? If not, you can use the "open file" command to get that information directly.

Craig
No. Something like that does not work.

Currently, I download the file and then read it but I’d rather read it in place and not touch it.

I do use tsnet with sftp to read and write to it. But I was hoping to read it in place and store the 1st line in a variable.

Mike

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 8:14 pm
by FourthWorld
1k-2k? Unless there's something wrong with the connection, or some unusual constraint in the problem (e.g. real-time gaming*) where the difference between 2k and a few bytes makes a noticeable difference, just download it. The overall throughput difference probably isn't much.

My own rule of thumb is that if it's smaller than a file system's block size (usually 4k these days), I don't spend wetware cycles thinking about it for local or remote IO.

* I was recently tickled to learn that many modern game devs have been using UDP for real-time updates to avoid the overhead of TCP, where apparently interpolating between missed packets is less impactful than the cost of TCP's assured delivery for certain types of game play:
https://www.gafferongames.com/post/udp_vs_tcp/

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 8:18 pm
by FourthWorld
Thinking about this more, you could modify libURL to cut off the HTTP read after a few bytes. But that's a lot of work, with minimal payoff unless the problem you're solving has specialized needs.

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 8:23 pm
by karmacomposer
Richard,

I agree with you. In fact, I already do this. However, I am having a very WEIRD problem.

I cannot discuss it fully, but in a nutshell, I keep track of what is being written and track it at all times.

Say I have a number, say 50. Now I add one to it and write it. Now it's, ta dah, 51.

Right? Not right. The next time I d/l the file, it's either blank or 1.

WTF.

I have no idea how to track this, i'll call it, bug. It's making my system very difficult to deal with as a result of not being reliable.

I use a lot of encryption and I am wondering if the time it takes to encrypt large files is somehow superceeding writing this tiny little file
and zeroing it out? I'm just not sure.

Therefore, i'd like a way to just read the number from a distance from a source that is not yet corrupted.

Instead of downloading it, I just want to read the number and store it in a new variable so that if the file becomes corrupted, I can write it back to it.

That's the best I can explain it due to being patents pending.

Mike

Re: how to get text from a file on a server

Posted: Wed Jul 22, 2020 11:35 pm
by FourthWorld
I like weird problems. If you want to discuss it via email, feel free to send an NDA. I can't guarantee I can fix it but sometimes having a second pair of eyes on something can help.

Re: how to get text from a file on a server

Posted: Thu Jul 23, 2020 1:16 am
by karmacomposer
I made a work around today.

I am writing to a .ini file at the very beginning and then checking against it at any write time.

It works as error correction but is more like a bandage and not a fix.

Mike

Re: how to get text from a file on a server

Posted: Thu Jul 23, 2020 2:24 am
by dunbarx
I did not see that a server was the source.

So, er, never mind... :oops:

Craig

Re: how to get text from a file on a server

Posted: Thu Jul 23, 2020 5:00 pm
by jacque
I need to update data on the server a lot, and I send the updated info to a server script which writes it to the correct file. Same for retrieval, the script reads the file and returns the info I need. This gets around permissions errors and has been reliable.

Re: how to get text from a file on a server

Posted: Thu Jul 23, 2020 5:53 pm
by jiml
I am simply trying to read a number from a text file located on a web server without having to download it at all.

How is this done?
...
It’s just a simple text file.
If that's actually what you want to do have you tried this?

Code: Select all

put url "https://mysite.com/mysimplenumberfile.txt" into theNumberIWanted
Jim Lambert

Re: how to get text from a file on a server

Posted: Thu Jul 23, 2020 6:22 pm
by FourthWorld
jiml wrote:
Thu Jul 23, 2020 5:53 pm
I am simply trying to read a number from a text file located on a web server without having to download it at all.

How is this done?
...
It’s just a simple text file.
If that's actually what you want to do have you tried this?

Code: Select all

put url "https://mysite.com/mysimplenumberfile.txt" into theNumberIWanted
What needs to be in place to allow writes to the server? How is authentication handled so those writes can't be made by others?

Re: how to get text from a file on a server

Posted: Thu Jul 23, 2020 10:48 pm
by karmacomposer
Not writing to the server in this case, just wanting to read a text file's contents.

I have reading and writing through SFTP using TSNet working perfectly by downloading/transferring the file and reading it on the node.

I was hoping to avoid all that extra code and just 'take it's temperature' essentially.

I made a work around that works.

Thanks everyone.

Mike