Thread: Filesize greater than contents of file?

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Question Filesize greater than contents of file?

    Hey guys, I'm working on a file transfer program right now, and I've figured out a way to get the size of a file though it may not be the best way. The thing is, I get a different size from what Windows says in explorer, and when I use CreateFile() then GetFileSize() on the file, it also gives the size Windows gives.

    In my version:
    -open a file in read mode
    -keep reading 1 character at a time, incrementing a counter
    -if eof(), quit the loop (before incrementing the counter)

    This consistently gives me a smaller filesize than Windows, and I can't figure out why. Also, I'm guessing that if I used the Windows filesize (send it over to the server so the server knows when the file is done receiving), the server would have to wait a long time for the client to "finish" sending the file.

    My question is, why does Windows make the filesize bigger? It says "size: (...) bytes, (...) bytes used" with the bytes used bigger, but even the actual size is too big according to what I can see. Why is this?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read my reply to this one . Does it help?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you're reading a text file (or any file in text mode), you're going to get a smaller size because you'll only get one character for each newline instead of two (a newline is actually a CRLF pair on Windows machines, as it was in DOS).

    Make sure you're opening the file in binary mode; make sure you're not using the >> operator but instead using read(). The size will be exact if done correctly.

    Windows doesn't make the size bigger, your method of reading the file is not yielding the right results.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Make sure you're opening the file in binary mode
    *smack* I was reading all about using code tags and why using feof() is bad, and I couldn't quite relate it to my problem

    from Hammer:
    Does it help?
    Not until I got Cat's post

    but instead using read().
    Yes, I'm using read(), but I thought that read() would automatically read in binary even if the thing was in text mode... come to think about it, that wasn't such a bright idea

    you're going to get a smaller size because you'll only get one character for each newline instead of two
    Wow, I never thought of that. I figured what I was doing was working great, because all my program does right now is send a textfile to the server, where the server saves it (in text mode lol).

    Alright, thanks for all the replies everyone!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM