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

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

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

    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;
    }
    Last edited by Binaryperson; 01-02-2013 at 11:07 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A cross-post here.
    And either a cross-post or a copy/paste from here and/or here.

    Please at least personalize your first messages to this forum.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Wow, impressive detective work. What is your point? I've posted my question in three different places and no one has been able to give me any help. Why would I change the question each time I post it?

    As for double posting on this forum, that was an accident. I was having trouble posting and I somehow double posted and don't know how to delete the first post.

    How do you want me to personalize it?

    Is it against this forums rules to post a question that you have posted on another forum/forums?
    Last edited by Binaryperson; 01-02-2013 at 11:12 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Wow, impressive detective work. What is your point?
    My point is that it's bad etiquette to cross-post, as those who volunteer their time to help might be wasting their time if a solution is being given somewhere on another forum. Why should someone articulate an answer if it's already been given elsewhere?

    And that "detective work" wasn't for your benefit - it was to let the others in this forum know that you're copy/pasting problems without regard for our time.


    I've posted my question in three different places and no one has been able to give me any help.
    Again, it's considered good etiquette to indicate that you've posted elsewhere. It's also good to realize that it might take a little more time for someone to respond meaningfully. Most of your other links are very recent.

    Not intended to be rude, but if you haven't read this link, I'd suggested it:
    How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    FYI you should also indicate your compiler and any specific problems you are having. I can see by looking at the first 10-15 lines of this code that some crucial seems to be missing.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    In every iteration of the loop...

    How many bytes do you read?
    How many do you write?

    And yes, you need to read the rules and follow them. Otherwise you will just get ignored. Most people out here _can_ help you, but you have to make us _want_, so learn how to be kind here.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Posting to forums is a new concept to me. In the future, I will cite any other websites I may have posted a similar questions to. I am using gcc 4.4.3. Would you mind sharing what is missing in my first 10-15 lines of code?

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    I have spent two days trying to figure this out, so I am extremely frustrated.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by comocomocomo View Post
    In every iteration of the loop...

    How many bytes do you read?
    How many do you write?

    And yes, you need to read the rules and follow them. Otherwise you will just get ignored. Most people out here _can_ help you, but you have to make us _want_, so learn how to be kind here.
    I thought it didn't matter? Well 512 are being read and 512 written. The total number of bytes must not exceed partSize bytes.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by Binaryperson View Post
    I thought it didn't matter? Well 512 are being read and 512 written. The total number of bytes must not exceed partSize bytes.
    Sorry. That '1' confused me. So you are writing to descriptor 1 instead of the new file?

    I don't understand the logic of your loop. You want to copy 512 bytes, but you only write if you actually read _less_ than 512. And in this case, you go for another 512?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... I find your various "XYZDescriptor" variables to be rather confusing. If something is a file descriptor, then a descriptor suffix makes sense. Otherwise, it doesn't. It seems to me that you're working with two files, with corresponding descriptors openDescriptorOriginal and openDescriptorNew. As such, I would expect something like:
    Code:
    while (total_bytes_read < partSize && (bytes_read = read(openDescriptorOriginal, BUFFER, partSize)) > 0)
    {
        total_bytes_read += bytes_read;
        write(openDescriptorNew, BUFFER, bytes_read);
    }
    So the idea is that you keep track of the total bytes read, then keep reading until that value reaches the partSize. After each read, you write exactly the number of bytes just read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Thanks to Matticus, I found that this was pretty much answered elsewhere, so I lost interest....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by Salem View Post
    Thanks to Matticus, I found that this was pretty much answered elsewhere, so I lost interest....
    Yes, that is why I am stil struggling with it.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, so what do you make of my reply in post #11? It seems like a simple thing to me, but of course if you are looking for answers all over the place, the sum of the replies that you're getting will not be very coherent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps the struggle is because you have so many different threads all over the place, that you're lacking a consistent narrative that can explain what is going on.

    Pick ONE forum, post your latest effort, and put redirect links on the ends of ALL your other threads to let everyone else who might help know what it going on.

    If your chosen forum bombs out, then (and only then) move onto somewhere else. Blasting the net with your question just leaves you confused with disparate answers, and annoys the crap out of people who help when they discover the cross-posts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-02-2013, 11:03 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. 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
  4. module that write in a specific /proc file
    By hosonno in forum Linux Programming
    Replies: 2
    Last Post: 03-31-2010, 11:48 AM
  5. Read large amount of numbers from file
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2007, 09:33 AM