deleting the last character from a txt file?

Posted By: PrenceOfDarkness

deleting the last character from a txt file? - 06/02/07 08:08

Hey guys, I'm having a real messed up problem... I can't seem to find a solution.. I've been writing variables to a file the problem is i'm having this really nasty bug in my game when I read the file back because I always end up having a space at the end (i need to use space as the delimit_str).

file_var_write always puts a space after whatever it puts in...

I need to get rid of the last character in my text file in order to fix this problem. Is there anyway to do this? I tried using ascii character 8 (backspace) but all it does is print some stupid character...
Posted By: Xarthor

Re: deleting the last character from a txt file? - 06/02/07 08:44

Code:

text temp_txt
{
strings = 1;
}

function del_last_char(_txt)
{
var handle;

//load the txt into a text object
txt_load(temp_txt,_txt);

//wait till its loaded
wait(3);

//cut off the last char
str_trunc(temp_txt.string[0],1);

//now resave the file:
handle = file_open_write(_txt);
file_str_write(handle,temp_txt.string[0]);
file_close(handle);
}

//call the function like this:
del_last_char("my_text_file.txt");



This is NOT tested but should work.
Posted By: PrenceOfDarkness

Re: deleting the last character from a txt file? - 06/02/07 14:55

ya it looks like it will... but my question is will this have a limit? Like upto how many characters can I copy with this method? I know strings are stupid and can only hold like 7000 or 70000 or something like that...

but for now.. if this works... it'll be fine! thanks

edit:
Okay, I read the manual... but it's still not to clear to me the way txt_load works. file_str_read has a 4000 character max... does this mean... if i use txt_load, after the 4000 characters of one string is filled up it'll carry on to the next string, assuming of course I have more then 1 string in the text object?
Posted By: PrenceOfDarkness

Re: deleting the last character from a txt file? - 06/02/07 17:23

alos I tried using bigdll.. but it doesn't seem to be working.. i included <FreeDll.wdl>;

and typed in:
temp = DllGetLengthFromFile("data.txt");
temp -= 3;
DllEreaseBytes(temp,3,"data.txt");

and nothing happened....
Then i tried to see if DllEreaseBytes worked at all and i tried:
temp = DllGetLengthFromFile("data.txt");
temp -= 3;
DllEreaseBytes(1,100,"data.txt");

nothing happened again.. can someone help out with this? What am i doing wrong?
Posted By: nipx

Re: deleting the last character from a txt file? - 06/02/07 18:09

i guess this problem (a space at the end of the file) is because delemit_str is a space.

Have you already tried to set the delemit_str to "" before you write the last var into you file? Then you wont get a space at the end, maybe. (Ive never tested )


nipx
Posted By: xXxGuitar511

Re: deleting the last character from a txt file? - 06/02/07 23:32

He already said he needed the delimeter str as a space. However, I guess he could set it to null, and then manually add a delimiter where needed...
Posted By: nipx

Re: deleting the last character from a txt file? - 06/03/07 00:05

Yes, I know he needs delimit_str=" " but he could set it to "" after he wrote the second last var and before the last one:

something like:

Code:

......
str_cpy(delimit_str, " ");
file_var_write(handle, a);
file_var_write(handle, b);
file_var_write(handle, c);
file_var_write(handle, d); //there's a " " after every var
str_cpy(delimit_str, ""); //but not after the very last one
file_var_write(handle, e);
.......




As I told, Ive not tested, its just a little hint...


nipx
Posted By: PrenceOfDarkness

Re: deleting the last character from a txt file? - 06/03/07 04:49

hey guys, thanks alot for all your help.. but I decided to rewrite my entire code so that I didn't have to worry about what it said at the end... I was trying to optimize the script and how fast it ran, i learned my lesson "If it aint broke... don't fix it )

Working with 3dgamestudio is like having a relationship....

with a drunk hooker...

better yet... more like a toilet , come on.. i know the ones who've been around since A5 or older know what i'm talking about

Anyways thanks for your help but I wont be needing you guys to waste your brain power on me anymore
Posted By: xXxGuitar511

Re: deleting the last character from a txt file? - 06/03/07 04:53

I remember A5... those were the days...

Glad you got it fixed... or improved... whatever
© 2024 lite-C Forums