User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,075 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,068 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3660 | Replies: 5
Reply
Join Date: Sep 2006
Posts: 7
Reputation: im4tion is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
im4tion im4tion is offline Offline
Newbie Poster

Reading specific line in a file. / Searching

  #1  
Sep 16th, 2006
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Rep Power: 4
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Reading specific line in a file. / Searching

  #2  
Sep 16th, 2006
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++
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote  
Join Date: Sep 2006
Posts: 7
Reputation: im4tion is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
im4tion im4tion is offline Offline
Newbie Poster

Re: Reading specific line in a file. / Searching

  #3  
Sep 16th, 2006
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..
Reply With Quote  
Join Date: Dec 2005
Posts: 3,267
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Reading specific line in a file. / Searching

  #4  
Sep 16th, 2006
Well if you've already created functions to read a whole record and write a whole record, then perhaps something like
int matchID ( record *rec, char *ID ) {
  return strcmp( rec->ID, ID );
}

Based on the result, you can decide whether (or not) to output the record to the screen, another file or whatever.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,668
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Reading specific line in a file. / Searching

  #5  
Sep 16th, 2006
Originally Posted by im4tion View 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?


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.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,775
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Reading specific line in a file. / Searching

  #6  
Sep 17th, 2006
Originally Posted by iamthwee View Post
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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC