Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: C programming files

  1. #1
    Join Date
    Nov 2012
    Beans
    43

    C programming files

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

  2. #2
    Join Date
    Nov 2011
    Beans
    9

    Re: C programming files

    Ok, I'm not sure exactly what you're trying to do here. Copy a file one part at a time, or split a file into parts. There were a number of things wrong with your version either way.

    Here's a version which will copy a file one part at a time. The new file should be identical to the original.

    I haven't test this extensively, but it seems to work.

    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";
    
        int parts = 3;
        int part_size;
        int part_number = 0;
        int bytes_read = 0;
    
        // 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);
    
        
        // Open ouput file
    
        result = open(new_file_name, O_CREAT | O_TRUNC | O_WRONLY );
        if ( result == -1 )
        {
          printf("Error opening %s\n", new_file_name);
          exit(EXIT_FAILURE);
    
        }
        new_descriptor = result;
    
        do 
        {
            // 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);
            if (result == -1) 
            {
               printf("Error writing %d bytes to %s.\n", bytes_read, new_file_name);
               exit(EXIT_FAILURE);
            }
    
        } while (result == part_size);
    
    
      // I'm lazy
      // At this point, close files and free buffer.
    
      return 0;  
          
    }

  3. #3
    Join Date
    Nov 2012
    Beans
    43

    Re: C programming files

    Sorry, for not explaining enough. Eventually I want to split a file into multiple parts. This was just meant as an attempt to see if I could get the first part of a file read and copied to a new file which would correspond to part1 of a series of files.

  4. #4
    Join Date
    Nov 2011
    Beans
    9

    Re: C programming files

    The example I made can be changed to do that. Each time through the loop you would have to generate a new file name, open the new file, write the data, and close the file.

    Hope my example helps.

  5. #5
    Join Date
    Nov 2012
    Beans
    43

    Re: C programming files

    How can I give each file a different second extension? So each time I run the loop I want each new file to be called, if it the original file is mypic.jpg and there are 3 parts, mypic.jpg.part0, mypic.jpg.part1, and mypic.jpg.part2.

  6. #6
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: C programming files

    I'm still rather confused by what you are trying to do. In your original post, you say that partSize is the number of bytes to write in each part. Then you give an example of partSize=2, but the part that is written is not 2 bytes. It seems more like in that example you are wanting to write 2 parts, rather than N parts each of size 2.

    In any event, do you really want to write a C program for this? The split command (with possibly a small bash wrappper) would seem ideal for whatever it is you are wanting to do.

  7. #7
    Join Date
    Nov 2012
    Beans
    43

    Re: C programming files

    All I want to do is take a file and split into a bunch of parts, which is each a seperate file. So If I have 400KB file and I say the number of parts is 2 then each will have a size if 200KB. Yes, I am sure I want to use C.

  8. #8
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: C programming files

    I think I'd lead toward scripting for this too, but if you want to pursue it in C, you need something like snprintf to generate the part names, e.g.:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static const unsigned MAXSIZE = 32;
    
    int main(int argc, char **argv)
    {
        char filename[] = "myfilename.jpg";
        unsigned parts = 5;
    
        char partname[MAXSIZE];         
        int bytes_written;
        unsigned p;
        
        for (p = 0; p < parts; p++) {
            bytes_written = snprintf(partname, MAXSIZE, "%s.part%d", filename, 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
        }
        exit(EXIT_SUCCESS);
    }
    I think you've reached the stage where you need to split your program into functions, especially if you wrap it in something like the above.

  9. #9
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: C programming files

    Quote Originally Posted by binaryperson View Post
    Yes, I am sure I want to use C.
    Ah... so this is school work. The utility 'split' (previously mentioned) is written in C. Why don't you download the source code and slap your name on it? Just kidding.

  10. #10
    Join Date
    Nov 2012
    Beans
    43

    Re: C programming files

    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;  
          
    }
    Last edited by binaryperson; January 3rd, 2013 at 02:22 PM.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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