Opening a file in a function 
Author Message
 Opening a file in a function

I am learning C from a book and I have come across a problem in a
program I had to write.

The excersise was that I had to open a file and do a bunch of stuff with
it, so to make my program cleaner I decided to try to open the file in a
function.  However, when I do this I get a GPF.  I have debugged the
program and this is the code that is giving me the problem:  

#include<stdio.h>
#include<stdlib.h>

FILE *fp;
void open(void);

main()
{
open();

Quote:
}

void open(void)
{

  if ((fp = fopen("file1.txt","r"))==NULL)
  {
         puts("can't open file");
         exit(1);
  }
  else puts("opened succussfully");
  fclose(fp);

Quote:
}

Now if I skip the function and open the file in main it works fine.  Is
opening a file in a function something that's not allowed or is there
something wrong with my code that I am missing.  Please help.

p.s. I am using Borland 4.52 on Win95.



Tue, 22 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Eli,

Quote:

> I am learning C from a book and I have come across a problem in a
> program I had to write.

> The excersise was that I had to open a file and do a bunch of stuff with
> it, so to make my program cleaner I decided to try to open the file in a
> function.  However, when I do this I get a GPF.  I have debugged the
> program and this is the code that is giving me the problem:

> #include<stdio.h>
> #include<stdlib.h>

> FILE *fp;
> void open(void);

> main()
> {
> open();
> }

> void open(void)
> {

>   if ((fp = fopen("file1.txt","r"))==NULL)
>   {
>          puts("can't open file");
>          exit(1);
>   }
>   else puts("opened succussfully");
>   fclose(fp);

> }

> Now if I skip the function and open the file in main it works fine.  Is
> opening a file in a function something that's not allowed or is there
> something wrong with my code that I am missing.  Please help.

> p.s. I am using Borland 4.52 on Win95.

You might want to change the name of your function from open() to
something else.  There is a function called open() that exists in one of
the C libraries, and it may be including that one (instead of yours) in
the executable.  Seems like you'd get some sort of compiler
error/warning, tho.

Regards,
Stephen



Tue, 22 Aug 2000 03:00:00 GMT  
 Opening a file in a function

: I am learning C from a book and I have come across a problem in a
: program I had to write.

: The excersise was that I had to open a file and do a bunch of stuff with
: it, so to make my program cleaner I decided to try to open the file in a
: function.  However, when I do this I get a GPF.  I have debugged the
: program and this is the code that is giving me the problem:  

: #include<stdio.h>
: #include<stdlib.h>

: FILE *fp;
: void open(void);

Check that there isn't already a system call named open()
prototyped in stdio/stdlib (or just rename open to my_open
and see what happens).  It may be confusing the compiler/linker.

Will



Tue, 22 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Quote:

> The excersise was that I had to open a file and do a bunch of stuff with
> it, so to make my program cleaner I decided to try to open the file in a
> function.  However, when I do this I get a GPF.  I have debugged the
> program and this is the code that is giving me the problem:

> #include<stdio.h>
> #include<stdlib.h>

> FILE *fp;
> void open(void);

> main()
> {
> open();

        I think the code that might be causing you problems immediately fol-
lows the open() function call.

Quote:
> }

> void open(void)
> {
>   if ((fp = fopen("file1.txt","r"))==NULL)
>   {
>          puts("can't open file");
>          exit(1);
>   }
>   else puts("opened succussfully");
>   fclose(fp);
> }

        Notice that after you open the file, thereby initializing fp to point
to a valid FILE, you promptly close fp, thereby destroying the formerly valid
FILE.  When you return to your main program to use fp, it is invalid and
causes a GPF.

                                                -- Stone



Wed, 23 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Quote:


>: I am learning C from a book and I have come across a problem in a
>: program I had to write.

>: The excersise was that I had to open a file and do a bunch of stuff with
>: it, so to make my program cleaner I decided to try to open the file in a
>: function.  However, when I do this I get a GPF.  I have debugged the
>: program and this is the code that is giving me the problem:  

>: #include<stdio.h>
>: #include<stdlib.h>

>: FILE *fp;
>: void open(void);

>Check that there isn't already a system call named open()
>prototyped in stdio/stdlib

If there was it would be unlikely to be type compatible with the one defined
here, so the compiler would probably have complained.

Quote:
>(or just rename open to my_open
>and see what happens).

Right.

Quote:
> It may be confusing the compiler/linker.

One possibility is that the fopen() code in the library is making a call
to a system fucntion called open() however it ends up getting linked to
call the function defined here instead. That shouldn't happen in a
correctly implemented ANSI environment but that doesn't always stop it in
practice!

--
-----------------------------------------


-----------------------------------------



Wed, 23 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Quote:

> #include<stdio.h>
> #include<stdlib.h>

> FILE *fp;
> void open(void);

/* THIS IS ERROR
   you should read manual before creating a function with
   VERY SHORT AND SIMPLE NAME
!  there's a std fcn called open();
*/
Quote:

> main()
> {
> open();
> }

> void open(void)
> {

>   if ((fp = fopen("file1.txt","r"))==NULL)

/* THIS IS NOT ERROR
   it's the same, but it's clearer for me.
    if (!(fp = fopen("file1.txt","r")))
*/

Quote:
>   {
>          puts("can't open file");
>          exit(1);
>   }
>   else puts("opened succussfully");
>   fclose(fp);

> }
> p.s. I am using Borland 4.52 on Win95.

