Thread: Have function return file pointer

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Question Have function return file pointer

    Hi,

    I'm pretty new to C and I currently have this problem where I want a function to return a file pointer because I have this expression through out my program several time, so I wanted to use a function to minimize the lines of code. Currently I have:


    insert
    Code:
    FILE Exam(void)
    {
    	FILE *fr;
    	fr = fopen("Exam.txt", "r");								
    	if (fr == NULL) 
    	{
    		printf("The file didn't open.\n");
    		return 0;
    	}
    	return fr;
    }


    but it gives me the following errors:
    error C2440: 'return' : cannot convert from 'int' to 'FILE'
    error C2440: 'return' : cannot convert from 'FILE *' to 'FILE'

    if you could help it would be greatly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your return type is 'FILE' but you want to return a 'FILE*', just just add an '*' after FILE in the return type--the same way you declared fr!

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3
    Hey nadroj

    I did what you told me:


    'FILE* Exam(void)'

    but now it says:

    error C2040: 'Exam' : 'FILE *(void)' differs in levels of indirection from 'int ()'

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You probably forgot to make a function prototype.

    Code:
    FILE *Exam(void);
    Put this in your header file if you have one. If you don't have a header file for this, put it near the top of your .c file under all of your include directives.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    3
    MacGyver,

    That worked but now when I put it into my program it says:

    error C2143: syntax error : missing ')' before 'type'
    error C2059: syntax error : ')'

    If anyone can help, that'd be great. Thanks

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't think that has anything to do with the code that you've posted already.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    The cause to that is that either you forgot a closure right parenthesis or you have an extra left parenthesis. Check for that in the related area. I usually add one for each "(" and subtract one for each ")" and if not zero, then that's where the problem lies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM