Results 1 to 5 of 5
Instead of using fopen, I'd like to use open() and write() to fill a file.
How does the size limit work? Do I have to use lseek() & write(fd, "", ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-01-2011 #1
open() file length limit? Need to set?
Instead of using fopen, I'd like to use open() and write() to fill a file.
How does the size limit work? Do I have to use lseek() & write(fd, "", 1) to set the file length, then can keep writing to the opened file descriptor?
What happens if not set?
Remember read something like "file storage memory is not guaranteed to be continuous..." but no more details remembered.
Will read more, but if someone has answer right away, very very much appreciated.
- 09-01-2011 #2Just Joined!
- Join Date
- Aug 2011
- Posts
- 16
Hi
With the size limit you can specify, how much data you want to write into the file in byte.
This code is checked, and works.Code:write(pointer, "foobar", 1); //result will be only f in the file write(pointer, "foobar", sizeof("foobar")); //result will be foobar in the file
BrCode:#include <stdio.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> void main(void) { int val, pointer; pointer = open("test", O_WRONLY); write(pointer, "foobar", sizeof("foobar")); close(pointer); } //remember to create an empty test file first (echo "" >> test)
Haze
- 09-02-2011 #3
I don't know what size limit you're talking about. You open() the file, and then just write() to it: the file will be resized as necessary. In particular, the system keeps track of the offset into a file. If you open a new file and write three bytes to it, the offset is now 3. You can also set the offset manually by using lseek(). Anytime you write starting at a particular offset, the file on disk will be modified as necessary to accept that offset.
Haze's code shows how to combine open() and write(). The only thing that I'll say about that code is that you can use the O_CREAT flag to open() to create the file if it doesn't already exist.
What size limit are you talking about? Filesystems may impose file size limits (for instance, FAT32 is well known for not supporting files over 4GB), but this is something different.
- 09-02-2011 #4Just Joined!
- Join Date
- Aug 2011
- Posts
- 16
@legendbb
What about just trying it and give us the result.
Br
Haze
- 09-02-2011 #5
Great thanks to you for working example code and in detailed explanation.
File size must be preset when working with mmap()
Found a good reference: gnu.org/s/hello/manual/libc/File-Size.htmlLast edited by legendbb; 09-02-2011 at 04:34 PM. Reason: solved


Reply With Quote
