Need another pair of eyes to figure this one out

From: Andrew Falanga (falandr_at_hp.com)
Date: 07/26/04


Date: Mon, 26 Jul 2004 14:58:53 -0600

This isn't an earth shattering problem, but it is annoying. I'm writing
some class libraries and in doing so am using some driver programs to
test the libraries and see if I'm doing it right.

At one point, I'm opening a file descriptor and also opening a file that
I want to write (in binary) to this descriptor. So that I can watch the
progress of the write, I have embedded in the loop code to show how much
is written and how much is left. Obviously, when completed the final
write to stdout should be the files total size sent with 0 bytes
remaining. However, that's not what I'm getting. So without further
ado, here (in skeleton form) is what I have:

#include <iostream>
#include <fstream>
// lot's more headers because the fd I'm writing to a socket

int main() {
   char m[1024]; // in my code this 1024 is #defined
   int sock = 0;

   // because this is a driver ap, I'm not checking these returns
   std::ifstream fin("input.file", std::ios::binary);

   // here I would assign a descriptor to sock

   int bSent = 0, bRemain = 0;
   int read; // for bytes read during last file op

   fin.seekg(0, std::ios::end);
   bRemain = fin.tellg();
   fin.seekg(0, std::ios::beg);
   std::cout << "File size is: " << bRemain << std::endl;

   while( !fin.eof() ) {
     fin.read(m, 1024);
     read = fin.gcount();
     // placing data on socket
     bSent += read, bRemain -= read;
     std::cout << "bytes sent: " << bSent
               << "bytes left: " << bRemain << "\r";
     std::cout.flush();
   }
   // close stuff here
   return 0;
}

Now, when this is done one would expect to see:

File size is : 30933149

Bytes sent: 30933149 Bytes remaining: 0

However, what I'm getting is:

File size is: 30933149

Bytes sent: 30933149 Bytes remaining: 05717951

What's wrong with my math up there? I'm to the point where I need
someone else to help spot my simple error, I'm really sure it is simple.

---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled. I do not wish my account at HP to
become a spam haven.



Relevant Pages

  • Re: [PATCH] pipe: dont block after data has been written
    ... a file descriptor which is "ready for writing" must not block. ... if you pass a very large buffer to write, ... please correct documentation? ... Every Unix I know behaves the same when writing to a pipe. ...
    (Linux-Kernel)
  • Re: How to tell when execv has succeeded
    ... writing from experience, and it does, but the POSIX spec. ... says "Close the file descriptor upon execution of an exec family function, ... if this error is returned, the state of fildes is unspecified. ...
    (comp.unix.programmer)
  • Re: How to tell when execv has succeeded
    ... writing from experience, and it does, but the POSIX spec. ... says "Close the file descriptor upon execution of an exec family function, ... if this error is returned, the state of fildes is unspecified. ...
    (comp.unix.programmer)
  • Re: Another conformance question... This time fputs().
    ... The file descriptor underlying stream is not a valid file descriptor ... If we open the file for writing, ... A request was made of a non-existent device, ...
    (freebsd-arch)
  • Re: Need another pair of eyes to figure this one out
    ... > some class libraries and in doing so am using some driver programs to ... I'm opening a file descriptor and also opening a file that ... newline in your output statement? ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (alt.comp.lang.learn.c-cpp)