943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 28942
  • C++ RSS
Oct 24th, 2006
0

Newbie C++ Search and Parsing Text file.

Expand Post »
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. :]

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Oct 24th, 2006
2

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

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Oct 24th, 2006
0

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

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Oct 24th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by chuck577 ...
while(!dealers.eof())
{
getline(dealers,line, '~'); //get line and ignore/skip delimiter
}
Use the following loop to read the file line by line.
C++ Syntax (Toggle Plain Text)
  1. while(getline(dealers,line)
  2. {
  3.  
  4. }
That is better than checking for the eof bit of the file stream.

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

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

C++ Syntax (Toggle Plain Text)
  1. string token;
  2. istringstream isstream (line);
  3. while ( getline( isstream ,token, '~') )
  4. {
  5. cout << token << " ";
  6. }
  7. cout << "\n";
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Oct 24th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by WolfPack ...
Use the following loop to read the file line by line.
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Oct 24th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by chuck577 ...
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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Oct 24th, 2006
1

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Oct 24th, 2006
2

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

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Oct 27th, 2009
-1
Re: Newbie C++ Search and Parsing Text file.
Click to Expand / Collapse  Quote originally posted by chuck577 ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sodak is offline Offline
6 posts
since Oct 2009
Nov 12th, 2010
0
Re: Newbie C++ Search and Parsing Text file.
sorry for replying on this old thread, but i'm trying to repeat this code, in my learning of input/output files. Wondering what headers you used?? When i compile this, I get errors

error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided

been googling, and see that the problem may lie in which headers are included? I'm running visual studio 2008 btw, and trying to use these includes and using namespace std

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frenzy50187 is offline Offline
1 posts
since Nov 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: loop iterates an extra time
Next Thread in C++ Forum Timeline: homework help





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


Follow us on Twitter


© 2011 DaniWeb® LLC