lundi 31 mars 2014

C++ How do we find each line of a word from a text file?


Vote count:

0




I am reading a text file word for word and I am trying to find the number of the line the word is in. For example if I had the following:


Dog Cat Car Truck


Dog is found on line one, Cat is found on line one, Car line two, and Truck line 3.


I have the following code:



int main(){
string word;
ifstream inFile;
Node* rootPtr = NULL; // Pointer to the root node

inFile.open("example.txt");
if (!inFile)
cout << "Unable to open text file";

while (inFile >> word) {
if (word == "#")
break;
else{
rootPtr = Insert(rootPtr,word.substr(0,10));
}
}
inOrderPrint(rootPtr);
inFile.close();
}


You can ignore anything to do with the pointers. That is for some other stuff. I have tried figuring out how to check for the end of the line and creating a counter that will increment every time then end of a line is found but I was unsuccessful.


Thanks for your help!



asked 54 secs ago

Nick

23





Aucun commentaire:

Enregistrer un commentaire