Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, ozgur, AbrahamR, wdlmaster), 849 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Replacing or overwriting some text in a .txt #463366
11/30/16 07:16
11/30/16 07:16
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Hello Forum,
it looks like I am stuck again frown .

I have a text document with a lot of vars, listed like in this example:
Quote:

$obj_stuff1 = Apple;
$obj_stuff2 = Banana;
$obj_stuff3 = Pear;
$obj_stuff4 = Orange;


I can successfully read out the file and always find what I am searching for.

What I am trying to do now is, I want to change a certain line, or just replace it.
For example, I want to change
Quote:
$obj_stuff4 = Orange;

into
Quote:
$obj_stuff4 = Mango;


This means that I need to open the txt-file, place the reading-pointer after
Quote:
$obj_stuff4 =

and then overwrite the word 'Orange' with the word 'Mango'.
Basically I could also just delete the whole line and replace it with the new one.

I am not even sure if 3D Gamestudio 7.5 is capable of something like that, but here is what I tried so far:
Quote:

//Open the file
var filehandle = file_open_game(_chr(filepath));

//Find the '$obj_stuff4'
file_find(filehandle,selected_obj);

//Place the read/write pointer 3 bytes behind for the ' = '
file_seek (filehandle, 3, 1);

//Write my stuff
file_str_write(filehandle,new_string_to_write);
file_close(filehandle);


Of course this does not work and I am not completely sure about everything aswell.
Is there any way to achieve what I am trying to do?

There might be better solutions for storing my data but I really would prefer if I could keep my txt in the same format laugh

Yours truly,
Yking

Re: Replacing or overwriting some text in a .txt [Re: Yking] #463368
11/30/16 09:39
11/30/16 09:39
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Hi there!

I faced something similar, please take a closer look at this thread:
Name of the assigned function and model

I ended up with my own solution, which isn't the best way to handle it, but it worked. I saved everything before the word/whole line I need to change (let's call this BEFORE) and everything after that word/line (AFTER). So at the end, I just make changes, add it to BEFORE string and add to the result AFTER string. I guess 'updating' the whole file instead of the line/word isn't good, but at least it works grin There are some other awesome solutions in that thread, take a look!

Best regards!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Replacing or overwriting some text in a .txt [Re: 3run] #463385
12/01/16 13:22
12/01/16 13:22
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Hello laugh

Thanks for your suggestions, I have looked into the topic you linked to and I want to try it as soon as possible laugh .

Previously I had the same idea of first saving/cutting off the beginning and everything after the desired text (or in your words: using BEFORE and AFTER strings).
But according to the manual, reading out some text with "file_str_read" is limited to 4000 characters in a STRING, and I would like a method that would work infinitly laugh (so, targeting specific parts only instead of taking everything apart would be cool laugh )

And in case you wonder why 4000 might not be enough: The txt is supposed to contain the language, so the language is not hard-coded into my script laugh

Last edited by Yking; 12/01/16 13:24.
Re: Replacing or overwriting some text in a .txt [Re: Yking] #463387
12/01/16 13:40
12/01/16 13:40
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Originally Posted By: Yking
But according to the manual, reading out some text with "file_str_read" is limited to 4000 characters in a STRING
Where did you find this? I quick looked through the manual, but couldn't find it. Have you seen an example made by txesmi? laugh

Best regards!

Edit: the only thing I found was this:
Originally Posted By: Manual
Strings have some limitations in C-Script: they have a maximum length of 10000 characters, and the original length can't be changed. There are no such limits in lite-C.


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Replacing or overwriting some text in a .txt [Re: 3run] #463388
12/01/16 13:45
12/01/16 13:45
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline
Serious User
Reconnoiter  Offline
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Iirc the limit is 10k and only to the using string, not to the filesize. Which means that as long as you empty the string often enough (you can still go from line to line), you will be fine. wink

Re: Replacing or overwriting some text in a .txt [Re: Reconnoiter] #463454
12/05/16 03:38
12/05/16 03:38
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Hello again laugh

I successfully solved all my problems, well, at least I hope so grin
I used the "BEFORE" and "AFTER" method of 3run because of one reason:

My 3DGS is version 7.5 and does not include some of the functions that are used in some examples given (str_cut & str_trim).

I had to fiddle around a lot until everything worked out, but in the end its not just easily possible to 'replace' a part of a txt.
The only option was to edit it in a string and replace it after.

Maybe I should upgrade my 3DGS in the future to gain some additional functionality grin

Re: Replacing or overwriting some text in a .txt [Re: Yking] #463470
12/06/16 02:57
12/06/16 02:57
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Well, nope, looks like I was right:
After 4095 letters a String is full, so if either my "BEFORE" or "AFTER" is full, everything that comes after will be ignored and therefore deleted frown

The limit of 4000 is described in my A7.5 manual, newer versions of the engine support more, but still not unlimited
(http://manual.conitec.net/file_str_read.htm)

I need to somehow divide everything up into multiple reading-parts and then put them back together.
I will post a solution if I find one that works good laugh

Re: Replacing or overwriting some text in a .txt [Re: Yking] #463472
12/06/16 08:26
12/06/16 08:26
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Alright, problem is solved aswell laugh

Basically I wrote a loop that goes 10 to 20 times and
reads 4000 letters (could be 10000 in newer versions laugh ) and attaches it to the end of my String.

For some reason, if the end of the document is reached but it still is looping, it just kept on attaching the last read portion (I think that is because the read/write pointer goes all error when it is positioned too far).
I solved this by just comparing the current read out portion with the last one, and should they be the same, it will just not attach it to my String laugh .

It actually works really well and I have tested it with up to 80.000 letters (an 80kB .txt).

This only works with light C, since light C Strings are unlimited laugh


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1