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. :]

#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 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.

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:

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.

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

Use the following loop to read the file line by line.

while(getline(dealers,line)
{

}

That is better than checking for the eof bit of the file stream.

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.

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.

string token;
istringstream isstream (line);
while ( getline( isstream ,token, '~') )
{
    cout << token << " ";
}
cout << "\n";

Use the following loop to read the file line by line.

while(getline(dealers,line)
{

}

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.

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

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.

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. :)

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 << " ";
}

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!

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

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>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.