# Opening files for use by multiple functions

This is a discussion on Opening files for use by multiple functions within the C Programming forums, part of the General Programming Boards category; where do I intialize a file so it can be viewed by more that one function in the same program? ...

1. ## 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. 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();
}

Popular pages Recent additions