Hi, i have som functions in a header file. But i get errors at compilation.

#include <stdio.h>
//#include <limits.h>
#include <math.h>
#include <string.h>
 
//define error codes
enum
{
	PERROR_FILEOPEN,
	PERROR_FILECLOSE
};
 
//other defines
#define P_LINELIMIT 512
 
//globally needed variables
int P_Error = 0;
char P_Line[P_LINELIMIT];
 
//typedefs
typedef FILE P_File;
 
//Error and Debug functions
void P_SetError(int err)
{
	P_Error = err;
}
 
//File functions
P_File * P_OpenFile(char file[]) //Opens a P file and returns its handle
{
	FILE * f = fopen(file, "r");
	if(f == NULL)
	{
		P_SetError(PERROR_FILEOPEN);
	}
	return (P_File*)f;
}
 
bool P_RunFile(P_File * file)
{
	//if syntax error: return false
	while(true)
	{
		P_Line = fgets(P_Line, P_LINELIMIT, (FILE*)file);
	}
	return true;
}
 
bool P_CloseFile(P_File * file)
{
	if(fclose((FILE*)file) != 0)
	{
		P_SetError(PERRPR_FILECLOSE);
		return false;
	}
	else
	{
		return true;
	}
}
 
 
===========================================================
ERRORS:
===========================================================
Error	2	error C2061: syntax error : identifier 'P_RunFile'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	40	
Error	3	error C2059: syntax error : ';'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	40	
Error	4	error C2059: syntax error : 'type'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	40	
Error	5	error C2061: syntax error : identifier 'P_CloseFile'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	50	
Error	6	error C2059: syntax error : ';'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	50	
Error	7	error C2059: syntax error : 'type'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	50

Please do not tell us you are putting that code in a header file ???? Functions do not go in header files, but only in *.c files. Only put function prototypes in header files.

I've hade some changes, but i still got syntax errors.

P.h

#ifndef _P_H_
#define _P_H_

#include <stdio.h>
//#include <limits.h>
#include <math.h>
#include <string.h>

//typedefs
typedef FILE P_File;

//define error codes
enum
{
	PERROR_NOERROR = 0,
	PERROR_FILEOPEN,
	PERROR_FILECLOSE
};

//other defines
#define P_LINELIMIT 512

P_File * P_OpenFile(char file[]); //Opens a P file and returns its handle
bool P_RunFile(P_File * file); //Executes a file
bool P_CloseFile(P_File * file); //Closes a previously opened P file

#endif

P.c

#include "P.h"

//globally needed variables
int P_Error = 0;
char P_Line[P_LINELIMIT];

//Error and Debug functions
void P_SetError(int err)
{
	P_Error = err;
}

//File functions
P_File * P_OpenFile(char file[]) //Opens a P file and returns its handle
{
	FILE * f = fopen(file, "r");
	if(f == NULL)
	{
		P_SetError(PERROR_FILEOPEN);
	}
	return (P_File*)f;
}

bool P_RunFile(P_File * file) //Executes a file
{
	//if syntax error: return false
	while(true)
	{
		P_Line = fgets(P_Line, P_LINELIMIT, (FILE*)file);
	}
	return true;
}

bool P_CloseFile(P_File * file) //Closes a previously opened P file
{
	if(fclose((FILE*)file) != 0)
	{
		P_SetError(PERRPR_FILECLOSE);
		return false;
	}
	else
	{
		return true;
	}
}

And those are the errors i'm getting

Error	1	error C2061: syntax error : identifier 'P_RunFile'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	24	
Error	2	error C2059: syntax error : ';'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	24	
Error	3	error C2059: syntax error : 'type'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	24	
Error	4	error C2061: syntax error : identifier 'P_CloseFile'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	25	
Error	5	error C2059: syntax error : ';'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	25	
Error	6	error C2059: syntax error : 'type'	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	25	
Error	7	fatal error C1004: unexpected end-of-file found	c:\documents and settings\nti\mina dokument\visual studio 2005\projects\p\p\p.h	27

Alas, Visual C does not support // comments in C sources.
Change them to old (classic) C comments /* */

Apropos: bool, true and false - C++ keywords in C source?..

Alas, Visual C does not support // comments in C sources.
Change them to old (classic) C comments /* */

Nope. VC++ supports // comments in C code. I think its part of the C standards now, but wouldn't swear to it.

There's no bool type and no true and false constants in standard C before C99 (and in C99 you must #include <stdbool.h>) I don't know about Visual C.

You can typedef enum { false, true } bool .

You also have an illegal assignment:

P_Line = fgets(P_Line, P_LINELIMIT, (FILE*)file);

Just eliminate the "P_Line ="

and a typo in:

P_SetError(PERRPR_FILECLOSE);
Comments
good help.

Nope. VC++ supports // comments in C code. I think its part of the C standards now, but wouldn't swear to it.

Well, try to compile (take VC++ 2008, for example) modern.c module with

void f();
int main() //
    f();
    return 0;
}

and see what happens ;)...

It doesn't compile because it is missing { on line 3, not because of the // comments. Compiles fine with *.c file extension on vc++ 2008 express.

void f();
int main() //
{ // <<<<<<<<<<<<<<<<<<<<<<<<<<<
    f();
    return 0;
}
Comments
i love when that happens :) ... except of course when it happens to me :(

It doesn't compile because it is missing { on line 3, not because of the // comments. Compiles fine with *.c file extension on vc++ 2008 express.

void f();
int main() //
{ // <<<<<<<<<<<<<<<<<<<<<<<<<<<
    f();
    return 0;
}

Yes, I was in a hurry ;)

This article has been dead for over six months. Start a new discussion instead.