What is wrong with my function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void ReadScript(int WhichScript)
{
	bool Started = false;
	bool InCondition = false;

	fpos_t pos;

	char temp[500];
	sprintf(temp, "Script/%d%s", WhichScript, ".jrpscript");
	myScriptFile[WhichScript] = fopen(temp, "r");
	if (myScriptFile[WhichScript])
	{
		char YO[50];

		while (feof(myScriptFile[WhichScript]) == false || strcmp(YO ,"Stop") == 0)
		{
			fscanf(myScriptFile[WhichScript], "%s", &YO);

			if (strcmp(YO , "Start") == 0)
			{
				Started = true;
			}

			
			else if (strcmp(YO, "If") && Started == true)
			{
				fscanf(myScriptFile[WhichScript], "%s", &YO);
				fgetpos (myScriptFile[WhichScript],&pos);
				fscanf(myScriptFile[WhichScript], "%s", &YO);
				if (strcmp(YO, "And") == 0)
				{
					fscanf(myScriptFile[WhichScript], "%s", &YO);
					InCondition = true;
				}
				else
				{
					fsetpos(myScriptFile[WhichScript], &pos);
				}

			}

			else if (strcmp(YO, "EndIf") == 0)
			{
				InCondition = false;
			}
		}

		fclose(myScriptFile[WhichScript]);
	}
}


for some reason it stuck in an infinite loop

this is the content of the 0.jrpscript (assuming i passed 0 as the parameter)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Start
1
2
3
4
If
5
6
7
8
9
10
EndIf
Stop


When i run through the debug, these lines being run:
1
2
3
4
5
else if (strcmp(YO, "If") && Started == true)
			{
				fscanf(myScriptFile[WhichScript], "%s", &YO);
				fgetpos (myScriptFile[WhichScript],&pos);
				fscanf(myScriptFile[WhichScript], "%s", &YO);


i have no idea what is wrong
I'm not sure what that code's supposed to do, but there are a number of problems.
 
feof(myScriptFile[WhichScript]) == false
feof returns an int, not a bool
 
strcmp(YO ,"Stop") == 0
is checking the value of YO before it's initialised.
 
fscanf(myScriptFile[WhichScript], "%s", &YO);
Arrays are passed by ref (the address of the first element). Attempting to pass the address of that is wrong. If you're lucking, the compiler will ignore you and do the right thing.

In places, you have multiple reads without checking for EOF.

Why is the FILE* held in a global variable when it's local to the function?

Have you walked thru this in a debugger to see what's wrong?
ERROR:while (feof(myScriptFile[WhichScript]) == false || strcmp(YO ,"Stop") == 0)
when feof(myScriptFile[WhichScript]) !=false
strcmp(YO,"Stop") == 0
so it stuck in an infinite loop
try this
while(feof(myScriptFile[WhichScript]) == false && strcmp(YO,"Stop") !=0)
I changed the feof(myScriptFile[WhichScript]) == false to != 0

it no longer stuck in an infinite loop


BUT

i still have 1 problem:

as you can see, my script file DOES contain an IF

but, some how the line fscan etc from the code:

1
2
3
4
5
else if (strcmp(YO, "If") && Started == true)
			{
				fscanf(myScriptFile[WhichScript], "%s", &YO);
				fgetpos (myScriptFile[WhichScript],&pos);
				fscanf(myScriptFile[WhichScript], "%s", &YO);


never run

I dont know why
Return Value

The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.

you changed the feof(myScriptFile[WhichScript]) == false to != 0
when you start to read file,its NOT a end of file so fefo(...) return 0
then it NEVER run the code in while{...}
But it does run code in the while

it just never run code in the if part
Topic archived. No new replies allowed.