Like Tree1Likes
  • 1 Post By Matticus

C read and write specific amount of bytes from file and to file

This is a discussion on C read and write specific amount of bytes from file and to file within the C Programming forums, part of the General Programming Boards category; Hello. I want my program to print "partSize" amount of bytes of the original file to the new file. If ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    10

    C read and write specific amount of bytes from file and to file

    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
    Registered User
    Join Date
    Jun 2011
    Posts
    2,242
    Inter-forum cross-post. Follow link for previously issued sass.
    Salem likes this.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    *thread closed*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  2. Function to write two bytes at a time to a file?
    By Flotonic in forum C Programming
    Replies: 11
    Last Post: 05-28-2011, 03:58 PM
  3. module that write in a specific /proc file
    By hosonno in forum Linux Programming
    Replies: 2
    Last Post: 03-31-2010, 11:48 AM
  4. Read large amount of numbers from file
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2007, 09:33 AM
  5. read 2 bytes from a file...
    By compile in forum C Programming
    Replies: 10
    Last Post: 10-17-2006, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21