Author Topic: (SCI1.1) File Operation Cluelessness  (Read 1395 times)

0 Members and 1 Guest are viewing this topic.

Offline claudehuggins

(SCI1.1) File Operation Cluelessness
« on: March 25, 2017, 06:26:02 PM »
I've been doing some experimenting with the file operation commands in SCI1.1, and can successfully read and write strings to/from a file. (E.g. Write "hello", then read it into the variable myString so printing myString prints "hello")
My question is, is there any methods or procedures I can use to read/write on a line-by-line basis?

Here's an example. Say I have a string "myString", and an integer "myNumber". Is there a way to write myString to line 1 of a file, write myNumber to line 2, and then later on seek to and fetch myNumber again by specifying that I want the second line?

Also, I've so far only gotten file operations to work with strings, and can work with that, but if there's a way to retrieve variables (e.g. Integers) I can do arithmetic to, it would be wonderfully helpful.

I've been asking a lot of dumb questions lately, so thank you all for being so patient with me  :-[


A while ago, at a block party I found myself socially trapped at, I thought to myself: I need a t-shirt that says, "I'd rather be programming".

Offline Kawa

Re: (SCI1.1) File Operation Cluelessness
« Reply #1 on: March 25, 2017, 07:03:19 PM »
File:readString and fiREAD_STRING apparently read up to N characters or the end of a line, if any. Conversely, File:writeString and fiWRITE_STRING would add a newline character.

Here's how The Dating Pool used to read its settings:
Code: [Select]
(= fHnd (FileIO fiOPEN {settings} fOPENFAIL))
(if (> fHnd 0)
(FileIO fiREAD fHnd @value 2)
(= _detailLevel value)
; That's one 16-bit integer. Repeat as needed.
(FileIO fiCLOSE fHnd)
)
Conversely:
Code: [Select]
(= fHnd (FileIO fiOPEN {settings} fCREATE))
(= value (gGame _detailLevel?))
(FileIO fiWRITE fHnd @value 2)
(FileIO fiCLOSE fHnd)
Of course, you should probably use the File class instead.

As such, if you were to write myString to a file, then (a stringification of?) myNumber, you'd simply have to read and toss the first line.

Offline lskovlun

Re: (SCI1.1) File Operation Cluelessness
« Reply #2 on: March 25, 2017, 11:37:56 PM »
As such, if you were to write myString to a file, then (a stringification of?) myNumber, you'd simply have to read and toss the first line.
You could make sure that every line has the same length (an appropriate argument to Format can ensure this). If that is the case (and you have to be able to count on it for this to work!), then fetching the n'th line becomes as simple as a fiSEEK followed by an fiREAD_STRING. But that makes the designated length a hard max, and adventurous users - or a buggy program - can spoil it.

Not knowing what you are trying to accomplish, I would probably reconsider. Seeking in a text file is not good practice in my book.

Offline Kawa

Re: (SCI1.1) File Operation Cluelessness
« Reply #3 on: March 25, 2017, 11:54:13 PM »
Nor in mine, unless you have an index file.

Offline lskovlun

Re: (SCI1.1) File Operation Cluelessness
« Reply #4 on: March 26, 2017, 02:13:05 AM »
Also, I've so far only gotten file operations to work with strings, and can work with that, but if there's a way to retrieve variables (e.g. Integers) I can do arithmetic to, it would be wonderfully helpful.
The ReadNumber kernel call can do that. You have to read into a string variable first, then call ReadNumber with a pointer to it (@strVar), and it returns an integer.


SMF 2.0.14 | SMF © 2017, Simple Machines
Simple Audio Video Embedder

Page created in 0.094 seconds with 23 queries.