Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / [SOLVED] opentoread not working after the first call

Author
Message
dandrews
4
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 11th Jun 2023 01:57
so i was messing around trying something for another project and i noticed that in this code when i try to opentoread on the second go it says file not found?

The author of this post has marked a post as an answer.

Go to answer

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 11th Jun 2023 02:11 Edited at: 11th Jun 2023 02:18
short version is you closed the file before reading it the 2nd time.

longer version is that it actually doesn't say "file not found". it says (in essence) "the reference to the file is invalid" (you closed it). if you rem out line 25, you'll get the same error @ line 26 (it's already closed).

more: it's probably not a good idea to open the same file for both read and write at the same time.
dandrews
4
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 11th Jun 2023 02:16
but i iclose the file right before reading it and i thought if i used fileread (opentoreadfile) it doe not open it again??
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 11th Jun 2023 02:22 Edited at: 11th Jun 2023 02:25
edited my first response but it's gonna cause a headache, as is. but, if you insist, see the changes:
dandrews
4
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 11th Jun 2023 02:36
ok i see but im still somewhat lost because i assumed if i closed the file it would be available again.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 11th Jun 2023 03:26 Edited at: 11th Jun 2023 03:32
Quote: "but i iclose the file right before reading it"
but it needs to be open to read it.

here you read & closed it:

then immediately tried to read from it (while its closed):

and close it again:

without ever re-opening it (to make it available again)?
dandrews
4
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 11th Jun 2023 03:35
i have fileread = opentoread(fileid) so i thought readline(fileread) would open it to read the line??
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 11th Jun 2023 03:45 Edited at: 11th Jun 2023 03:47
fileread is holding the ID# of the instance of OpenToRead(). that's where it's opened (and assigned an ID for later reference)

readline(fileread) is reading the line from the file referenced via that instance of the open file (that's all).

ie, on the first attempt to read the first line, teststring is assigned the value of readline(fileread).

you've read the help files, right?
OpenToRead(), ReadLine(), et al?
dandrews
4
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 11th Jun 2023 04:11 Edited at: 11th Jun 2023 04:14
This post has been marked by the post author as the answer.
agh i see thank you. so if i used readfile = opentoread(fileid) then closefile(fileid) ... i would then have to define readfile as readfile= opentoread(fileid) again

Login to post a reply

Server time is: 2024-11-21 09:51:05
Your offset time is: 2024-11-21 09:51:05