Invalid Initialization

This is a discussion on Invalid Initialization within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 03: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, 08:45 PM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 06:37 AM

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