Fighting to learn!

Discussion in 'C Programming' started by =?iso-8859-1?q?Fredrik_H=E5kansson?=, Jul 24, 2004.

  1. 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;
    }
    =?iso-8859-1?q?Fredrik_H=E5kansson?=, Jul 24, 2004
    #1
    1. Advertising

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

    boa Guest

    Fredrik Håkansson wrote:
    > 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
    boa, Jul 24, 2004
    #2
    1. Advertising

  3. Hey thanx!

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

    Fredrik



    On Sat, 24 Jul 2004 07:45:35 +0000, boa wrote:

    > Fredrik Håkansson wrote:
    >> 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?=, Jul 24, 2004
    #3
  4. =?iso-8859-1?q?Fredrik_H=E5kansson?=

    -berlin.de Guest

    Fredrik Håkansson <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Jul 24, 2004
    #4
  5. Ahh good explanation how do i know if I'm using a standard C function??

    On Sat, 24 Jul 2004 08:52:28 +0000, Jens.Toerring wrote:

    > Fredrik Håkansson <> wrote:
    >> 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?=, Jul 24, 2004
    #5
  6. =?iso-8859-1?q?Fredrik_H=E5kansson?=

    -berlin.de Guest

    Fredrik Håkansson <> wrote:
    > On Sat, 24 Jul 2004 08:52:28 +0000, Jens.Toerring wrote:
    >> Fredrik Håkansson <> wrote:
    >>> 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.
    >>

    > 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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Jul 24, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    7,237
  2. Leif K-Brooks

    Implementing Web-based RPG fighting?

    Leif K-Brooks, May 21, 2005, in forum: HTML
    Replies:
    1
    Views:
    589
    Travis Newbury
    May 21, 2005
  3. Replies:
    16
    Views:
    609
    Jarek Zgoda
    Aug 1, 2005
  4. David MacQuigg

    Fighting Spam with Python

    David MacQuigg, Aug 25, 2005, in forum: Python
    Replies:
    10
    Views:
    543
    David MacQuigg
    Aug 27, 2005
  5. Brendan
    Replies:
    8
    Views:
    242
    Brendan
    Dec 4, 2005
Loading...

Share This Page