LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-27-2007, 08:31 PM   #1
oliviapesayco
LQ Newbie
 
Registered: Jun 2007
Posts: 10

Rep: Reputation: 0
system call


Please tell me what is wrong with this code... There are no errors, it creates the file, but it does not write to the file:

#include<stdio.h>
#include<fcntl.h>

main()
{
char *p = ("hello world");
int fp;

fp = open ("samplex.c", O_CREAT, 0666);
write (fp,p,11);
fork ();
}
 
Old 06-27-2007, 10:21 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
check out man 2 open

Quote:

The parameter flags is one of O_RDONLY, O_WRONLY or O_RDWR which
request opening the file read-only, write-only or read/write, respec-
tively, bitwise-or’d with zero or more of the following:

O_CREAT
...
I compiled and ran the following:

Code:
int main(void)
{
        char *p = ("hello world");
        int fp;

        fp = open ("samplex.c", O_WRONLY | O_CREAT, 0666);
        write (fp,"hello world",11);
        fork ();
        return 0;
}
yielding "hello world" as expected.
 
Old 06-27-2007, 10:22 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
probably the lack of a close(). The string "Hello World." is probably short enough that the OS cached the write, but then you never properly closed the string, so the cache may not ever be written to disk. And, out of curiosity, what is the purpose of the fork()?

Regarding bartonski's comment: Umm, oh yeah, that too.
 
Old 06-28-2007, 09:39 AM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by PatrickNew
probably the lack of a close(). The string "Hello World." is probably short enough that the OS cached the write, but then you never properly closed the string, so the cache may not ever be written to disk.
Quick programmer's trick:

Whenever you have *anything* that can be opened and closed, eg. open and closed braces, malloc/free, fopen/fclose, open and close perens... you get the picture... write both at the same time, then write your code between them. This is guaranteed to save you hours of debugging.

Caveat to the above:

If you have some sort of branch make sure that any resources that you have open get closed regardless of how the conditional turns out.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I include my own system call in file system driver kgp Linux - Kernel 3 06-06-2007 10:41 AM
system system call usage sureshkellemane Programming 3 03-20-2007 12:41 AM
list file system linux supports "please give the command or system call" varun_shrivastava Linux - General 4 01-09-2007 07:28 AM
Calling a system call from a system call? Loc_8 Programming 0 09-16-2006 04:21 PM
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 01:37 PM


All times are GMT -5. The time now is 02:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration