Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Fighting to learn!

Reply
Thread Tools

Fighting to learn!

 
 
=?iso-8859-1?q?Fredrik_H=E5kansson?=
Guest
Posts: n/a
 
      07-24-2004
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;
}
 
Reply With Quote
 
 
 
 
boa
Guest
Posts: n/a
 
      07-24-2004
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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Fredrik_H=E5kansson?=
Guest
Posts: n/a
 
      07-24-2004
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


 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      07-24-2004
Fredrik Håkansson <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
=?iso-8859-1?q?Fredrik_H=E5kansson?=
Guest
Posts: n/a
 
      07-24-2004
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 <(E-Mail Removed)> 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


 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      07-24-2004
Fredrik Håkansson <(E-Mail Removed)> wrote:
> On Sat, 24 Jul 2004 08:52:28 +0000, Jens.Toerring wrote:
>> Fredrik Håkansson <(E-Mail Removed)> 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 ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cambodian Midget Fighting League lyman.1.hymen@spamgourmet.com HTML 6 07-12-2005 11:18 PM
Guardian Backpacks - First Crime-Fighting Backpack Designed to Save Lives posh Computer Support 2 06-07-2005 04:58 PM
Implementing Web-based RPG fighting? Leif K-Brooks HTML 1 05-21-2005 03:08 PM
Fighting abuse with abuse Mara Computer Support 70 03-24-2005 08:30 PM
REVIEW: "Malware: Fighting Malicious Code", Ed Skoudis Rob Slade, doting grandpa of Ryan and Trevor Computer Security 0 02-19-2004 04:17 PM



Advertisments