Fighting to learn!

  • Thread starter =?iso-8859-1?q?Fredrik_H=E5kansson?=
  • Start date
?

=?iso-8859-1?q?Fredrik_H=E5kansson?=

Hello!

Please explain I'm trying to learn. I try to write the string /tmp/duken
into a file named /tmp/duken just for learning. I know there are other
functions like fopen() and so on but why is the below not working??

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


main()
{
int fd, wr, end, n;
const char *file = "/tmp/duken";
n = strlen(file);
printf("The lenght is %d \n",n);
if ((fd = open(file, O_CREAT, S_IRWXU)) < 0)
printf("Can't open %s", file);
if ((wr = write(fd, file, n)) <= 0)
printf("Unable to write to %s %d was the error\n",file,wr);
fsync(fd);
end = close(fd);
return end;
}
 
B

boa

Fredrik said:
Hello!

Please explain I'm trying to learn. I try to write the string /tmp/duken
into a file named /tmp/duken just for learning. I know there are other
functions like fopen() and so on but why is the below not working??

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


main()
{
int fd, wr, end, n;
const char *file = "/tmp/duken";
n = strlen(file);
printf("The lenght is %d \n",n);
if ((fd = open(file, O_CREAT, S_IRWXU)) < 0)

if ((fd = open(file, O_WRONLY | O_CREAT, S_IRWXU)) < 0)

printf("Can't open %s", file);
if ((wr = write(fd, file, n)) <= 0)
printf("Unable to write to %s %d was the error\n",file,wr);
fsync(fd);
end = close(fd);
return end;
}


boa
 
?

=?iso-8859-1?q?Fredrik_H=E5kansson?=

Hey thanx!

When do you want to use fopen and when do you want to use open??

Fredrik
 
J

Jens.Toerring

Fredrik Håkansson said:
When do you want to use fopen and when do you want to use open??

You use fopen() and fprintf() or fwrite() when you want to write
portable C programs since neither open() nor write() are standard
C functions, so you have no guarantee that they will exist on a
different platform or accept the same arguments or behave in the
same way. So, unless you have a very good reason not to use the
standard C functions avoid them. Basically, the only times I use
open(), write() or read() is when dealing with Unix device files
(because of the finer grained control these functions allow) or
have to do some Unix (POSIX) specific stuff. But nearly everything
your program does can be easily done with fopen() and fprintf()/
fwrite(), the exceptions being setting the permission bits when
opening the file and doing what fsync() does (another non-standard
function, but I would guess fflush() would also do in your case
if you think it's necessary at all).

Regards, Jens
 
?

=?iso-8859-1?q?Fredrik_H=E5kansson?=

Ahh good explanation how do i know if I'm using a standard C function??
 
J

Jens.Toerring

Fredrik Håkansson said:
Ahh good explanation how do i know if I'm using a standard C function??

Since you seem to be using a Unix system have a look at the man
pages and go to the "CONFORMING TO" section - you usually should
find some notice there that the function is conforming if "ANSI C"
or "ANSI X3.159-1989" or "ISO 9899" or "ISO/IEC 9899:1999"
or "ISO C99" is mentioned (that are a few examples I found, these
are all different names for the (old) C89 standard and the (new)
C99 standard). Or get yourself a good book about C, there you
should find a list of all these functions.

BTW, could you be so kind to stop top-posting and instead put
your messages _below_ what you are responging to (after removing
what isn't relevant anymore)? Many people here (and also in other
technical newsgroups) find top-posting rather annoying because it
makes it hard to figure out to what you are responding. Thanks.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top