i want to read stuff in a text file one line at a time by line because i have to store the first thing in each line in a list the first time though, close the file reopen it again to get back to the top of file, search for that first item in the first line that was placed in the list and add the rest of the info in that like so it is associated with it then go to the next line and so on. at the end of each line there is a # to indicate the end.
the only thing i am having trouble with is with the loop where you put the first item in the line and skip the rest of the things in the line until u hit the # symbol.
here is an example txt file of what i mean
Code:
Atlanta Houston 650 Washington 600 #
Austin Dallas 200 Houston 300 #
Buffalo New_York 450 Newark 500 #
Chicago Denver 550 New_York 950 #
Dallas Austin 200 Chicago 1500 #
Denver Atlanta 800 Chicago 550 #
Houston Atlanta 650 #
Newark #
New_York Chicago 950 Buffalo 450 #
Washington Atlanta 600 Dallas 700 #
here is the function i am working on i have highlited in red where my trouble spot it. its the while statement that i dont get to how i can recognize the # so it knows when to stop inFiling nothing and go to the next line. the way i have it now i get an error because of the != but dosent string have a != operator? i made one myself and it still said the same thing
Code:
void Graph::GetGraph()
{
Edge edge;
Vertex V;
string vName;
cout << "Enter the the file location" << endl;
cin >> inFileName;
inFile.open(inFileName.c_str());
if (!inFile.is_open()) //test for file
{
cerr << "Cannot open file: " << inFileName << endl;
getche();
return;
}
while(!inFile.eof()) //loop through untill end of file
{ //cout << "hi" << endl;
inFile >> V.name;
g.push_front(V);
VertNum++;
// Loop until # is inFiled - the next inFile will either be another V or eof
while(V.name != '#')
{
inFile;
}
}// end obtain info
//cout << "hi" << endl;
inFile.close();
inFile.clear();
// Reopen file - load edges
while(!inFile.eof())
{
inFile >> vName;
// Find matching Vertex V
Vertex *walker;
Vertex *ptr;
if(!first)
{
cout << "There is no Graph" << endl;
return;
}
ptr = NULL;
walker = first;
while(!walker && (vName > walker->name))
{
ptr = walker;
walker = walker->pNextVert;
}
if(vName == walker->name)
V.name = walker->name;
else
return;
/****************************************************/
// Find the ptr to Vert
// Loop thru edge-list
while(!'#')
{
inFile >> edge.name;
inFile >> edge.weight;
V.edgelist.push_front(edge);
}
}
inFile.close();//close infile
}