Thread: Urgent, help needed.

  1. #1
    Registered User
    Join Date
    Mar 2010

    Urgent, help needed.

    I was given an assignment to complete. I'm doing a library program which including borrow and return program. And here, i seek the advice on how to delete a line in a text file. My program work this way, when a student borrow a book, the studentid and the book code will be written in a file call relation.txt which contain 2 column ( studentid , bookcode), now what i want is the return program. When a student want to return a book, the program will ask for his studentid and the bookcode, and it will scan through the text file searching the number that match with the bookcode that being input by the student, after found it, it will delete that line in the text indicating the student already return the book. Do anyone has any idea how to write this program?

  2. #2
    Registered User
    Join Date
    Sep 2006
    One way (more suitable for text format files), is to read in all the records that don't match the one you're looking for, and then write them out to a temporary file.

    When you find the record you're looking for, just don't write that one out.

    Continue writing out any remaining records in the text file, and close both files. Now delete the old file. Then rename the temporary file, with the original records file name.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Would it be easier to write a new file as Adak said or to alter the old file. ( in my mind, this is the way i thought of before, read the file, then strcmp the bookcode input by the student with the bookcode existed in the file and then delete the whole line.) Which program would be easier and less complicated?

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    There is no way to "delete" a line. The only way would be to move all the data below the line you want gone up, so it's a tad complex.
    Adak's way is slower, but certainly more programmer friendly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Welcome to the boards, in the future please take note of the forum guidelines, in particular this:
    Tips for Efficient and Successful Posting

    1. Don't use all caps.

    2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    "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. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 12:46 PM
  2. boolean algebra....urgent help needed!
    By Saimadhav in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2008, 07:22 AM
  3. Urgent help needed in Socket Programming
    By lisa23 in forum C Programming
    Replies: 2
    Last Post: 09-15-2005, 11:18 PM
  4. Help Needed: Borland C++ 5.5 Installation - URGENT!
    By Linette in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2002, 06:44 PM

Tags for this Thread