HEY C Board! How is everyone? Binaryperson here wishing everyone a Happy new year!
I want my program to print "partSize" amount of bytes of the original file to the new file.
If the original file contains:
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
and part size is 2 then
0123456789
0123456789
0123456789
is stored to the new file.
So the program is not writing until end of line, that would be the whole file. It is writing partSize amount of bytes.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int createDescriptor;
int openDescriptorOriginal;
int closeCreateDescriptor;
int openDescriptorNew;
//char fileNameOriginal[] = "picture.jpg";
char fileNameOriginal[] = "original.txt";
char fileNameNew[] = "new.txt";
int parts;
int partSize;
parts=2;
int bytesRemaining;
int partNumber;
char BUFFER[512];
int readDescriptor;
int openDescriptorNew;
if ((openDescriptorOriginal = open(fileNameOriginal, O_RDONLY )) == -1)
{
printf("Error opening %s", fileNameOriginal);
exit(EXIT_FAILURE);
}
struct stat buf;
int r = fstat(openDescriptorOriginal, &buf);
if (r)
{
fprintf(stderr, "error: fstat: %s\n", (char *) strerror(errno));
exit(1);
}
int originalFileSize = buf.st_size;
printf("The file is %.9f Kilobytes large.\n", (double) originalFileSize/1024);
partSize = ((originalFileSize + parts) + 1)/parts;
if ((openDescriptorNew = open(fileNameNew, O_CREAT | O_WRONLY )) == -1)
{
printf("Error opening %s", openDescriptorNew);
exit(EXIT_FAILURE);
}
//I am stuck at this part
//I want to write the first "partSize" bytes of "fileNameOriginal" to "fileNameNew"
/*
while ((readDescriptor = read(openDescriptorOriginal, BUFFER, partSize)) < partSize))
{
write(1, BUFFER, readDescriptor);
}
*/
if ((closeCreateDescriptor = close(openDescriptorOriginal)) == -1)
{
printf("Error closing file.\n");
}
return 0;
}