Thread: Reading from a file quickly

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Reading from a file quickly

    I'm trying improve my C++ connect four game by having it "learn" from its mistakes. To do this I'm gonna have it write to a file certain positions and how it handled them wrong so it doesn't make the same mistake twice. Since after awhile the number of positions will be increasing, I want the program to be able to read from the file quickly or else all of its calculations will get bogged down. Anyone know if there is a way to decrease file reading times, like possibly writing and reading in binary?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Read the file at the beginning of the program into a data structure, then you can read from that structure very quickly. The only slowdown would be at the beginning, before the game starts.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Use a database. You can add your records into the database and then query it (which is very fast) for your previous records. The nice thing is that you can get get recordsets that match similar games so you don't have to load the entire file into memory.

    You can do a search for a posting by my with a similar title and find a link to how to do this using CDaoDatabase and CDaoRecordset.
    Best Regards,

    Bonkey

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380


    My friend did this for a chess program once! Like Prelude said; Read the file into some data structure at the begining of the program (onto the heap), where it can quickly be accessed during execution

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks all! Truth is I got about halfway through writing that post when the exact same thought occurred to me and I went, duh! But I posted anyways because I was curious of the answer! Just out of curiosity, IS there anyway to speed up file reading and writing to a text file?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just out of curiosity, IS there anyway to speed up file reading and writing to a text file?
    Not really, reading and writing to a file is a slow operation. It's like memory allocation, it's slow, so instead of making several requests, you get a lot of memory with one call to improve performance. When using files its faster to read everything all at once instead of bit by bit. Beat the speed disadvantage through good design.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM