| | |
Newbie C++ Search and Parsing Text file.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
I am finally learning about fstream but Im stomped with this problem that I have. I have a current file that I am opening. Each line of said file looks like this:
D40001~10997~811~DANIWEB~555-555-5555~7.70~I~2111
There are around 5000 lines with different DXXXXX numbers. I want the user to input a dnumber (DXXXXX) and have it search the file for said Dnumber and then display the name that is associated with the Dnumber. So typing in 'D40001' should display the name Daniweb. Here is my code but I've went through 5 tutorials on the net and cannot find out how to search this file and do what I want it to do. From the searching that I did do, they said that I need to parse the file and then match the DXXXXX and display the screen. I'm clueless to how to do this. Here is the code that I have so far. Also, this is not for homework. I'm too poor for school. :]
D40001~10997~811~DANIWEB~555-555-5555~7.70~I~2111
There are around 5000 lines with different DXXXXX numbers. I want the user to input a dnumber (DXXXXX) and have it search the file for said Dnumber and then display the name that is associated with the Dnumber. So typing in 'D40001' should display the name Daniweb. Here is my code but I've went through 5 tutorials on the net and cannot find out how to search this file and do what I want it to do. From the searching that I did do, they said that I need to parse the file and then match the DXXXXX and display the screen. I'm clueless to how to do this. Here is the code that I have so far. Also, this is not for homework. I'm too poor for school. :]
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; void Display(); void SearchNumber(string); int main() { // Variable for the dealer number string dNumber; // ofstream constructor opens file ifstream dealers ( "dealers.tdl", std::ios::in); // exit program if unable to open file. if (!dealers) { cerr << "File could not be opened" << endl; exit(1); } Display(); cout << "D Number: "; cin >> dNumber; } // Puts the D Number request in the middle of the Dos Window void Display() { for (int i = 0; i < 11; i++) cout << endl; for (int j = 0; j < 33; j++) cout << " "; }
Right. This is what you should do.
1. Open the file ( You have already done that)
2. Read the opened file line by line into a string (lets call it
3. Create a istringstream object lets call it
4. You can again use
5. Compare the first word with the user input and if it matches, display the 4th word.
Try it and post your effort. We will help if you encounter further difficulties.
1. Open the file ( You have already done that)
2. Read the opened file line by line into a string (lets call it
line). You can use getline for this.3. Create a istringstream object lets call it
isstream, from the string line.4. You can again use
isstream and getline to get each word delimited by the '~' character. 5. Compare the first word with the user input and if it matches, display the 4th word.
Try it and post your effort. We will help if you encounter further difficulties.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Right. This is what you should do.
1. Open the file ( You have already done that)
2. Read the opened file line by line into a string (lets call itline). You can use getline for this.
3. Create a istringstream object lets call itisstream, from the stringline.
4. You can again useisstreamandgetlineto get each word delimited by the '~' character.
5. Compare the first word with the user input and if it matches, display the 4th word.
Try it and post your effort. We will help if you encounter further difficulties.
C++ Syntax (Toggle Plain Text)
while(!dealers.eof()) { getline(dealers,line, '~'); //get line and ignore/skip delimiter }
I see that when I do a cout << line; after getline, it displays one line at a time so I am guessing it doesnt store the whole file in the line string like I thought before. I am also guessing in that same while loop is where I need to do the line comparisons. I also added tthr '~' and it puts everything that was delimited on its seperate line for instance:
D40001
10997
811
DANIWEB
555-555-5555
7.70
I
2111
I am kind of stuck at:
3. Create a istringstream object lets call it isstream, from the string line.
I understand creating the object using:
istringstream isstream (line)?
I'm kind of confused now. sorry.
Last edited by chuck577; Oct 24th, 2006 at 6:33 am.
•
•
•
•
while(!dealers.eof())
{
getline(dealers,line, '~'); //get line and ignore/skip delimiter
}
C++ Syntax (Toggle Plain Text)
while(getline(dealers,line) { }
•
•
•
•
I see that when I do a cout << line; after getline, it displays one line at a time so I am guessing it doesnt store the whole file in the line string like I thought before. I am also guessing in that same while loop is where I need to do the line comparisons.
•
•
•
•
3. Create a istringstream object lets call it isstream, from the string line.
I understand creating the object using:
istringstream isstream (line)?
C++ Syntax (Toggle Plain Text)
string token; istringstream isstream (line); while ( getline( isstream ,token, '~') ) { cout << token << " "; } cout << "\n";
バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Use the following loop to read the file line by line.
That is better than checking for the eof bit of the file stream.C++ Syntax (Toggle Plain Text)
while(getline(dealers,line) { }
Yes. That is correct.
Yes. That is the way. Now try the following piece of code.
C++ Syntax (Toggle Plain Text)
string token; istringstream isstream (line); while ( getline( isstream ,token, '~') ) { cout << token << " "; } cout << "\n";
Wow,
That worked A LOT better than the way I had it. Now the delimiters are gone and each line is still displayed together. That worked flawlessly. Since I dont want the users to actually see the all of the lines of the files I will remove the cout << token << " "; and cout <<"\n and leave it with this
C++ Syntax (Toggle Plain Text)
while(getline(dealers,line)) { string token; istringstream isstream (line); while (getline(isstream,token,'~')) { // Place code to match the first // string in a line with the string // entered by the user. If it // doesnt match go to next line etc. // Output the name (4th position) in // reverse string order. } }
I understand what I need to do here but I am not sure of the syntax or even the functions that I would use. I already have the line so I should need to use getline again.
•
•
•
•
I already have the line so I should need to use getline again.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
Thanks so much for the help Wolf. I have 2 more problems. I kinda did a hack job with my code but it works for what I need to do. The problem I am having with the way I programmed it is that I want to put an output reading "Dealer Not Found" if the DXXXXX doesnt match. I tried putting this in the while(geline....) loop but it prints out each time it tries to find a match for every line.
Also, what would be the best way to loop this program after a DXXXXX number is entered? Only closing the program after the user says that they are done.
Here is my updated code.
Also, what would be the best way to loop this program after a DXXXXX number is entered? Only closing the program after the user says that they are done.
Here is my updated code.
void Display(); int main() { // Variable for the dealer number string dNumber; string line; // ofstream constructor opens file ifstream dealers ( "dealers.tdl", std::ios::in); // exit program if unable to open file. if (!dealers) { cerr << "File could not be opened" << endl; exit(1); } // Puts the text in the correct place on the page Display(); cout << "D Number: "; cin >> dNumber; string val1, val2, val3, val4, val5, val6, val7, val8; // Search through each line for dNumber while(getline(dealers,line)) { istringstream isstream (line); getline(isstream,val1,'~'); getline(isstream,val2,'~'); getline(isstream,val3,'~'); getline(isstream,val4,'~'); getline(isstream,val5,'~'); getline(isstream,val6,'~'); getline(isstream,val7,'~'); getline(isstream,val8,'~'); // If dNumber matches out put the name and the branch if (dNumber.compare(val1) == 0) cout << "\n" << setw(22) << "Dealer Name: " << val4 << "\t" << "Mailbox: " << val2 << "\t" << "Branch: " << val8 << endl; } } // Puts the D Number request in the middle of the Dos Window void Display() { for (int i = 0; i < 11; i++) cout << endl; for (int j = 0; j < 33; j++) cout << " "; }
Last edited by chuck577; Oct 24th, 2006 at 6:26 pm.
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
If anyone is interested I fixed the DEALER NOT FOUND problem. All I did was make a bool variable named match. If it matched it set it to true and broke out of the while loop. If it didnt match I set it to false and it continued to search until it found a match. If no match was found i printed out "DEALER NOT FOUND" outside of the while loop. Now time to make the whole thing loop!
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#9 Oct 27th, 2009
•
•
•
•
If anyone is interested I fixed the DEALER NOT FOUND problem. All I did was make a bool variable named match. If it matched it set it to true and broke out of the while loop. If it didnt match I set it to false and it continued to search until it found a match. If no match was found i printed out "DEALER NOT FOUND" outside of the while loop. Now time to make the whole thing loop!
Thanks,
sodak
![]() |
Similar Threads
- Search string in a text file (C)
- read text file (C)
- Binary Search on a text file (Java)
- read text file (C#)
- Parsing Text file (Java)
- New User (C)
Other Threads in the C++ Forum
- Previous Thread: getting some weird errors don't understand. dynamic array + Iterator.
- Next Thread: convert decimal digits to string...help plz
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






