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;
}