Thread: Generating file of a certain size

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Generating file of a certain size

    Not sure if this is a good way of generating a 15MB file from opening a file and writing to a new file. The "file.in" is just a dummy file of size 50mb created from
    Code:
    dd if=/dev/zero of=yourfilename.test bs=52428800 count=1
    .

    Is there a more optimized way of generating a file of size 15MB given the code below? I use the code below and it does generate a file of size 15.7mb from the "file.in".

    Here's my code:
    Code:
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    
    int main() 
    { 
        char buffer[1024]; 
        int in, out, outread; 
        int temp = 0;
        in = open("file.in", O_RDONLY);
        out = open("file.out", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
        while((outread = read(in, buffer, sizeof(buffer))) > 0)
        {
            if(temp == 15728640) return 0; // returns when size is 15mb
            else write(out, buffer, outread);
            temp+=outread;
        }
    
        close[in]; 
        close[out]; 
    
        return 0; 
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is obviously wrong:
    Code:
    close[in];
    close[out];
    It should have been:
    Code:
    close(in);
    close(out);
    Instead of using a magic number, perhaps it would be better to:
    Code:
    #define SIZE_REQUIRED (15 * 1024 * 1024) /* 15 MB */
    temp is a common abbreviation for "temporary", but here that's not the most interesting thing about it, so I would rename it to total_bytes_read, or something like that. Your loop could then be changed to:
    Code:
    while(total_bytes_read < SIZE_REQUIRED && (outread = read(in, buffer, sizeof(buffer))) > 0)
    {
        write(out, buffer, outread);
        total_bytes_read += outread;
    }
    As for efficiency: it seems pretty reasonable. Maybe having the buffer size be BUFSIZ would help (if 15728640 is perfectly divisible by BUFSIZ), or maybe not.

    It would also be a good idea to check the return value of open.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Sorry for this
    Code:
    close[in];
    close[out];
    wasn't paying attention when I typed that out.

    I've also changed my codes accordingly based on your input, thanks again!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > wasn't paying attention when I typed that out.
    So why didn't you post some code you actually tried, instead of just winging it?
    Otherwise, we just waste time pointing out the bleedingly obvious stupid mistakes.

    The constant BUFSIZ in stdio.h is as good a choice as any for the size of block file access.

    Disk seek times are millions of times slower than DRAM access times, so worrying about whether 1K or 2K buffers will make a difference isn't worth the effort.

    Also, filling a large files with nothing but zeros may be detected by the OS (or filesystem), and you end up with a sparse file. This might be superficially a lot quicker, since it involves a lot less writing to disk.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Generating dll file
    By Elnaz in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2011, 04:55 PM
  3. Generating data file names from program variables
    By a380x in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 12:12 PM
  4. Replies: 3
    Last Post: 08-13-2006, 05:58 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM

Tags for this Thread