Compare text from a file?

Talk about making games.

Compare text from a file?

Postby bat78 » Sat Jun 01, 2013 3:32 am

Hello developers. I'm currently working on easy, advanced and effective way for online gaming in game-editor and i am actually almost done.
But i am not sure for one thing. How can i compare text from a file, using my conditions like:
else if (line[0], ch[1] == integer) {
..
}
Prob i have to store the text into an array with fgets?
Maybe i can use something like:
if (strcmp(line[0], ch[1], integer) == 0) {
..
}
But that work only with arrays.. but directly from a text file.. i don't think they are. Well at least i tried and i couldn't figure it out.
SHORTLY: Lets say i want to use numbers from a FILE as a value of a variable.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Compare text from a file?

Postby skydereign » Sat Jun 01, 2013 9:28 pm

If the files are formatted in the same way, it is technically possible to jump to the same position in the file using fseek, and never need to store the values in an array. But, you'll still need to draw out the values using fgets to compare them.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Compare text from a file?

Postby bat78 » Sat Jun 01, 2013 9:51 pm

like how? :o
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Compare text from a file?

Postby skydereign » Sat Jun 01, 2013 10:00 pm

Using this function. http://www.cplusplus.com/reference/cstdio/fseek/
General idea, you seek to the same spot in both files, and then read a line in from both files. Normally you can't seek to ends of lines, but if you know each line is a certain number of bits, you can do the calculation yourself. Another approach is that you record the positions at the top of the file, so you can read those in, so you know where to fseek to.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Compare text from a file?

Postby bat78 » Sat Jun 01, 2013 10:17 pm

How that will help me
1. To store numbers from .txt file to in variables in gE.
2. Use conditional code if the text in the .txt file is equal to a string in gE or a const choice.

Its little foggy for me :(
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Compare text from a file?

Postby skydereign » Sat Jun 01, 2013 10:23 pm

bat78 wrote:1. To store numbers from .txt file to in variables in gE.
2. Use conditional code if the text in the .txt file is equal to a string in gE or a const choice.

Are you asking how to write and read from a file?
bat78 wrote:Its little foggy for me :(

All I did was answer your question, I don't know why you want to compare text from a file. From the above, it sounds like you just want to know how to do file io.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Compare text from a file?

Postby bat78 » Sat Jun 01, 2013 10:39 pm

I know how to read and write from/to a file but i want to compare the file with my conditional text.
I gived examples like:

if (strcmp(textArray[0], "TRUE") == 0) {
ExitGame();
}

so if the first line of the text file is TRUE, the game exiting.
That does not work.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Compare text from a file?

Postby skydereign » Sat Jun 01, 2013 10:43 pm

Since you are using fgets, textArray[0] will end in a new line character. You should remove it from the string before comparing.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Compare text from a file?

Postby bat78 » Sat Jun 01, 2013 11:03 pm

Im using copy of the GE's formuled content
Code: Select all
char textArray[10][256]; //Max 10 text of 255 characters
int nText = 0; //Number of texts read


void readText(char * fileName)
{
 
 char line[256];
 FILE *arq = fopen(fileName, "r");


 if(arq)
 {
  while(fgets(line, 255, arq) && nText < 10)
  {
   if(strlen(line) > 0) //Dont put empty lines
   {
    strcpy(textArray[nText], line);
 
    nText++;
   }
  }
 }
}


what i have to do..
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Compare text from a file?

Postby skydereign » Sat Jun 01, 2013 11:10 pm

Here's the text from a file.
this is line 1
second line
last line (line 3)

The first line is "this is line 1", so you would try comparing it to that. But when you use fgets, it will return one extra character (the new line). Therefore if you try to compare "this is line 1" and "this is line 1\n" (\n being the newline character) they will not be equal. So, with the string you got from the fgets, change the last character to the null character. You have to do this because fgets will return the newline character as part of the string.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Compare text from a file?

Postby bat78 » Sat Jun 01, 2013 11:18 pm

So null can be a string too If we use fgets and compare a text from a file..
Okay then, so what the code becomes..
null char? like Space? What code i should put now then?
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron