Get a line in a file

Posted By: JGGamer

Get a line in a file - 01/13/10 21:42

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:

Code:
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.
Posted By: JGGamer

Re: Get a line in a file - 01/13/10 23:55

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:

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?
Posted By: GorNaKosh

Re: Get a line in a file - 01/14/10 00:47

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:
Code:
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' wink

So take this advices and try to fix your code. I hope I could help you a bit and have done no other mistakes grin
Posted By: JGGamer

Re: Get a line in a file - 01/14/10 03:28

Quote:
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

Quote:
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?

Quote:
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
Posted By: GorNaKosh

Re: Get a line in a file - 01/14/10 09:23

oky here we go ...
Code:
#include <acknex.h>

STRING* WordText = "#4000"; //string with space for 4000chars (4000chars is the max length of file_str_read())
STRING* fileString = "test.txt"; //the file
var lineToRead = 3; //the line that should be read

TEXT* Word =
{
  layer = 15;
  pos_x = 100;
  pos_y = 100;
  font = "Verdana#15"; //here you forgot a font
  strings = 1;
  flags = SHADOW | OUTLINE | TRANSLUCENT | SHOW;
  alpha = 100;
} 

void main() {
	
	video_mode = 8;
	video_screen = 2;
	
	var myFile = file_open_read(fileString); //you have to use file_open_READ to read the file
	if(myFile == 0) error("Couldn't open the file!");
	
	var i;
	for(i=0;i<lineToRead;i++) {
		file_str_read(myFile,WordText);
	}
	
	file_close(myFile);
	str_cpy((Word.pstring)[0], WordText);
}


Posted By: JGGamer

Re: Get a line in a file - 01/14/10 16:02

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.

Code:
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
Posted By: EvilSOB

Re: Get a line in a file - 01/14/10 19:09

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...
Posted By: JGGamer

Re: Get a line in a file - 01/16/10 01:48

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:

Code:
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.
Posted By: GorNaKosh

Re: Get a line in a file - 01/16/10 13:51

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 wink
Posted By: JGGamer

Re: Get a line in a file - 01/17/10 01:47

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 workcool. I wonder why it doesn't...? confused

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 smile
Posted By: JGGamer

Re: Get a line in a file - 01/17/10 02:59

Ok. It's working. I just need to get one last piece right.
The words are all joined, so I need to put a comma in between, and preferably drop each word one line down. Which means I have to have a string like this: ",\n". But I don't seem to know how to do it right, because i get an error with this line:
str_cat(newString,WordText+",\n"); ...in this code:

Code:
myFile = file_open_read(fileString);
	if(myFile == 0) {fileOpen = 0;}
	var i = 0;
	while (i < 91)
	{
		file_str_read(myFile,WordText);
		str_cat(newString,WordText+",\n");
		str_cpy((testing.pstring)[0], WordText);
		i += 1;
		if (i >= 91) 
		{
			file_close(myFile);
			fileOpen = 0; 
			myFile = file_open_write(fileStringCopy);
			file_str_write(myFile,newString);
			file_close(myFile);
			break;
		}
		wait(1);
	}



I searched the manual, but found nothing to help me.
Posted By: MMike

Re: Get a line in a file - 01/17/10 04:21

use the line feed of c++? not the \n try
"\\n"

or "\r" or even better "\r\n" carriage return
Posted By: JGGamer

Re: Get a line in a file - 01/18/10 04:54

I don't think '\n' is the problem. The issue is how to make a line when using a pointer (For example, wordText - which is a string, and then a code to tell the engine to drop to the next line). When I was using python, it was simply a matter of adding +"\n", which doesn't work with lite-c. If someone has info on this, I would be glad to have it.

Anyway... Great News! I found the solution! A simple way to delete a line in the file by copying of course.

Here's the code:


Code:
//Don't forget to declare the variables.

action recheck() //******THIS IS NOT IMPORTANT - just to see if I could use the copy.
{
	myFile = file_open_append(fileStringCopy);
	if(myFile == 0) {fileOpen = 0;}
	else
	{
		fileOpen = 1;
		var i;
		for (i=0; i<lineToRead; i++)
		{
			file_str_read(myFile,WordText);
			str_cpy((testing.pstring)[0], WordText);
		}
		file_close(myFile); fileOpen = 0;
	}
}

action replaceFile()
{
	myFile1 = file_open_read(fileString); //open file 1
	myFile2 = file_open_append(fileStringCopy); //open file 2
	if (myFile1 == 0) {fileOpen = 0;}
	var ii = 0;
	var skipLine = 0;
	while (ii < 91)
	{
		ii += 1;
		//when the line is reached...
		if (skipLine == 0 && ii == lineToRead) {skipLine = 1;} //...indicate
		file_str_read(myFile1,WordText); //read a line from file 1
		if (skipLine != 1) //as long as the line is not read...
		{
			str_cpy(newString,WordText); //...copy
			file_str_write(myFile2,newString); //...write a line to file 2
			file_str_write(myFile2,","); //place a comma after
		}
		str_cpy((testing.pstring)[0], newString); //check word on screen
		if (skipLine == 1) {skipLine = 2;} // indicate that the line is read
		if (ii >= 91) //when all lines are read...
		{
			file_close(myFile1); //close files
			file_close(myFile2);
			fileOpen = 0; 
			ent_remove(me); //remove temporary entity
			temp = ent_create("mark.mdl",vector(0,0,0),recheck);
			break; //stop the loop
		}
		wait(1);
	}
}

action copyFile() //******THIS IS NOT IMPORTANT - just a copy to string
{
	myFile = file_open_read(fileString);
	if(myFile == 0) {fileOpen = 0;}
	var i = 0;
	while (i < 91)
	{
		file_str_read(myFile,WordText);
		str_cat(newString,WordText); //copy the entire file to a string
		str_cat(newString,",");
		str_cpy((testing.pstring)[0], WordText);
		i += 1;
		if (i >= 91) 
		{
			file_close(myFile);
			fileOpen = 0; 
			ent_remove(me); //remove temporary entity
			temp = ent_create("mark.mdl",vector(0,0,0),replaceFile);
			break;
		}
		wait(1);
	}
}

//Get the word in the line
myFile = file_open_read(fileString);
if(myFile == 0) {fileOpen = 0;}
else
{
	fileOpen = 1;
	var i;
	for (i=0; i<lineToRead; i++)
	{
		file_str_read(myFile,WordText);
		str_cpy((Word.pstring)[0], WordText);
		str_cpy(theWord,WordText);
	}
	file_close(myFile); fileOpen = 0;
	temp = ent_create("mark.mdl",vector(0,0,0),copyFile);
}



In order to just use two files for copying, I copy the original using 'file_cpy', and use that file to read from (file 1). After the second file has been created (file 2), I clear file 1 by using 'file_open_write ("")'. It should be empty now, so then I can use that to copy from file 2. So I would be copying back and forth between those two files.
I feel good about this, because now I have a better feel on using files. I'm still interested in dropping a line though. It's not so important now, but it might come in handy later. So if anyone has some input, please post it. Thanks for all the help.
Posted By: GorNaKosh

Re: Get a line in a file - 01/18/10 06:50

You want to get a new line while writing in your file, right?
I think it's the same like '\r\n', but I use this sometimes:
Code:
file_asc_write(hndFile,13);
file_asc_write(hndFile,10);


Posted By: JGGamer

Re: Get a line in a file - 01/18/10 15:40

Yes! '\r\n' works! Thanks.
Sorry MMike, I hadn't tried that one. Mission complete - Thanks.
Posted By: JGGamer

Re: Get a line in a file - 01/18/10 16:01

Ah. I think I better address this now, because i may need to use it.

Let's say I have a string pointer - STRING* name = "#40";
Now I want to place a message at runtime - str_cpy((message.pstring)[0], name "has the ball");

How do I connect the string pointer 'name' to the string 'has the ball', so that I can change 'has the ball' to anything I want during the game, while 'name' (whatever name is used) remains the same?
Posted By: JGGamer

Re: Get a line in a file - 01/18/10 17:14

Oh dear... and I made a mistake. I forgot to change append to write in the code. Sorry. It should be file_open_write instead of file_open_append:

Code:
action replaceFile()
{
	myFile1 = file_open_read(fileString); //open file 1
	myFile2 = file_open_write(fileStringCopy); //open file 2
	if (myFile1 == 0) {fileOpen = 0;}
	var ii = 0;
	var skipLine = 0;
	while (ii < 91)
	{
		ii += 1;
		//when the line is reached...
		if (skipLine == 0 && ii == lineToRead) {skipLine = 1;} //...indicate
		file_str_read(myFile1,WordText); //read a line from file 1
		if (skipLine != 1) //as long as the line is not read...
		{
			str_cpy(newString,WordText); //...copy
			file_str_write(myFile2,newString); //...write a line to file 2
			file_str_write(myFile2,","); //place a comma after
		}
		str_cpy((testing.pstring)[0], newString); //check word on screen
		if (skipLine == 1) {skipLine = 2;} // indicate that the line is read
		if (ii >= 91) //when all lines are read...
		{
			file_close(myFile1); //close files
			file_close(myFile2);
			fileOpen = 0; 
			ent_remove(me); //remove temporary entity
			temp = ent_create("mark.mdl",vector(0,0,0),recheck);
			break; //stop the loop
		}
		wait(1);
	}
}


Posted By: GorNaKosh

Re: Get a line in a file - 01/19/10 05:47

to extend the string 'name' you have to use str_cat() if you need the name seperatly in your script again you could use an other temporary string like this:
Code:
//[...]
STRING *strTemp = str_create("#999");
str_cpy(strTemp, name);
str_cat(strTemp, "has the ball");
str_cpy((message.pstring)[0], strTemp);
str_remove(strTemp);


Posted By: JGGamer

Re: Get a line in a file - 01/20/10 19:49

Ah... append. Didn't think about that. Thanks.

One final thing - I hope. How do I convert a variable to a string? I found this in the manual, but I'm not sure how to use it. var_for_name("myVariable = 400"));
Posted By: GorNaKosh

Re: Get a line in a file - 01/20/10 20:19

Möp Fail grin ... try str_for_num()
Posted By: JGGamer

Re: Get a line in a file - 01/21/10 03:12

Sorry, I meant a variable pointer (for example: var myVar;), not the number itself.
Posted By: GorNaKosh

Re: Get a line in a file - 01/21/10 05:39

so you want to get the real script-name of the variable as string?
engine_getvarinfo()
like this???
Posted By: JGGamer

Re: Get a line in a file - 01/22/10 18:48

My version of A7 doesn't support engine_getvarinfo(). It does recognize engine_getvar() though. I tried:
Code:
long type;
STRING* varToString = engine_getvar(myVar,&type);


...but the string pointer does not contain a string.

I'm wondering if this is what I want though. Just to be sure that you understand.
I want to change the 4 in var myVar = 4; to a string, so that I can show it on screen. When the variable changes, it will update on screen at runtime.

Isn't there some simple code like str(myVar), as found in python, that converts the variable to a string?
Posted By: MrGuest

Re: Get a line in a file - 01/22/10 18:56

hey
str_for_num(STRING*, var) ?
Posted By: JGGamer

Re: Get a line in a file - 01/23/10 05:48

Hey! It worked!

GorNaKosh had given that to me before, and I didn't get it work. I guess I had complicated things.

Thanks for all your help. I can finally move on. Thanks
© 2024 lite-C Forums