Thread: file reading problems

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    file reading problems

    Here is my code:

    Code:
     int Start( void )
    {
    
        printf ( "Starting...\n\n"
                 "All questions will be multiple choice, answer with either\n"
                 "1, 2, 3 or 4.\n\n\n" );
    
    
          /* here will be open data.txt */
    
        FILE *fp;
    
            if ( ( fp = fopen( "data.txt", "r" ) ) == NULL )
            {
                    fprintf (stderr, "Error opening file.\n");
                    exit(1);
            }
    
            fclose(fp);
    
    
    
        return 0;
    }
    and the problem lies where "FILE *fp;" is...
    The error i get is.
    [C++ Error] main.c(76): E2140 Declaration is not allowed here

    I checked weather it would compiler under another compiler, and Dev-C++ does the job. Why would Borland have a b!tch about where the Declaration is?

    Thanks for anyone that can help me.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Declarations need to go at the top of a code block. So, it looks like this:
    Code:
    int foo(int i)
    {
      FILE  *fp;
      /* Other  variable declarations here */
      /* Then the code */
      return something;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    In C declarations must be at the beginning of a block.
    [edit]
    I definately need to decide faster what to type.
    However, in JAVA you can declare a variable wherever you want.
    [/edit]
    Last edited by Sargnagel; 06-26-2003 at 06:16 AM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>In C declarations must be at the beginning of a block.
    Not in C99.
    That's why gcc compiles without errors.

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    Disregarding what is legal and what isn't, do you guys think that declaring variables right before they are used makes the code easier to read or harder? I've always wondered about this cause in a C++ book I have the author does this, so apparently it is legal in C++ also, but is it easier to read?

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I think they should be declared right before they are used. That way if you have a 80-line function, you know which variables are used where. If you declare them all on the top, it might be harder to see what goes on when.

    C++ allows local variable declarations anywhere you want.

  7. #7
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Wledge
    >>In C declarations must be at the beginning of a block.
    Not in C99.
    That's why gcc compiles without errors.
    Yes, you're right. Before switching to gcc I used Borland. It truly does complain about declarations that are not at the beginning of a block. I guess that's why I have never tried it with gcc.

    I got used to writing in both styles. I've never thought about which style is better. But if declaring variables right when you need them gives the compiler more information for optimizations (although I doubt that), then I would go for it regardless of bad (?) readability.
    Last edited by Sargnagel; 06-26-2003 at 06:57 AM.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Speedy5
    I think they should be declared right before they are used. That way if you have a 80-line function, you know which variables are used where. If you declare them all on the top, it might be harder to see what goes on when.

    C++ allows local variable declarations anywhere you want.
    I find it harder myself -- so I'm sure it completely depends on what you started learning on. In my case of course, C long before C99. I'm with you SourceCode.

    I'd rather have the declarations at the top so I don't have to search the code for a declaration if I happen to notice a problem toward the end of the module.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Personally, I prefer them at the top of the code block, that way I know where to find them all easily, and I know the code will compile.

    The only feature I really miss (as I don't generally code C99), is the ability to declare a loop variable in a for statement, like this:
    >>for (int i = 0; i < 10; i++)
    This is much neater way of doing things.

    Sometimes, I declare variables within a specific block (ie, not at function level), just to keep things tidy. Like this:

    Code:
    void foo(void)
    {
       /* lots of code here */
    
      if (i == 10)
      {
        int j;
        /* More code */
      }
      
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 02:47 PM