Thread: File Get Corrupted while doing anything

  1. #1
    Registered User
    Join Date
    Apr 2009

    File Get Corrupted while doing anything

    Hey Guys,,

    I did this coding .. but when ever i add data it doesn't save the data, and when i add second time the data it does.

    Even when i try to delete any thing the file gets corrupted.

    Sorry i cant copy the whole code here. It a big file.

    So i have attached my CPP file.. PLease help me ..

    Thanks in Advance,
    Anshu Verma

  2. #2
    The larch
    Join Date
    May 2006
    Your use of C-style strings leaves you open for buffer overruns.

    For other file manipulations it would probably be simpler to load everything into memory, delete from the list in memory and write everything back when the program finishes.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area

    gets - Use fgets instead... or better yet, stop mixing C/C++ I/O and just use getline in place of the gets calls.

    You are recursively calling main in your opt functions. Don't do that.

    What compiler are you using... ancient/deprecated/non-standard headers (and functions) abound?

    Your class design doesn't feel right to me. A class's member functions should not themselves have variables of that class present. I would take advantage of polymorphism to reduce duplicated code and have a templated class design, call it "crime" or something. This crime class can have member functions to "read" the relevant file on program startup and stores the data into a vector/list container that it manages. The destructor's can implement a "write" to the file in question. This will keep the data all in memory and not rely on repeated reading/writing of date to/from the files. Each crime class holds information for a particular crime and holds data for those crimes "murder", "theft" in the above mentioned vector/list and can support the add/delete/modify/search functionality as well. This separates the add/search/delete/modify operations from the individual murder/theft class's themselves which should really only store data for those crimes (along with some basic functionality).

    As for other issues, mixing gets (or even getline) with cin can result in the user typing in something and having it processed by the cin which leaves the newline in the buffer. This trailing newline can then be picked up and processed by the next gets/getline call which results in an input operation being "skipped". Every such operation needs to make sure the input stream is cleared prior to any gets/getline call (and don't think about using fflush(stdin), the faq talks about alternatives).

    You should be using std::string containers instead of char arrays.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. archive format
    By Nor in forum A Brief History of
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM