Small C problem

This is a discussion on Small C problem within the C Programming forums, part of the General Programming Boards category; sorry for asking the question but can any one here see whats worng with the following Code: int main (void) ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    27

    Small C problem

    sorry for asking the question but can any one here see whats worng with the following

    Code:
    int main (void)
    {
    	//...Determining whether there is stored tree or not...
    	printf("\n+--------------------------+");
    	printf("\n|      Guessing Game       |");
    	printf("\n|------------By------------|");
    	printf("\n|------------?-------------|");
    	printf("\n+--------------------------+");
    	printf("\n\n\nPress any key to continue..."); 
    	//getch(); 
    	//clrscr();
    	FILE *in;
    
    	in = fopen ( "people.txt", "rb" );
    	if (in == NULL)
    	{
    	         //fclose(in); 
    		//BeginNew(); 
    		//Continue();
    	}
    	else
    	{
                      //fclose(in); 
    		//ReadFile(); 
    		//Continue();
    	}
    	return 0;
    }
    when i run that i get 2 errors the first is about the use of FILE:
    Code:
    C:\guessing_game\Game.c(35) : error C2275: 'FILE' : illegal use of this type as an expression
    the other is:
    Code:
    C:\guessing_game\Game.c(35) : error C2065: 'in' : undeclared identifier
    ive gone through various posts here and gone through the FAQ for files but as far as i can see im doing nothing wrong.

    Can any one offer a bit of insight as to what im doing wrong or am missing?
    Always posting problems

    Always needing help

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Hmm...try declaring the FILE at the beginning of main, where you normaly declare variables. The compiler might like it better there. Otherwise...I'm not really sure what the issue is.
    Also, you might want to read the file before you close it
    -Crazed

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    Quote Originally Posted by CrazedBrit
    Hmm...try declaring the FILE at the beginning of main, where you normaly declare variables. The compiler might like it better there. Otherwise...I'm not really sure what the issue is.
    Also, you might want to read the file before you close it
    -Crazed


    wow thats the craziest thing i ever saw as soon as i moved the line
    Code:
    FILE *in;
    it completely takes away all the errors cheers
    Always posting problems

    Always needing help

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    in c, all local variable declarations must be made at the start of
    a code block (i.e. a function). this may have been changed in
    the last standard but i'm too tired to check, and if it was, then its
    still better practice to declare them at the start anyways.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    I'm not sure if you were meant to do this or not but you are opening a .txt file in binary mode.

  6. #6
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Richie T
    in c, all local variable declarations must be made at the start of
    a code block (i.e. a function). this may have been changed in
    the last standard but i'm too tired to check, and if it was, then its
    still better practice to declare them at the start anyways.
    C99 allows interspersing of declarations and code.

    I won't completely agree with you about it being a better practice, but if your compiler does not support C99 then you have to declare them before the first executable line of code.

    If you're coming to C from C++, and if your compiler supports C99, then declaring variables just before you use them will be a familiar practice.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 05:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 11:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 05:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 04:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21