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: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

Newbie C++ Search and Parsing Text file.

 
0
  #1
Oct 24th, 2006
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. :]

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <string>
  6. using namespace std;
  7.  
  8.  
  9.  
  10. void Display();
  11. void SearchNumber(string);
  12.  
  13. int main()
  14. {
  15. // Variable for the dealer number
  16. string dNumber;
  17.  
  18. // ofstream constructor opens file
  19. ifstream dealers ( "dealers.tdl", std::ios::in);
  20.  
  21. // exit program if unable to open file.
  22. if (!dealers)
  23. {
  24. cerr << "File could not be opened" << endl;
  25. exit(1);
  26. }
  27.  
  28. Display();
  29.  
  30. cout << "D Number: ";
  31. cin >> dNumber;
  32.  
  33.  
  34. }
  35.  
  36. // Puts the D Number request in the middle of the Dos Window
  37. void Display()
  38. {
  39. for (int i = 0; i < 11; i++)
  40. cout << endl;
  41. for (int j = 0; j < 33; j++)
  42. cout << " ";
  43.  
  44. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Newbie C++ Search and Parsing Text file.

 
1
  #2
Oct 24th, 2006
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 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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

Re: Newbie C++ Search and Parsing Text file.

 
0
  #3
Oct 24th, 2006
Originally Posted by WolfPack View Post
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 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.
Ok, Ive read the file line by line using this:

  1. while(!dealers.eof())
  2. {
  3. getline(dealers,line, '~'); //get line and ignore/skip delimiter
  4. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Newbie C++ Search and Parsing Text file.

 
0
  #4
Oct 24th, 2006
Originally Posted by chuck577 View Post
while(!dealers.eof())
{
getline(dealers,line, '~'); //get line and ignore/skip delimiter
}
Use the following loop to read the file line by line.
  1. while(getline(dealers,line)
  2. {
  3.  
  4. }
That is better than checking for the eof bit of the file stream.

Originally Posted by chuck577 View Post
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.
Yes. That is correct.

Originally Posted by chuck577 View Post
3. Create a istringstream object lets call it isstream, from the string line.

I understand creating the object using:

istringstream isstream (line)?
Yes. That is the way. Now try the following piece of code.

  1. string token;
  2. istringstream isstream (line);
  3. while ( getline( isstream ,token, '~') )
  4. {
  5. cout << token << " ";
  6. }
  7. cout << "\n";
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

Re: Newbie C++ Search and Parsing Text file.

 
0
  #5
Oct 24th, 2006
Originally Posted by WolfPack View Post
Use the following loop to read the file line by line.
  1. while(getline(dealers,line)
  2. {
  3.  
  4. }
That is better than checking for the eof bit of the file stream.


Yes. That is correct.


Yes. That is the way. Now try the following piece of code.

  1. string token;
  2. istringstream isstream (line);
  3. while ( getline( isstream ,token, '~') )
  4. {
  5. cout << token << " ";
  6. }
  7. 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

  1.  
  2. while(getline(dealers,line))
  3. {
  4. string token;
  5. istringstream isstream (line);
  6. while (getline(isstream,token,'~'))
  7. {
  8.  
  9. // Place code to match the first
  10. // string in a line with the string
  11. // entered by the user. If it
  12. // doesnt match go to next line etc.
  13. // Output the name (4th position) in
  14. // reverse string order.
  15.  
  16. }
  17. }


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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Newbie C++ Search and Parsing Text file.

 
0
  #6
Oct 24th, 2006
Originally Posted by chuck577 View Post
I already have the line so I should need to use getline again.
You have the line, and you can get the first word delimited by a '~'. So you can use strcpy to compare the first word of the line with the word entered by the user and go to the next line if it doesnt match.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

Re: Newbie C++ Search and Parsing Text file.

 
1
  #7
Oct 24th, 2006
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.

 
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

Re: Newbie C++ Search and Parsing Text file.

 
2
  #8
Oct 24th, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: sodak is an unknown quantity at this point 
Solved Threads: 0
sodak sodak is offline Offline
Newbie Poster
 
0
  #9
Oct 27th, 2009
Originally Posted by chuck577 View Post
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!
I have a similar program I am trying to write. I think a bool type program would work better for my purposes. Could you post your code?
Thanks,
sodak
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC