473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Fighting to learn!

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;
}
Nov 14 '05 #1
5 1499
boa
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
Nov 14 '05 #2
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


Nov 14 '05 #3
Fredrik Håkansson <fr*****@spamme.younix.se> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
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 <fr*****@spamme.younix.se> 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


Nov 14 '05 #5
Fredrik Håkansson <fr*****@spamme.younix.se> wrote:
On Sat, 24 Jul 2004 08:52:28 +0000, Jens.Toerring wrote:
Fredrik Håkansson <fr*****@spamme.younix.se> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Rob | last post by:
i know javascript, vbscript, asp css and alot more and im only 14 i was wondering which is easier to learn php or cgi. any help?
42
by: Bicho Verde | last post by:
I have now free time and money to do what I want :-) I have some basic skills in programming (C, Pascal, Macromedia Actionscript) but don't know exactly what to do in the world of programming. And...
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
16
by: skip | last post by:
Twice today I responded to rude messages (once here, once on the SpamBayes list) whose authors didn't deserve the benefit of my time. In both cases, other people rightfully responded with some...
21
by: TAM | last post by:
Hi, I read that ASP.NET uses VB.NET instead of VBScript. I also read that ASP.NET is a subset of VB.NET. So if I learn VB.NET first then do I have the knowledge for programming ASP.NET...
31
by: anand devarajan | last post by:
hi friends, im anand im just a beginner in c learning for the past two weeksnow i can write simple prgs can anyone help me to get well known to c lang so that i should able to write even tough...
0
by: baseballswim123 | last post by:
Friend, I'm always looking for good and intelligent individuals like you to visit my website, www.ChezBrandon.com, and it has pictures of beautiful women, information about aged clones, and a...
0
by: karajitsu | last post by:
you can find it here the World Ultimate Fighting at http://www.dirtyfighting.blogspot.com
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.