/* I was not satisfied with manual, i'm using DJGPP

steve.
*/



Tue, 29 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Quote:


> > #include<stdio.h>
> > #include<stdlib.h>

> > FILE *fp;
> > void open(void);
> /* THIS IS ERROR
>    you should read manual before creating a function with
>    VERY SHORT AND SIMPLE NAME
> !  there's a std fcn called open();
> */

It *not* a standard C function and therefore *not necessarily* an error.
There are C compilers that do not have a function named "open()",
and rightfully so. AFAIK this widely spread compiler extension
originated with Unix.

So you are partly right: if the compiler supports the "open()" function
as a language extension, you are invading the implementation name space.

Quote:
> >   if ((fp = fopen("file1.txt","r"))==NULL)
> /* THIS IS NOT ERROR
>    it's the same, but it's clearer for me.
>     if (!(fp = fopen("file1.txt","r")))
> */

And I prefer:
  fp = fopen( "file1.txt", "r" );
  if ( fp == NULL )
  {
  }
you see: style differs.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 29 Aug 2000 03:00:00 GMT  
 Opening a file in a function

|> So you are partly right: if the compiler supports the "open()"
|> function as a language extension, you are invading the
|> implementation name space.

Actually, it's the other way around; if your implementation is
reserving an identifer named "open", it is the implementation that
is invading *your* namespace (if the identifier is declared in one
of the standard C headers, that is.)

Regards,

--
Chris Engebretson - Raytheon STX Corporation | Ph#: (605)594-6829
USGS EROS Data Center, Sioux Falls, SD 57198 | Fax: (605)594-6940

Opinions are not those of  Raytheon Systems Company  or the USGS.



Tue, 29 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Quote:


> > void open(void);
> /* THIS IS ERROR
>    you should read manual before creating a function with
>    VERY SHORT AND SIMPLE NAME
> !  there's a std fcn called open();
> */

Huh. Practice what you preach; there is -not- a standard function called
open().

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Tue, 29 Aug 2000 03:00:00 GMT  
 Opening a file in a function


Quote:


>> > void open(void);
>> /* THIS IS ERROR
>>    you should read manual before creating a function with
>>    VERY SHORT AND SIMPLE NAME
>> !  there's a std fcn called open();
>> */

>Huh. Practice what you preach; there is -not- a standard function called
>open().

Despite this, it makes sense to be aware of identifiers which are likely
to be troublesome on some platforms, and stay away from them.

As it happens I don't believe 'open' poses that much of a problem -- on
all the systems I have access to it's declared in a non-standard header,
<fcntl.h>, and with common linker implementations the version in your
own code will be used in preference to the one in the library.

But there are function names to steer clear of: 'index' and 'bcopy'
spring to mind.

Someone is probably going to comment that "we needn't bother about broken
implementations", but IMO that's like marching across the street without
checking for traffic and then complaining from your hospital bed that it
was your right of way.  :-)

[Original poster: the problem with your code was almost certainly not to
do with the name of your 'open' function, but rather the misplaced call
to 'fclose' which has been pointed out by another respondent.]

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Wed, 30 Aug 2000 03:00:00 GMT  
 Opening a file in a function

Richard Stamp wrote in a message to All:

 RS> As it happens I don't believe 'open' poses that much of a problem
 RS> -- on all the systems I have access to it's declared in a
 RS> non-standard header, <fcntl.h>, and with common linker
 RS> implementations the version in your own code will be used in
 RS> preference to the one in the library.

Indeed, and that's exactly what causes a big problem on several
implementations. Functions like fopen() will call your open() instead
of the one in the library, and you can guess what the results of that
will be.

greetings,
Tom



Wed, 30 Aug 2000 03:00:00 GMT  
 Opening a file in a function



Quote:
>Richard Stamp wrote in a message to All:

> RS> As it happens I don't believe 'open' poses that much of a problem
> RS> -- on all the systems I have access to it's declared in a
> RS> non-standard header, <fcntl.h>, and with common linker
> RS> implementations the version in your own code will be used in
> RS> preference to the one in the library.

>Indeed, and that's exactly what causes a big problem on several
>implementations. Functions like fopen() will call your open() instead
>of the one in the library, and you can guess what the results of that
>will be.

Indeed. This was one of my surprising experiences with Turbo/Borland
C.

#include <stdio.h>

int open() { fputs("hello",stderr);  return -1; }

int main(void) {
  FILE *fp=fopen("c:/autoexec.bat","r");
  /**/

Quote:
}

Oops...

Regards
Horst

   *** Las orillas del Nahuel Huapi ***



Thu, 31 Aug 2000 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. opening a file in a function

2. can't open file: to many files open

3. Difference between MRU File Open and Regular File Open

4. Basic Open File, Save/ Save as functions for dos word processor

5. File Open function trouble....

6. Opening a file, inside a separate Function

7. passing a open file to a function

8. HOW TO Filter Files Without File Extention in Open File Dialog Box

9. File extension filtering for the file dialog presented for file opening

10. Problem opening a file for read while it is open for write

11. opening a file using open method

12. Problem with opening binary files using open()

 

 
Powered by phpBB® Forum Software