difficulties with open()

difficulties with open()

Post by Greg Marti » Wed, 21 Jul 1999 04:00:00



I'm having trouble with open(). I've tried a couple of things by isolating
it from the program I'm writing. Here's some code and description of what
I've tried.

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 char fname[FILENAME_MAX];

/* the filename I've been using is index.html. If I declare
char fname[11]; then the code works. Likewise if I declare
char fname[] = "index.html"; */

 int fd;

 fgets(fname, sizeof fname, stdin);
/* I have tried write() instead of fgets() but it doesn't help */

 if((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC |           O_APPEND,
S_IRWXU)) < 0)
 {
  perror("open");
  exit(EXIT_FAILURE);
 }
 return 0;

Quote:}

TIA and regards,
Greg Martin
 
 
 

difficulties with open()

Post by Kenneth C Stah » Wed, 21 Jul 1999 04:00:00



> I'm having trouble with open(). I've tried a couple of things by isolating
> it from the program I'm writing. Here's some code and description of what
> I've tried.
> #include <fcntl.h>
> #include <unistd.h>
> #include <sys/stat.h>
> #include <stdio.h>
> #include <stdlib.h>
> int main(void)
> {
>  char fname[FILENAME_MAX];

> /* the filename I've been using is index.html. If I declare
> char fname[11]; then the code works. Likewise if I declare
> char fname[] = "index.html"; */
>  int fd;
>  fgets(fname, sizeof fname, stdin);
> /* I have tried write() instead of fgets() but it doesn't help */

>  if((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC |           O_APPEND,
> S_IRWXU)) < 0)
>  {
>   perror("open");
>   exit(EXIT_FAILURE);
>  }
>  return 0;
> }

try doing a: ls -q index*

You'll see that there is a character after .html.

fgets() does not strip off the line feed that comes in from stdin. You need to
strip it off before you try to open a file using the name that you type in.

Ken

 
 
 

difficulties with open()

Post by Bill Raus » Wed, 21 Jul 1999 04:00:00




> I'm having trouble with open(). I've tried a couple of things by isolating
> it from the program I'm writing. Here's some code and description of what
> I've tried.

> #include <fcntl.h>
> #include <unistd.h>
> #include <sys/stat.h>
> #include <stdio.h>
> #include <stdlib.h>

> int main(void)
> {
>  char fname[FILENAME_MAX];

> /* the filename I've been using is index.html. If I declare
> char fname[11]; then the code works. Likewise if I declare
> char fname[] = "index.html"; */

>  int fd;

>  fgets(fname, sizeof fname, stdin);
> /* I have tried write() instead of fgets() but it doesn't help */

>  if((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC |           O_APPEND,
> S_IRWXU)) < 0)
>  {
>   perror("open");
>   exit(EXIT_FAILURE);
>  }
>  return 0;
> }

> TIA and regards,
> Greg Martin

You need to remove the newline character from the end of the filename.
fgets() always returns a complete line, including the newline, if it fits
in the buffer you give it. Try adding:   fname[strlen(fname)-1] = '\0';  
right after your fgets() call.

--
Bill Rausch
bill at numerical dot com
UNIX, Mac, Windows software development

 
 
 

difficulties with open()

Post by Rainer Temm » Thu, 22 Jul 1999 04:00:00



> I'm having trouble with open(). I've tried a couple of things by isolating
> it from the program I'm writing. Here's some code and description of what
> I've tried.
> ... some code zapped ..
>  char fname[FILENAME_MAX];
>  int fd;
>  fgets(fname, sizeof fname, stdin);
> /* I have tried write() instead of fgets() but it doesn't help */
>  if((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC |           O_APPEND,
> S_IRWXU)) < 0)
> ...some code zapped ...

Hi Greg,
...
You don't tell us what the exact problem with open is...so I assume it failes with
some error (what did perror() say?)
...
Generally: Don't forget, that fgets() doesn't eliminate the '\n' char in the inputstring.
If you don't wipe it out yourself, you'll try to open a file with a name containing the
new-line char. This might be possible on some systems, and might fail on others.
...
Another point might be the TRUNCate and APPEND flag toghether. I tried this on my system
(sinix 5.43) and it works...but it doesn't seem to make much sense...You either want to
append (in this case you shouldn't truncate), or erase the file and write a new one (int this case
append is not needed, because the file is empty after truncation).
...
Regards Rainer
 
 
 

difficulties with open()

Post by Alberto Chess » Thu, 22 Jul 1999 04:00:00




Quote:> I'm having trouble with open(). I've tried a couple of things by
isolating
> it from the program I'm writing. Here's some code and description of what
> I've tried.

> #include <fcntl.h>
> #include <unistd.h>
> #include <sys/stat.h>
> #include <stdio.h>
> #include <stdlib.h>

> int main(void)
> {
>  char fname[FILENAME_MAX];

> /* the filename I've been using is index.html. If I declare
> char fname[11]; then the code works. Likewise if I declare
> char fname[] = "index.html"; */

>  int fd;

>  fgets(fname, sizeof fname, stdin);

Just use gets() [bad] or remove the trailing '\n':
  {
    char *p=filename;
    if ((p=strrchr(filename, '\n')))
      *p=0;
  }

fgets() leave '\n' at the end of the filename and open() will look for
"index.html\n" instead of "index.html"

 
 
 

1. Subject: Difficulty installing Open Office 641c - Need help!!

While attempting to install Open Office 641c on a newly purchased
laptop the install gets to the area where the install directory is
specified and when the next button is hit the x windows system
freezes.  I cant even ctrl-alt-F? to another tty.  Anyone have any
insight into what's going on?  BTW I attempted to install the
StarOffice package and encountered the same thing.

2. Prom2300+ & LINUX

3. Help me catch this mouse...

4. Open Server 5 PPP difficulties

5. Web Mirroring Utility

6. Gridware difficulties and NFS difficulties (?)

7. (fwd) TCP bug in 2.0.30 - increasing the sequence number after FIN

8. Opening PackagesI'm having difficulty opening python or any others through KDE. THe files are

9. OT Open source = Open mind?

10. What percentage of open-source programmers are paid for working on open-source projects?

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

12. Open Desktop VS Open Enterprise