opening file...

opening file...

Post by D'laila Pereir » Sun, 03 Dec 2000 04:00:00



while trying to open a file using piece of code -

if((fd=open(filename,"O_RDONLY",0))<0){
perror("error opening file");
close(fd);
exit(1);

Quote:}

i get the error message - error opening file. how should i overcome
this???
 
 
 

opening file...

Post by Floyd Davidso » Sun, 03 Dec 2000 04:00:00



>while trying to open a file using piece of code -

What is the definition of variable fd?
What is the definition of variable filename?
What headers have you included?

Have you read the man page for open(2) ?

Quote:>if((fd=open(filename,"O_RDONLY",0))<0){

Did this complile with no warning???  If it did you should
read the documentation for your compiler to determine how to
increase the level of warnings.  There is certainly the
error that the second argument to open(2) is not a pointer
to char which is what you are passing it; but if no error
about prototype mismatch was produced then you most likely
do not have a prototype for open(2) in scope and that would
be due to not including the appropriate headers (see the
man page for open(2) on your system to determine which
headers are needed).  

In any case... crank up the warning level for your compiler.
For example, using the GNU-C gcc compiler with no command
line options,

  gcc foo.c

results in no warnings.  However,

  gcc -W -Wall foo.c

results in a "passing arg 2 of `open' makes integer from pointer
without a cast" warning if the proper headers are included, and
"implicit declaration of function `open'" when the headers are
missing.  Clearly there is good reason to pay attention to these
warning!

Specifically, your problem can be resovled by removing the
double quotes from around the O_RDONLY macro.  Also note that
the third argument to open(2) is meaningless, and should not be
used, unless the O_CREAT flag is or'd into the second argument.
That is not the case here, so you should only use two arguments
to open(2).

Quote:>perror("error opening file");
>close(fd);

There is no point in closing a file which was never opened.

Quote:>exit(1);
>}

Your code also needs to be formatted with indents; at least if
you wish others to try reading it.

Quote:>i get the error message - error opening file. how should i overcome
>this???

The man page for open(2) lists something over a dozen different
values for errno after a failure.  If you read the man pages for
the functions you use, and then take advantage of the
information made available, you will be better able to trouble
shoot your programs.

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>


 
 
 

opening file...

Post by Michael Buria » Mon, 04 Dec 2000 08:13:28



> while trying to open a file using piece of code -

> if((fd=open(filename,"O_RDONLY",0))<0){

                       ^        ^

Quote:> perror("error opening file");
> close(fd);
> exit(1);
> }

> i get the error message - error opening file. how should i overcome
> this???

do a

 grep "O_RDONLY" `locate fcntl.h`

to see what O_RDONLY means, it's no string (it's a symbolic constant for
a number)
and so putting it into "" is quite useless.

binary OR'ing strings together wouldn't make much sense either ;)

 
 
 

opening file...

Post by Michael Buria » Mon, 04 Dec 2000 08:26:24



> > i get the error message - error opening file. how should i overcome
> > this???

forgot one thing,

this book might be a valueable reading
( in case you haven't already )

W. Richard Stevens
Advanced Programming in the Unix Enviroment

 
 
 

opening file...

Post by Nikolai Hristo » Mon, 04 Dec 2000 04:00:00


Quote:> W. Richard Stevens
> Advanced Programming in the Unix Enviroment

I would rather suggest
Beginning Linux Programming 2nd Ed.

Steven's book is more of a reference and gives nice ideas about how to do
thing but AFTER you know the basics.

Nick
---------------------
Nikolai Hristov
Slayter Box 1194
Denison University
Granville OH 43023

 
 
 

1. Open File Manager (open file backup) for Linux?

Is there a way to back up open files on a Linux machine?

I've been looking into setting up a file server using Linux using mars-nwe, to
service a couple DOS and NT clients.

If I use NT Server I can get a program called Open File Manager from St.
Bernard Software that does the trick.

Anyone know of such a program or feature under Linux??

2. upgrading 1.01 -> 1.03 (SLS)

3. OpenWin Filemgr: 'cannot open file: too many files open' ?

4. Help with PPP?

5. Help: What controls number of files a user can open (file open command)?

6. Generating aout binaries on ELF system

7. map a process open file to a open socket ?

8. Removed some pkg and now I can't telnet to machine

9. Can't open a broadcast socket because there are too many open files?

10. cannot open /dev/mem: too many open files??

11. Open files vs. open inodes

12. Getting open file count of a file?

13. The GIMP error mesg (can't open file as "xwd" file)