• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[Solved] Specific way to set loop until with text eof

Xer0botXer0

Senpai
Good day,

From my understanding you can't modify a text file off the bat (for some lame reason).
So it has to be loaded into an array or so,
in order to do that I have to loop through the lines within the text file while populating an array how ever I don't know the line height!

Either I can iterate through the lines while incrementing a variable then I'd know, or while incrementing a variable (with offset of +1) to it, then it would basically be an endless look how ever I'd use file_text_eof to finally set the variable to lower than the current loop (i) value.

But is there actually a built in function for this ?
 
As far as I know there are no built-in functions for getting the number of lines in a file.

You just have to do as you say, read the file using a while loop and count them yourself, until file_text_eof(f) returns true.
 

FrostyCat

Redemption Seeker
What's wrong with this?
Code:
var f, lines, lines_count;
lines = ds_list_create();
f = file_text_open_read("filename.txt");
while (!file_text_eof(f)) {
  ds_list_add(lines, file_text_read_string(f));
  file_text_readln(f);
}
file_text_close(f);
lines_count = ds_list_size(lines);

/* Work with the list lines here; lines_count is the number of lines */

f = file_text_open_write("filename.txt");
for (var i = 0; i < lines_count; ++i) {
  file_text_write_string(f, lines[| i]);
  file_text_writeln(f);
}
file_text_close(f);
ds_list_destroy(lines);
 

chamaeleon

Member
From my understanding you can't modify a text file off the bat (for some lame reason).
Text files has no concept of "lines", it is just sequences of bytes, some of which programs may decide to interpret as line breaks while reading the bytes in. Modifying a text files means overwriting bytes (potentially including those that represent newlines, and thereby unintentionally losing "lines"), and you cannot make "lines" longer or shorter due to the nature of files being just a sequence of bytes. GMS lacks the ability to seek the file read/write position for an open file, but this is more an issue for those who wish to work with binary files with a structure in mind (having offsets embedded, or doing things in blocks of known size, etc.) The one operation you have available to you is to append more text at the end, because the operating system can tell the program where the end is, and it has no impact on the text that goes before it.
 

Xer0botXer0

Senpai
I think a DS_List might work very well here.

Going to give it a go, for starters I've got this in a text file that I want to modify so it should be a good warmup for the massive amount of information that is ahead.

settings.txt

[settings]
remember_username:
1
remember_password:
0
[/settings]

I thought perhaps doing something like
remember_username = 1

would be easier to work with but I'm already here so.
 
S

SSJCoder

Guest
have you tried using ini files?
not sure what you're trying to achieve so it's hard to tell what are the best functions for the job
 

Xer0botXer0

Senpai
I have worked with ini files before, they're really fun and simple to work with.
I do have my doubts that I should instead be going with ini files.

But I think my games data is going to be a lot more complicated with more depth. higher dimensions (which can be simulated with ini files too)..
I just felt that there's more freedom working with text files.

upload_2020-2-23_8-30-20.png

This is pretty much what I'm going for atm, the opening and closing tags will represent sections within a text file.
perhaps I'm just making it harder for myself haha!
 
S

SSJCoder

Guest
well, if you do want to create a custom kind of parser/text processor then you wouldn't want INI files, since they are mostly for simplicity.
what Game Maker do you use? I can look into it for you
 

Xer0botXer0

Senpai
Ok this is sorted
Had to include the settings.txt into my included files in gms.
(I wonder if I'll have to do this for every file ?? which may be an issue because I create certain files through gms. :/ like 2-3 files per user.

-

What's wrong with this?
Code:
var f, lines, lines_count;
lines = ds_list_create();
f = file_text_open_read("filename.txt");
while (!file_text_eof(f)) {
  ds_list_add(lines, file_text_read_string(f));
  file_text_readln(f);
}
file_text_close(f);
lines_count = ds_list_size(lines);
I'm getting an error, "file not opened for reading" at "while (!file_text_eof(f))"..

Code:
//read first
var txt_settings = file_text_open_read(working_directory + "\settings.txt");
var load_file = ds_list_create();
var line_count = -1;
while !(file_text_eof(txt_settings))
{
    ds_list_add(load_file,file_text_read_string(txt_settings));
}
file_text_close(txt_settings);
line_count = ds_list_size(load_file);
var txt_settings = file_text_open_read(working_directory + "\settings.txt");
This line is returning -1
But if I go to my working directory settings.txt is right there.
 
Last edited:
var txt_settings = file_text_open_read(working_directory + "\settings.txt");
This line is returning -1
But if I go to my working directory settings.txt is right there.
drop the back slash "\" - working_directory already includes this. Usually don't include working_directory either, it should work fine without it normally.
 

Xer0botXer0

Senpai
Hmm are you sure about dropping the backslash and working directory ?

The issue was that I didn't create the settings.txt file in the correct location (in the clients folder) I had it in the servers folder lol.
 

FrostyCat

Redemption Seeker
But I think my games data is going to be a lot more complicated with more depth. higher dimensions (which can be simulated with ini files too)..
Higher depth and dimension are better handled using JSON files that have built-in parsing routines, not custom formats that you have to write parsing routines for.
Had to include the settings.txt into my included files in gms.
(I wonder if I'll have to do this for every file ?? which may be an issue because I create certain files through gms. :/ like 2-3 files per user.
You only need to do this for files that you try to read before having written to. Check file_exists() first. If it exists, open the file, and if it doesn't exist, write the file and start with default configs.
 

Xer0botXer0

Senpai
I'll look up JSON files today,
I really set myself up for hard-aches if I go the txt route.
Tonight I'm suppose to work with txt files again for the rest of the registration process including character creation inventory item lists, their properties.. oof.
 
Top