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

  1. #16
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by laserlight View Post
    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.
    Maybe these this forum isn't for beginners?
    total_bytes_read and bytes_read I need to declare as integers and my program should work?
    what do I think of it? Your solution seems simple and I like keeping it simple. Let me just try it out to see if it works.
    The solution provided on stack-overflow is extremely confusing to me and doesn't work.
    Last edited by Binaryperson; 01-03-2013 at 02:50 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Binaryperson
    Maybe these this forum isn't for beginners?
    What makes you think so?

    Quote Originally Posted by Binaryperson
    total_bytes_read and bytes_read I need to declare as integers and my program should work?
    The point is to understand what is going on and then implement it youself with my example code snippet as a reference.

    EDIT:
    Quote Originally Posted by Binaryperson
    Your solution seems simple and I like keeping it simple. Let me just try it out to see if it works.
    The solution provided on stack-overflow is extremely confusing to me and doesn't work.
    Ah, but my solution does not yet consider edge cases: what happens if the number of bytes read will cause the total to exceed the partSize when written? You will need to reduce the number of bytes written in that case so that it will fit the partSize nicely.

    Another thing: you keep talking about partSize, but in your introduction to the problem, what you call "partSize" really should be "numParts". Hence, with numParts == 2, the partSize will be 1/2 * number of bytes in the file.
    Last edited by laserlight; 01-03-2013 at 02:58 AM.
    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

  3. #18
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    partSize is the size of each part. parts is the number of parts. partSize is determined by dividing the the file size by the number of parts. Then, the first partSize amount of bytes are read from the original file and written to a new file. Well thanks for explaining the concept but it is alright, I found another way to solve the problem.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Binaryperson
    partSize is the size of each part. parts is the number of parts. partSize is determined by dividing the the file size by the number of parts.
    Yes, that is what you mean. The thing is, in post #1, you wrote:
    Quote Originally Posted by Binaryperson
    and part size is 2 then
    What you actually meant there was "parts is 2".

    Quote Originally Posted by Binaryperson
    Well thanks for explaining the concept but it is alright, I found another way to solve the problem.
    What way might that be?
    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

  5. #20
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Well sorry for the misunderstanding.

    Quote Originally Posted by laserlight View Post
    What way might that be?
    It involves using the malloc() function. You just malloc() partSize amount of bytes and then read and write the allocated amount of bytes.

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