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?
C++... How do I separate a C String, entered by a user, by commas? Say they...
C++... How do I separate a C String, entered by a user, by commas? Say they enter Frog,Wolf,Spider... I want a array containing the elements {Frog, Wolf, Spider} thanks Here's my idea so far... cout << "Enter the column names (c of them), each name seperated by a comma" << endl; char colNames[100]; cin >> colNames; for (int i = 0; i < 99; ++i){ if (colNames[i] == ','){ //parse the string, assign that string name to that column }...
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?
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT