file identifier compiler error

Hello,
I created this function and it uses the file identifier "gamebase" created in the main function. when i try to compile it it gives an error like;
error C2065: 'gamebase' : undeclared identifier

1
2
3
4
5
6
7
8
9
10
void readFile(void)
	{
		gamebase=fopen("base.txt","a+");
		int index;
		for(index=0;index<Y_AXIS;index++)
		{
			fgets(table[index].tblrow,X_AXIS,gamebase);
		}
		
	}

I have to create the file in the main function and also have to use it in this function? how can I do it?
Make gamebase a global variable
Thanks for the reply but I tried to make it global variable like

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define X_AXIS 9
#define Y_AXIS 18

FILE *gamebase;
gamebase=fopen("base.txt","a+");

...........


and it gives errors

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2040: 'gamebase' : 'int' differs in levels of indirection from 'FILE *'
error C2440: 'initializing' : cannot convert from 'FILE *' to 'int'

You should also specify the line number where this error is happening

Try this:FILE *gamebase=fopen("base.txt","a+");

gamebase=fopen("base.txt","a+"); wouldn't work as this is a statement telling the computer to do something. It should be in a function. The global scope can only be used to declare functions,variable,classes,etc. But you can overcome this problem by using a compound statement where you are giving an instruction while declaring a variable and assigning it a value at the same time.
Don't make it global.

I'll assume this is a C program.

If the file is used later on within main, do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FILE* readFile(const char* filename)
{
	FILE *gamebase;

	gamebase = fopen(filename,"a+");
	if (gamebase)
	{
		int index;
		for (index=0; index < Y_AXIS; ++index)
		{
			fgets(table[index].tblrow, X_AXIS, gamebase);
		}		
	}

	return gamebase;
}
and change main() to use it like this:
 
	gamebase = readFile("base.txt");

it really helped thanks.
Topic archived. No new replies allowed.