Find the answer to your Linux question:
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.
  1. #1
    Just Joined! legendbb's Avatar
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    27

    Red face 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.


  2. #2
    Just 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.

    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
    This code is checked, and works.
    Code:
    #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)
    Br
    Haze

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    16
    @legendbb

    What about just trying it and give us the result.

    Br
    Haze

  6. #5
    Just Joined! legendbb's Avatar
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    27
    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.html
    Last edited by legendbb; 09-02-2011 at 05:34 PM. Reason: solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •