Thread: Opening files for use by multiple functions

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

    Opening files for use by multiple functions

    where do I intialize a file so it can be viewed by more that one function in the same program?

    I tried putting

    Code:
    FILE *in=fopen("C:\\cs10e\\input5.txt","r");
    above the main, but I get an illegal intiialization error, and if I put it twice, inside the two functions, it doesnt work properly (reading the data).

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can't do any work aside from simple initialization and declarations in the global namespace, try this instead:
    Code:
    #include <stdio.h>
    
    static FILE *in;
    
    void f ( void )
    {
      /* Blah blah with in */
    }
    
    int main ( void )
    {
      in = fopen ( "C:\\cs10e\\input5.txt", "r" );
      /* Blah blah with in */
      f();
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Handling multiple input files from command line
    By cnfwriter in forum C Programming
    Replies: 13
    Last Post: 08-25-2008, 08:07 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Beginner Question: Multiple source files
    By ironfistchamp in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2006, 02:19 PM
  5. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM