Thread: Binary files

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why didn't you answer my question?
    Because there's no way to tell whether that's the real question, because you didn't post the real code.

    Paraphrased code is absolutely NO use for debugging since there is NO way to tell whether you've missed some critical detail.

    Hence it is simply a waste of both your time and mine to even look at code which you've pasted from memory and neither compiled or run.
    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.

  2. #17
    Registered User
    Join Date
    May 2005
    Posts
    207
    There is nothing paraphrased about it. That's a program I'm writing specifically to experiment with binary files. And as for "neither compiled nor run", if you had actually read my post you would've noticed that I *had* run it because I posted the results.

    And you still haven't answered my question. Is there any reason why you're posting here?

    Quzah:

    1) I don't know the standard. I thought Windows would have some kind of error when you attempted to open the file twice.

    2) I expected my 2nd fopen to append but it didn't. I was just wondering why it didn't.

    You guys are seriously uptight...

    mw
    Last edited by Lionmane; 08-23-2005 at 10:46 AM.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Lionmane
    Quzah:

    1) I don't know the standard. I thought Windows would have some kind of error when you attempted to open the file twice.

    2) I expected my 2nd fopen to append but it didn't. I was just wondering why it didn't.

    You guys are seriously uptight...

    mw
    You're telling someone who has been here for years longer than you (just using the latest time stamp) to justify their reasons for posting, and demanding they answer you, and we're uptight?

    1) I guess you've never opened two copies of "notepad" and opened the same file in each copy have you?

    2) Using the above, if you make different changes in each one, what would you expect to happen? Well, since "notepad" loads the whole file into memory, it's going to write the whole thing over again with whatever one you "file -> save" last.

    The OS can allow or disallow you opening files however it feels like. Some don't let you open it more than once, and will crash. Some don't care. In either case, this has nothing to do with C. The language itself doesn't care what operating system it runs on. However, you need to actually think about what you're doing.

    When you open a file for writing, and then open it again for the same action, what do you expect to happen when you start writing on one open instance and then writing on another? How can this be a good idea ever? Are you sure your output gets flushed? Does it write the changes for one before the other? All of this you need to think of. That's the whole point. We can tell you the answer, but at some point in time, you actually need to start thinking about what it is you're doing.

    While Salem and I have been here for years (probably seven or so at least), we're not going to be here forever, so at some point in time, you're going to have to start thinking for yourself. The sooner the better.


    Quzah.
    Last edited by quzah; 08-23-2005 at 11:19 AM.
    Hope is the first step on the road to disappointment.

  4. #19
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You stated in your post
    (If there are any syntax errors, I apologize. I'm on a seperate computer and had to retype it here)
    Which means that if we saw a syntax error, typo, etc we have no way of knowing if its in the real code or not. Thus its a waste of our time to look at it.

    Let me caution you again about getting in fights with our senior members.

    And calling Salem inept is like calling the sun cold.

  5. #20
    Registered User
    Join Date
    May 2005
    Posts
    207
    Thanks, Quzah. And don't worry; I'm a good programmer. I'm still learning how the computer works though...

    Thantos:
    I tried to type the code as close as I could to the original. If I didn't get it perfect, I've already apologized for it. I would've been better off not even mentioning it considering the complete lack of etiquette in the response.

    And if it's such a waste of your time might I respectfully ask why you post in this thread? This is not a waste of time to me. If you consider that fighting, then just forget I asked.

    mw
    Last edited by Lionmane; 08-23-2005 at 01:28 PM.

  6. #21
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, I figured it out! Any changes you make to a file do not take place until the file is closed!

    Heck, just being able to share data between text and binary files is cool to me! QBasic would NEVER let you do that! :-)

    So I guess theoretically you could tax the memory by doing too many file changes before closing it. Hmm... I guess I'll try that next.

    This is really giving me some good ideas on what controls I need to add to prevent my program from screwing up! :-)

    mw

  7. #22
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Any changes you make to a file do not take place until the file is closed!
    Not quite.

    The stdio functions have to call the low-level read() and write() to actually read data from the disk or write data to the disk. The stdio library has an internal buffer of a limited size. The syscalls read() and write() are slow relative to the stdio functions fread(), fwrite(), fgets(), fputs(), etc. So when you use one of those stdio functions to write to a file it just adds it to the buffer. When the buffer fills up it write()s it to disk and the process continues. This minimizes the number of write() calls the program needs to make.

    So if you try writing a huge amount of data using the stdio functions you won't tax the memory. You'll just keep filling the buffer and the write() will keep getting called.
    If you understand what you're doing, you're not learning anything.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you just call fflush...


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    May 2005
    Posts
    207
    Yeah, I figured I had missed something when I made an infinite loop that wrote a single character to the binary text file.

    It created a 3.5 GB text file in a matter of seconds! :-)

    mw

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Last edited by Lionmane : 08-22-2005 at 07:14 AM.
    Last edited by Lionmane : Yesterday at 07:38 AM.
    Last edited by Lionmane : Yesterday at 05:46 PM.
    Last edited by Lionmane : Yesterday at 08:28 PM.
    Perhaps you should focus on saying what you mean first time around rather than posting some crap only to draw flames and then "retracting" it later on.

    > And don't worry; I'm a good programmer.
    I thought Quzah had the funniest response in this thread until I read this!!!

    > I tried to type the code as close as I could to the original.
    Doesn't matter - the quality of the answer is in direct response to the quality of the question. If you miss detail in your post, then don't go mouthing off at everyone trying to help you when the quality of the answers isn't what you wanted.
    Remember, Garbage In Garbage Out - you got what you deserved.
    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.

  11. #26
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you forgot about the GarbageTOGold() function

    found in <BS.h>

  12. #27
    Registered User
    Join Date
    May 2005
    Posts
    207

    File I/O

    Quote Originally Posted by itsme86
    Not quite.

    The stdio functions have to call the low-level read() and write() to actually read data from the disk or write data to the disk. The stdio library has an internal buffer of a limited size. The syscalls read() and write() are slow relative to the stdio functions fread(), fwrite(), fgets(), fputs(), etc. So when you use one of those stdio functions to write to a file it just adds it to the buffer. When the buffer fills up it write()s it to disk and the process continues. This minimizes the number of write() calls the program needs to make.

    So if you try writing a huge amount of data using the stdio functions you won't tax the memory. You'll just keep filling the buffer and the write() will keep getting called.
    I was wanting to calculate prime numbers dynamically to the limits of "int" and was considering the use of a binary file for my algorythm instead of a dynamic pointer array.

    At this point, I'm just learning how to use binary files. Prelude's FAQ suggests they're not as efficient as text files though, so I'll have to consider that too.

    Do you think the buffer would interfere with my algorythm? I can't imagine trying to use my algorythm in blocks! (augh!) I'm sure I could do it though...

    mw
    Last edited by Lionmane; 08-24-2005 at 02:42 AM.

  13. #28
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Can you outline your preferred algorithm ? Because I cannot think of anything that would be more efficient using a file instead of any memory construct. Are there are memory constraints on your machine that you have to use a file ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  14. #29
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    Quote Originally Posted by Salem
    > And don't worry; I'm a good programmer.
    I thought Quzah had the funniest response in this thread until I read this!!!
    good point salem, although i personally think his next sentance was ... interesting ...

    Quote Originally Posted by Lionmane
    I'm a good programmer. I'm still learning how the computer works though...
    may i suggest to study the basic first?
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  15. #30
    Registered User
    Join Date
    May 2005
    Posts
    207
    I think I have 128 megs RAM.

    Code:
    Eratosthenes Sieve
    
    count = 3;
    
    do
    {
        for (discard = count * count; discard <= max; discard = discard + count * 2)
        {
            if (prime[discard] > 0)
            {
                prime[discard] = 0;
            }
        }
            count++;
    } while (count * count <= max);
    The computer asks the max range to calculate prime numbers. It then sets up a pointer array with (max + 1) elements. A prime number is assigned a 1 value and a composite number (ie: nonprime) is assigned a zero value in the pointer.

    I had heard about the Atkins Sieve, but I couldn't grasp it.

    mw
    Last edited by Lionmane; 08-24-2005 at 08:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 01:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM