Hi,

Can anyone help me with this problem.For example i have a text file called food.txt with contents


1 apples
2 oranges
3 mango
4 bananna


What i would like to do is enter the choice of fruit into a string and loop through the file until the word is found.My problem being i dont fully understand any of the examples online and would be grateful if someone could point me in the right direction.

Here is my code so far

main(){
    
    char filename[size];
    char line[size];
    string fruit;
    
    
    cout << "Please enter filename: ";
    cin >> filename;
    
     cout << "enter fruit:" << endl;
    cin >> fruit;
    cout << fruit;
    
    
    cout << "\n Filename is: " << filename << endl;
    
    cout << "Opening file.." << endl;
    
    fstream* file = new fstream(filename,fstream::in);
    
    while(file->getline(line,size) != NULL)
    {
        cout << line;
        
        // if the line is the same as fruit entered print out the line(this does not work!)
        if(line == fruit)
        {
            cout << line;
        }
    }
    
    system("PAUSE");
    
    
}

Recommended Answers

All 8 Replies

First of all you don't want to use getline() to read a single word because getline() will read the entire line. It is better to use >> operator to read words like this:

std::string word;
std::string filename;
...
...
// open the ifstream (Note:  ifstream should NOT be declared as a pointer!)
// use ifstream instead of fstream for input-only streams
ifstream file(filename.c_str()); 
// read the file one word at a time
while( file >> word )
{
   if( word == fruit )
   {
         cout << "Found it!\n";
          break;
   }
}

Thats great thank you for the help,what i forgot to mention was that when the word is found,i want to take it out of the file.


For example in my file:

1 apples
2 oranges
3 mango
4 bananna

There is numbers and a space before each of the fruits.So if i picked #2,there is a number and a whitespace character preceding it.

What im trying to figure is how to take the contents of the line in the file out,but am struggling to come to terms with how to do so.

The only way that can be done is to rewrite the entire file. One way is to read everything into an array, or vector, or strings, then reopen the file for writing. Rewrite all the strings except the one you want to delete.

Since the lines contain other stuff you might want to use getline() as you originally posted. Then replace if(line == fruit) with this: if( line.find(fruit) != string::npos) >> while(file->getline(line,size) != NULL)
Recode that like this: while( getline(file,line) ) if you make line a std::string instead of char*

:/ im sorry again but my communication today is dire,iv been looking at a computer screen for most of the day so my head is abit clouded.

What i MEANT to say(and this time im going to explain it properly!) is i dont mean literally TAKE OUT of the file,just to display them on the screen again.

FOr instance if the user wishes to see what fruit is #3,they input this number and it is retrieved,or alternativley they can enter the name of the fruit and it is displayed.

Im sorry for not being so clear the last time! :)

Well, with the help above, I'm certain you can figure it out. You know the format of the file, you know what you type in at the prompt. From there, AD's code has a bunch of what you need. Just add to that what you already know.

Unfortunately thats not the case walt.Im very confused about searching text files by individual lines and starting at a point in a text file through a key word,id appreciate any help thats the reason im posting on this board.

Read a line. Check if the line contains the number (at the beginning) or the text (at the end). Since you're using character strings, the strxxx family of functions will help. Also, you can look at each individual character in the array/string.

Also, in Englis, there is a space after each period, comma, question mark, etc. They are there for readability, not just to annoy the writer :icon_wink:

There are any number of ways to do this.

If I assume the file has a number of lines of text and each line has two fields, one being a word which is preceded by a space which is preceded by a number, then I would create a class called Item. Each object of type item will have a number and a name as member variables. I can read file using getline() and separate the two fields after reading in the line, or I can use >> to read in each field of the line one by one. Either way I will use a loop to read the file line by line putting the value of each field into an object of type Item. I will then push Item onto the back of a vector of Items and go on to the next line, unil I've read the whole file into the vector of Items. Then I can search the vector of Items by using either of the member variables of the items as the target.

Alternatively I could use a map to store the information in the file and use the number as the key to the map and the name as the data.

If I wasn't allowed to use vectors or maps and I didn't know about classes then I would use parallel arrays and use the index of the number or the name to get the other information about a given item.

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.