Question

In: Computer Science

c++ How do I get it before a particular string when I use <ifstream>? For example,...

c++

How do I get it before a particular string when I use <ifstream>?

For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple).

And then I want to move on to the next row.

How can i do?

Solutions

Expert Solution

Thanks for the question, For your requirement , you need to use getline function

I have demonstrated below as how to do it using a very simple file. Comments are included so that you can understand and apply on your own.

Refer the screenshot of the file and the output. I think this is what you were looking for.

Do let me know in case you need any further help.

=========================================================================

#include<iostream>

#include<fstream>

using namespace std;

int main(){

               

               

                ifstream infile("D:\\veg.txt");

               

                string name,junk;

               

                // use getline to read upto a particular character in a line without reading that particular character ( here it is : )

                // takes 3 arguments, stream, string variable and delimitting character

                // reading stops just before the ':' character

                // string before the character ':' get stored in the variable name

                while(getline(infile,name,':')){

                                cout<<name<<endl;

                                // read the left over to a string variable, with delimiting character as new line character

                                getline(infile,junk,'\n');

                                // move to the next row

                               

                }

                infile.close();

}


Related Solutions

c++ I want to flip the string when it's string type. For example, if it is...
c++ I want to flip the string when it's string type. For example, if it is 'apple', I would like to print 'elppa'. how can i do?
a. What is Bitbucket and how do I get there? b. What is Sourcetree? c. How...
a. What is Bitbucket and how do I get there? b. What is Sourcetree? c. How do I get Sourcetree and install it? d. With a lot of details and screen shots show the following with Sourcetree i.Clone a repository from Bitbucket ii.Fetching iii.Pulling iv.Staging v.Committing vii.Pushing
How do I know if my company is going downhill ?(money wise) before I get bankrupt...
How do I know if my company is going downhill ?(money wise) before I get bankrupt because till this day I had no such problem but when the day comes I want to be prepare!!! What should I review or see.?
How do I know if my company is going downhill ?(money wise) before I get bankrupt...
How do I know if my company is going downhill ?(money wise) before I get bankrupt because till this day I had no such problem but when the day comes I want to be prepare!!! What should I review or see.?
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
Who get a performance appraisal? How do you get a performance appraisal? When when do you get a performance appraisal?
Who get a performance appraisal?How do you get a performance appraisal?When when do you get a performance appraisal?
How do I get the first initial of a first, middle, and last name? Also when...
How do I get the first initial of a first, middle, and last name? Also when I look to count characters in the name I want to be abel to count the spaces in-between the names how can i do this?
How do i know when to use a specific leader behavior?
How do i know when to use a specific leader behavior?
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
please do this in C++! I want to understand it, it must be done before the...
please do this in C++! I want to understand it, it must be done before the evening or nightime. Follow instructions exactly as it says. Please send a screenshot also with your code so I can see how it is supposed to be formatted. Since typing it a chegg answer, makes it look a bit messy. Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT