2 registered members (AndrewAMD, juanex),
1,247
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Get a line in a file
#305274
01/13/10 21:42
01/13/10 21:42
|
Joined: Nov 2008
Posts: 109
JGGamer
OP
Member
|
OP
Member
Joined: Nov 2008
Posts: 109
|
I need some help using files. I want to read a particular line in a file, copy it as a string, then delete the line. Can someone show me how to do this please? EDIT: I just thought of something. Maybe I can use either one of these codes:
var i;
for (i=0; i<3; i++)
{
Line = file_str_read(myFile,fileString);
}
var i = 0;
while (i < 3)
{
i += 1;
Line = file_str_read(myFile,fileString);
if (i == 3) {file_close(myFile);}
wait(1);
}
I try them and see.
Last edited by JGGamer; 01/13/10 22:05.
|
|
|
Re: Get a line in a file
[Re: JGGamer]
#305292
01/13/10 23:55
01/13/10 23:55
|
Joined: Nov 2008
Posts: 109
JGGamer
OP
Member
|
OP
Member
Joined: Nov 2008
Posts: 109
|
OK... I'm stuck. I am using TEXT in order to see if the code worked, but I am getting problems. I use this code:
STRING* WordText = "WWW";
STRING* fileString;
TEXT* Word =
{
layer = 15;
pos_x = 100;
pos_y = 100;
size_y = 40;
string (WordText);
flags = SHADOW | OUTLINE | TRANSLUCENT | VISIBLE;
alpha = 100;
}
myFile = file_open_write("dialog.txt");
var i = 0;
while (i < 3)
{
i += 1;
WordText = file_str_read(myFile,fileString);
if (i == 3) {file_close(myFile); break;}
wait(1);
}
Word.string[1] = WordText; //This returns an error: string is not a member of text.
str_cpy((Word.pstring)[1], WordText); //This one doesn't seem to do anything.
How can I get my text to show the line read in the file?
|
|
|
Re: Get a line in a file
[Re: JGGamer]
#305294
01/14/10 00:47
01/14/10 00:47
|
Joined: Feb 2009
Posts: 84 Deutschland/Niedersachsen
GorNaKosh
Junior Member
|
Junior Member
Joined: Feb 2009
Posts: 84
Deutschland/Niedersachsen
|
If I'm right there a few problems in your script... STRING* WordText = "WWW"; This defines a string with a length of 3 character. I think your lines in your file could be longer than three characters. For this use something like this:
STRING* WordText = "#999"; //String with place for 999chars
STRING* fileString; This is an empty pointer and no ready to use String-Object. Use str_create in a function or define it like you can see above. WordText = file_str_read(myFile,fileString); The function file_str_read returns a var with the length of the string that was been found. You can't save a var in a string pointer ... Oo Then your while loop makes no sense to me. You try to run it three times and read the first three lines one after the other into the string 'fileString' ... Would you like to read the thrid line or what? str_cpy((Word.pstring)[1], WordText); This is the right code to copy a string into a text to show it on the screen. But remeber that your readed Text is in the string 'fileString' So take this advices and try to fix your code. I hope I could help you a bit and have done no other mistakes
|
|
|
Re: Get a line in a file
[Re: GorNaKosh]
#305300
01/14/10 03:28
01/14/10 03:28
|
Joined: Nov 2008
Posts: 109
JGGamer
OP
Member
|
OP
Member
Joined: Nov 2008
Posts: 109
|
WordText = file_str_read(myFile,fileString); The function file_str_read returns a var with the length of the string that was been found. You can't save a var in a string pointer ... Oo I don't understand. I was following the manual: STRING sTest; ... fhandle = file_open_read("test.txt"); // test.txt contains "this,is,a,test" file_str_read(fhandle,sTest); // sTest now contains "this" file_close(fhandle);Could you explain a bit more? Thanks Then your while loop makes no sense to me. You try to run it three times and read the first three lines one after the other into the string 'fileString' ... Would you like to read the thrid line or what? Yes, I want to read the third line. I was following this procedure: var_nsave filehandle; ... filehandle = file_open_read("myfile.txt"); // file contains: "123456.789 1.414" temp = file_var_read(filehandle); // now temp == 123456.789 temp = file_var_read(filehandle); // now temp == 1.414 file_close(filehandle);I was thinking that the loop would get me to the third line. I was using the text to see if it worked. Could you help me with the code? str_cpy((Word.pstring)[1], WordText); This is the right code to copy a string into a text to show it on the screen. But remeber that your readed Text is in the string 'fileString' At least I got some part right. I used 'WordText' because when I tried to use 'fileString', I got an error. Even when I tried to copy 'fileString' to another string, I get an error. Can you help a bit more? Thanks
|
|
|
Re: Get a line in a file
[Re: GorNaKosh]
#305400
01/14/10 16:02
01/14/10 16:02
|
Joined: Nov 2008
Posts: 109
JGGamer
OP
Member
|
OP
Member
Joined: Nov 2008
Posts: 109
|
Thanks It's working I learned though, that each word has to have a comma after, even though it's on a different line. Now I'm trying to find out how to delete/remove a line. There is no code called 'str_remove', so that means I would have to remove it with a blank string... I think. This is what I came up with, but it doesn't work, and something tells me it is wrong.
myFile = file_open_append(fileString);
var i;
for (i=0; i<lineToRead; i++)
{
file_str_read(myFile,WordText);
str_cpy((Word.pstring)[0], WordText);
file_str_write(myFile,str_cpy(WordText,""));
}
file_close(myFile);
This doesn't work either: file_str_write(myFile,"");Could you also help me with this one? Appreciate it. Thanks
|
|
|
Re: Get a line in a file
[Re: JGGamer]
#305429
01/14/10 19:09
01/14/10 19:09
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
I dont think you can read from a file opened for APPEND, but not sure of that. But take a look at THIS old code, you can use it as is, or just steal ideas from it...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Get a line in a file
[Re: EvilSOB]
#305649
01/16/10 01:48
01/16/10 01:48
|
Joined: Nov 2008
Posts: 109
JGGamer
OP
Member
|
OP
Member
Joined: Nov 2008
Posts: 109
|
Thanks for that, but I'm not sure how to use it. Lite-c doesn't recognize any of those codes. Is that C++? Thanks anyways. I thought of something else that might work. What if I copy the file into a string, remove the line, then save it as a file again? Only I'm not getting it done. I thought this should work:
while (i < 100)
{
i += 1;
file_str_read(myFile,WordText);
str_cat(newString,WordText);
str_cpy((testing.pstring)[0], newString); //for displaying results
if (i >= 100) {file_close(myFile); break;}
wait(1);
}
but it's not. Should this not loop until all the lines are read, while appending each word to 'newString'? So could you help me understand a few things, just in case I am missing a minor flaw (Forgive me if the flaw is major. This is new to me)? For example, Why am I using '0' in 'str_cpy((Word.pstring)[0], WordText);' instead of '1' or higher? In the TEXT parameters, how can I set strings to unlimited (strings = 1;)? Oh, and append appears to allow for reading.
|
|
|
Re: Get a line in a file
[Re: JGGamer]
#305677
01/16/10 13:51
01/16/10 13:51
|
Joined: Feb 2009
Posts: 84 Deutschland/Niedersachsen
GorNaKosh
Junior Member
|
Junior Member
Joined: Feb 2009
Posts: 84
Deutschland/Niedersachsen
|
The code from EvilSOB is liteC, too. It's a very good code, but just as complicated for beginners. Your code should copy every content of 'WordText' at the end of 'newString'. Remeber to initialize newString with enough space like mentioned before. (testing.pstring)[0] - testing is your text-object - pstring is the child which is holding the displayed strings You have to use [0] because it's an array of strings and the index starts with zero allways. So if you use 'str_cpy((testing.pstring)[1], newString);' you copy the content of 'newString' into the second string of your text-object. You can't create a text-object with a unlimited count of strings. While defining the object you have to suggest how much strings you might need, because you can't change this value during runtime, too. But I think there is no need for a text with unlimited strings. This object is for displaying strings on screen and there is limited space only. For saving strings there are better possibilities... Sry for my bad english. Hope you get it
|
|
|
Re: Get a line in a file
[Re: GorNaKosh]
#305758
01/17/10 01:47
01/17/10 01:47
|
Joined: Nov 2008
Posts: 109
JGGamer
OP
Member
|
OP
Member
Joined: Nov 2008
Posts: 109
|
Does the code from Evil work in any GS version, because I get an error of unidentified "whats it again? I don't remember.". Is it unrecognized identifier? Anyhow I get that error where the engine doesn't recognize something. I'm using version 7. So my code is correct, which means it should work . I wonder why it doesn't...? Thanks for the info. I now have a better understanding. I'll go through it with a fine-tooth comb, and see if I can figure out why it's not working. Thanks
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|