Thanks, the file naming method you showed me works. I'll divide up the program into functions once I get it working properly. Can someone please test the code? It seems to work, I divided a picture into 2 equal files. But, for some reason it say writing part 2(0 bytes) and writing part 3 (0 bytes). If I ask for 2 parts it should just be part 0 and part 1, why are there 4 parts? Also if I try to free the buffer I a segmentation fault.
EDIT: if I use free() the program produces 2 equal sized files but I also get a segmentation fault. If I comment it out the files produced are 0 bytes each but there is no segmentation fault.
EDIT: it should be free(buffer), but still same problem.
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 original_descriptor;
int new_descriptor;
int original_file_size;
int result;
// char original_file_name[] = "original.txt";
// char new_file_name[] = "new.txt";
char original_file_name[] = "picture.jpg";
// char new_file_name[] = "znewpic.jpg";
int parts = 2;
int part_size;
int part_number = 0;
int bytes_read = 0;
static const unsigned MAXSIZE = 32;
char partname[MAXSIZE];
int bytes_written;
unsigned p;
// Open the file. If the result is -1 there was an error. Any
// positive result will be the descriptor for the input file.
result = open(original_file_name, O_RDONLY );
if ( result == -1 )
{
printf("Error opening %s.", original_file_name);
exit(EXIT_FAILURE);
}
original_descriptor = result;
// Get the file size.
struct stat buf;
result = fstat(original_descriptor, &buf);
if (result)
{
fprintf(stderr, "error: fstat: %s\n", "error");
exit(1);
}
original_file_size = buf.st_size;
// Get the part size
printf("The file is %0.2f Kilobytes large.\n", (double) (original_file_size/1024));
part_size = (original_file_size + 1)/parts;
if (part_size < 1)
{
part_size = 1;
}
// Create a buffer large enough to handle partsize bytes.
char *buffer = malloc(part_size);
//int partnumber=0;
char new_file_name[15]= "znewpic.jpg";
do
{
for (p = 0; p < parts; p++) {
bytes_written = snprintf(partname, MAXSIZE, "%s.part%d",new_file_name , p);
if (bytes_written >= MAXSIZE) {
fprintf(stderr, "ERROR: Max length of part name is %d\n", MAXSIZE);
exit(EXIT_FAILURE);
}
// puts(partname); // The rest of your code in here
umask(0000);
// result = open(new_file_name, O_CREAT | O_TRUNC | O_WRONLY,0777 );
result = open(partname, O_CREAT | O_TRUNC | O_WRONLY,0777 );
if ( result == -1 )
{
printf("Error opening %s\n", partname);
exit(EXIT_FAILURE);
}
new_descriptor = result;
// Read data into buffer
result = read(original_descriptor, buffer, part_size);
if (result == -1)
{
printf("Error reading from %s.\n", original_file_name);
exit(EXIT_FAILURE);
}
bytes_read = result;
printf("Writing part %d (%d bytes).\n", part_number, bytes_read);
++part_number;
// result = write(new_descriptor, buffer, bytes_read);
result = write(new_descriptor, buffer, bytes_read);
if (result == -1)
{
printf("Error writing %d bytes to %s.\n", bytes_read, new_file_name);
exit(EXIT_FAILURE);
}
close(new_descriptor);
}
free(part_size);
} while (result == part_size);
// I'm lazy
// At this point, close files and free buffer.
return 0;
}
Bookmarks