944,148 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12892
  • C++ RSS
Sep 16th, 2006
1

Reading specific line in a file. / Searching

Expand Post »
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?
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
im4tion is offline Offline
7 posts
since Sep 2006
Sep 16th, 2006
0

Re: Reading specific line in a file. / Searching

Anyone will be glad to give you hand if you show your efforts here.
If you don't really know how to work with files then I would suggest you to read you book first or go through basic tutorial like
File I/O in C
File I/O in C++
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 16th, 2006
0

Re: Reading specific line in a file. / Searching

Uhm I think you misunderstand me. I've done it; I'm just stuck on searching data from a file. I was hoping someone could give an example on it. I do not want it to be from my coding just an irrelevant example.
And I did read the two links you gave..
Reputation Points: 14
Solved Threads: 0
Newbie Poster
im4tion is offline Offline
7 posts
since Sep 2006
Sep 16th, 2006
1

Re: Reading specific line in a file. / Searching

Well if you've already created functions to read a whole record and write a whole record, then perhaps something like
C++ Syntax (Toggle Plain Text)
  1. int matchID ( record *rec, char *ID ) {
  2. return strcmp( rec->ID, ID );
  3. }

Based on the result, you can decide whether (or not) to output the record to the screen, another file or whatever.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 16th, 2006
1

Re: Reading specific line in a file. / Searching

Click to Expand / Collapse  Quote originally posted by im4tion ...
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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. line one => ID: 398439
  3. line two => name: iamthwee
  4. 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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 17th, 2006
1

Re: Reading specific line in a file. / Searching

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Wont this present problems if the file is really large like those real time log files or data files. It would be really better if you read a chunk or a logical structure of data from the original file and keep on writing it to a new file and skip those records which you want to as such delete as you say. After this operation if finished the original file can be overwritten with the new one or deleted.

just my point of view, bye.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 7th, 2011
0
Re: Reading specific line in a file. / Searching
If you want to read a particular line perhaps this could help. Suppose you want to read line no.131 then use the following code:
C++ Syntax (Toggle Plain Text)
  1. ifstream fin("file.dat");
  2. int lineno = 131;
  3. int count = 0;
  4. string str;
  5. while(count < 130 && getline(fin, str))
  6. count++;
  7. getline(fin, str);
Last edited by santakdalai90; Nov 7th, 2011 at 7:32 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santakdalai90 is offline Offline
4 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with array
Next Thread in C++ Forum Timeline: fatal error LNK1120 ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC