Hi. I was wondering if anybody could help me with a small problem.
I would like to read a specific line from a txtfile and either output/delete it.
Basically, I'm doing a program where a user inputs/view records and i'm stuck at searching it.
My txtfile input from user has a format like this:
ID:
Name:
Contact:
etc...
and it repeats.
So i would like to search by ID and output the id,name,contact...etc.
Anyone could give me a hand here?
You neglected to mention what variant of the c language you intend to use, c or c++?
In any case here is how I would do it . Create a class or structure and separate it into three attributes. ID, name and contact.
Read the file three lines at a time, feeding each line into the class/structure. I.e.
line one => ID: 398439
line two => name: iamthwee
line three=>contact: 00349303
You can use the ':' (colon) as a delimiter to split each line into two parts. The right-hand part is the most important bit and you will feed this into the class/structure variables.
Then, once you have an array of class/structure variables, traverse through the array, matching either the ID/name/contact details.
Use the 'string.find' API for c++ or make your own find function using 'strcmp' if you intend to use 'c'.
In regards to the how do 'I delete a line from the file problem',you should know that
deleting doesn't actually exist.
Rather, you give the illusion of deletion by:-
1. copying the contents of the original file into memory,
2. change the a variable of that file whilst in memory,
3. erase the original file,
4. then write a new file with the ammendments and then rename the new file with the name of the original one.