Thread: GCC compile error coz of file pointer

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    GCC compile error coz of file pointer

    I get a compile error when it runs into my file pointer stuff. I am compiling using GCC, and am compiling .c files.

    Code:
    MAIN.C
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main()
    {
        printf("Hi");
        message();
        FILE * Tmp;
        Tmp = fopen("hihello.txt", "a");
        fprintf(Tmp, "Hi\nHello");
        fclose(Tmp);
    }
    Code:
    SUB.C
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int message()
    {
        printf("\nHello");
    }
    In function main:
    Parse error before *
    Tmp undeclared

    ...that sounds like I'm not using the correct header file?

    Contents of my compile batch file:

    Code:
    @echo off
    cls
    path c:\mingw\bin;c:\windows\command
    gcc -c -g main.c
    gcc -c -g sub.c
    gcc -o pgm main.o sub.o
    The world is waiting. I must leave you now.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    In C all variables must be declared at the beginning of the block. This should work better:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE * Tmp;
        printf("Hi");
        message();
        Tmp = fopen("hihello.txt", "a");
        fprintf(Tmp, "Hi\nHello");
        fclose(Tmp);
    }
    Also, message is defined as returning an int yet you don't actually return anything. And be sure to check Tmp so that you can be sure fopen didn't fail.

    -Prelude
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    printf("Hi");
    message();
    FILE * Tmp;
    This is C++ code - declarations anywhere


    FILE * Tmp;
    printf("Hi");
    message();
    This is C code, declarations at the start

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    thanks

    Thank you Salem and Prelude.

    Prelude:
    Also, message is defined as returning an int yet you don't actually return anything.
    Returing something, with just a simple fprint call?

    And be sure to check Tmp so that you can be sure fopen didn't fail.
    Isn't that what append is for? If it doesn't exist, create it.
    The world is waiting. I must leave you now.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Returing something, with just a simple fprint call?
    Exactly, either declare the function as void or return the return value of printf.

    >Isn't that what append is for? If it doesn't exist, create it.
    The file is created if possible, but you can't assume that it will be.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I compiled and ran my program. ( Upon adding on a small piece of code ). It executes fine, but upon completion, Windows says, "This program has performed an illegal option". I have 2 sub source files, one is on my previous post, the other I don't have posted yet is pretty much the same. I return 0 in both of these functions, re-compile, and the program exexcutes fine, and doesn't cause any errors upon termination.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. header file compile error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 02-23-2002, 06:28 AM