Thread: Invalid Initialization

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Invalid Initialization

    Code:
     FILE *indata=fopen("c:\\input.txt","r");
    give me an error when I put it outside main, I need to use the file in some fucntions. Why?

    but when I put
    Code:
     FILE *indata
    outside main, and
    [code] indata=fopen("c:\\input.txt","r"); [code]
    inside main, the program and fucntions run perfectly, why can't I put the whole thing outside main?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > give me an error when I put it outside main
    Well of course, code outside of a function is meaningless.

    > I need to use the file in some fucntions. Why?
    So open the file in main(), then pass the FILE* as a parameter to other functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Or if you INSIST on a global variable ( god knows why) just do file = fopen(..); in main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM