| | |
Reading specific line in a file. / Searching
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 7
Reputation:
Solved Threads: 0
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?
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?
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++
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.
Well if you've already created functions to read a whole record and write a whole record, then perhaps something like
Based on the result, you can decide whether (or not) to output the record to the screen, another file or whatever.
C++ Syntax (Toggle Plain Text)
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.
•
•
•
•
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?
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)
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.
*Voted best profile in the world*
•
•
•
•
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.
just my point of view, bye.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Reading in a random line from a file (C++)
- Reading pascal file and searching for a particular word in that file (Pascal and Delphi)
- Reading from a file question (Python)
Other Threads in the C++ Forum
- Previous Thread: Help with the execution of this program
- Next Thread: Algorithm doubt
Views: 7607 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






