I have a problem with creating a file in C. I would like to open existing file (or create one if it doesn't exist) and write something into it:
/* open the file */
if ((shfd = open(argv[2], O_CREAT | O_RDWR, 0677)) %26lt; 0)
my_error("open failed");
/* map a portion of the file to buffer in memory */
if ((mem = mmap(0,SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, shfd, 0)) == (void *)-1)
my_error("mmap failed");
sprintf(mem, "%d", getpid());
However, when the file doesn't exist and is created, its empty, and I get "bus error" message at the last line... If I try to write something to the file that already exists and is empty, I get the same message. If it exists and is not empty, it works fine...
Could anyone please help? I would really appreciate any help, since I don't know why I'm getting that error...
Thanks a lot for your time.
Hi! How do I open a file in C and create one if it doesn't exist?
You can't extend a file with mmap(). You can map
memory that you might write into, but when you close
or fsync the file, it will NOT be extended.
The historical way to do this is to lseek to the position
you want and write a single byte. The filesystem will
arrange that the intervening bytes will look like zeros.
I've modified your program snippet to open a file,
mmap it, and scribble in a bunch of bytes -- while
preserving whatever is there. It's not particularly
clever, but it should show most of the details you're
looking for. You can contact me through YA if you
have questions.
#include %26lt;sys/types.h%26gt;
#include %26lt;sys/stat.h%26gt;
#include %26lt;fcntl.h%26gt;
#include %26lt;sys/stat.h%26gt;
#include %26lt;unistd.h%26gt;
#include %26lt;sys/mman.h%26gt;
#include %26lt;stdio.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;stdlib.h%26gt;
int
main(int argc, char* argv[])
{
int shfd;
struct stat statbuff;
char *filename = argc%26gt;1 ? argv[1] : "testfile";
int mmapsize = argc%26gt;2 ? atoi(argv[2]) : 100000;
char *mem;
/* open the file */
if ((shfd = open(filename, O_CREAT | O_RDWR, 0677)) %26lt; 0)
perror("open failed"), exit(-1);
/* if it is too small, we will extend the file for the mmap */
if (fstat(shfd, %26amp;statbuff) != 0)
perror("stat"), exit(-1);
if (statbuff.st_size %26lt; mmapsize) {
if (lseek(shfd, mmapsize-1, SEEK_SET) == (off_t)-1)
perror("lseek"), exit(-1);
if (write(shfd, "\0", 1) %26lt; 0)
perror("write"), exit(-1);
}
/* map a portion of the file to buffer in memory */
if ((mem = mmap(0, mmapsize, PROT_READ | PROT_WRITE, MAP_SHARED, shfd, 0)) == MAP_FAILED)
perror("mmap failed"), exit(-1);
/* scribble on the file *after* whatever was already there */
if (statbuff.st_size %26lt; mmapsize)
memset(mem+statbuff.st_size, 'X', mmapsize-statbuff.st_size);
if (close(shfd) %26lt; 0)
perror("close"), exit(-1);
return 0;
}
Reply:Hmmm, I dont follow how you are trying to do it but this is how I always do it.
void main()
{
FILE *Pfile;
char FileName[] = "nameoffile";
char buffer[];
//open file or create if it doesnt exsist
Pfile = fopen(FileName, "w"); // w = open for write
// r = open for read
// wb = open binary for write
//fill buffer with what you want to write to file
strcat(buffer, "stuff you want to add to file");
//write buffer to the file
fwrite(%26amp;buffer, strlen(buffer), 1, Pfile);
//DONT forget to close the file when done!!
fclose(Pfile);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment