Page 1 of 1
Compare text from a file?
Posted:
Sat Jun 01, 2013 3:32 am
by bat78
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.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 9:28 pm
by skydereign
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.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 9:51 pm
by bat78
like how?
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 10:00 pm
by skydereign
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.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 10:17 pm
by bat78
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
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 10:23 pm
by skydereign
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.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 10:39 pm
by bat78
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.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 10:43 pm
by skydereign
Since you are using fgets, textArray[0] will end in a new line character. You should remove it from the string before comparing.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 11:03 pm
by bat78
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..
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 11:10 pm
by skydereign
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.
Re: Compare text from a file?
Posted:
Sat Jun 01, 2013 11:18 pm
by bat78
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?