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 4 39130
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 no*****@yahoo.c om 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.progr ammer. the
answer you're probably going to get will include the words umask and
ftruncate.
#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
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
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 produces an output file
repeatedly, simulating the input file separately for each replicate.
The error occurs when trying to open the input file to write out the
new data. The problem is difficult to reproduce since it only shows up
once every few...
|
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 rules when it comes to file paths.
A lot of Microsoft installers for example, and also installers of other companies, do not work because they handle paths in the following manner:
|
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 *p_afile;
if((p_afile = fopen("shoppinglist.txt", "r")) == NULL)
{
fprint("Could not open file");
|
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. But the open() on ifstream only
takes char * strings (mbcs?).
In old iostreams, I could _wopen() the file, get the filedesc, and call
attach() on the ifstream.
|
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 = "C:\MyFile.txt"
' create file if not exist
If Not File.Exists(strfilename) Then
File.CreateText(strFileName)
| |
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 file doesn't exist.Now
tell me what should I do!
Thanks
|
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
****************************************************************
pair_status/myPair
|
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 with the unix “cp” utility. I've
been a unix admin for Solaris during 1998-2004. Even the first time i
learned about cp, i noticed some pecularity. But only today, i thought
about it and wrote it out exactly what's going on.
Here's a...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |