473,511 Members | 15,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

open file in c / create if file doesn't exist

Hi everyone!

I have a problem with trying to open a file in C.

The following line in my code is suppoed to open a specified file if it
exists and create a new one with this name if one doesn't exist:

if ((shfd = open(argv[2], O_CREAT | O_RDWR, 0644)) < 0)
my_error("open failed");

If I try to open a file that doesn't exist I get an error:
Bus error

And when it creates a file it sets permissions to -rw------ instead of
-rw-r--r-- as I would hope... Is there something I'm not getting about
how O_CREATE works?...

However, if I try to open existing file that is not empty, it works
fine...

Course anyone please help me?

thank you

Apr 6 '06 #1
4 39104
another important thing - it given an error because I map a portion of
the file in memory, as in

if ((mem = mmap(0,
SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED,
shfd,
0)) == (void *)-1)
my_error("mmap failed");
and then I try to write something into it:

sprintf(mem, "%d", getpid());

The file remains empty and I get the "bus error" message.
any help would be greatly appreciated...

Thanks for your time

Apr 6 '06 #2
no*****@yahoo.com wrote:
I have a problem with trying to open a file in C.

The following line in my code is suppoed to open a specified file if it
exists and create a new one with this name if one doesn't exist:

if ((shfd = open(argv[2], O_CREAT | O_RDWR, 0644)) < 0)
my_error("open failed");

If I try to open a file that doesn't exist I get an error:
Bus error

And when it creates a file it sets permissions to -rw------ instead of
-rw-r--r-- as I would hope... Is there something I'm not getting about
how O_CREATE works?...


you're going to want to take this up in comp.unix.programmer. the
answer you're probably going to get will include the words umask and
ftruncate.

Apr 6 '06 #3
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

shfd = open(argv[2],
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (shfd < 0) {
perror("open");
// exit code here
}
// proceed code here

Apr 13 '06 #4
int rv;
// open fd (last post)

rv = lseek(shfd, SIZE+1, SEEK_SET);
if (rv < 0) {
// handle error
}

rv = write(shfd, "", 1);
if (rv < 0) {
// handle error
}

rv = lseek(shfd, 0, SEEK_SET);
// handle error
}

mem = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shfd, 0);
if (mem == MAP_FAILED) {
// handle error
}

Apr 13 '06 #5

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

Similar topics

6
8752
by: haynesc | last post by:
Hi, I'm having a problem where when trying to open a file in write mode, I get an IOError stating no such file or directory. I'm calling an external program which takes an input file and...
11
3585
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
4
3634
by: Frank | last post by:
Could someone tell me how to open a file at run time that I didn't know the name of at compile time? I know how to open a file at compile time when I know what the name is going to be. FILE...
9
13403
by: Charles F McDevitt | last post by:
I'm trying to upgrade some old code that used old iostreams. At one place in the code, I have a path/filename in a wchar_t string (unicode utf-16). I need to open an ifstream to that file. ...
5
1148
by: VB Programmer | last post by:
I want to write to a simple text file. If it doesn't exist I want to create it first. Here is my code (portion): Imports System.IO ' at the top of the class : : Dim strFileName As String =...
52
7458
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
16
2207
by: Gary Wessle | last post by:
Hi please help with this. std::fstream iofs( f.c_str(), std::ios::in|std::ios::out ); std::cout << f << '\n' << iofs.is_open() << std::endl; puts out...
2
1387
by: xahlee | last post by:
In this week i wrote a emacs program and tutorial that does archiving a website for offline reading. (See http://xahlee.org/emacs/make_download_copy.html ) In the process, i ran into a problem...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
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
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
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
5673
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,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.