I have an input file that has multiple lines in the current format:
[message1]/[message2]/[message3]
I have to extract [message#] and put each into a separate container (vector, array, etc). Is there an easy way to do this with a stringstream?
Note: [message#] will NOT contain the character '/'.
I have initially tried to do this:
istringstream iss;
iss >> message1 >> "/" >> message2 >> "/" >> message3;
but it wouldn't work. Any hints appreciated.
Related
I'am working on a program where there are first names, last names and a numbers on a file and i need to read that information into my program. My actual problem is that there is people who doesnt have a second name or second last name. To solve the issue I started trying to read from a file until a specific character is found, For example:
Robert, Ford Black,208 //Where Robert is the first name, and Ford Black are his two last names
George Richard, Bradford,508 //Where George Richard are both his first names, and Bradford is his only last
name
I am saving this information in three separeted string, one that will store first and second name, first last and second last name and the third one for the numbers.
I'm trying to only use native libraries from c++.
I've been reading that getline(a,b,c) and IStringStream can actually solve my problem but I don't know how to correctly implement it
It's just a matter of using std::getline with a delimiter character to read out of the string stream. See a simplified example (no error checking) below:
for (std::string line; std::getline(std::cin, line); )
{
std::string firstName, lastName;
std::istringstream iss(line);
std::getline(iss, firstName, ','); // A comma delimits end-of-input
iss >> std::ws; // Skip over any whitespace characters
std::getline(iss, lastName); // Read remaining line
std::cout << "First Name: " << firstName << std::endl;
std::cout << "Last Name: " << lastName << std::endl;
}
Note the line iss >> std::ws; using std::ws from <iomanip> is there to eat up extra whitespace characters (which appear after your comma, in your example).
I'm assuming the C++ line comments in the input are only an annotation for this question, and not part of the actual input.
#include<bits/stdc++.h>
using namespace std;
int main()
{
ifstream myfile("files.txt");
string fullname;
while(getline(myfile,fullname,'/')) break; //here im reading till the first / is acquired and the entire string is stored in "fullname"
string firstname,lastname;
size_t pos=fullname.find(',');
firstname=fullname.substr(0,pos); //store the firstname
lastname=fullname.substr(pos+1);// storee the lastname
cout<<firstname<<" "<<lastname;
}
As the question posed was to read names im assuming before the digit if there were a " / " you can read upto the first occurance of /. this will give you the fullname. Then using the substr on the fullname and find the occurance of a comma if at all it exists. All the characters to the left of position of comma will form your first name and the rest on the right of the position of comma will form the lastname.
I'm having my head around for a couple of days with this question, and I still can't solve my problem.
If I need to read two strings from keyboard I can do this:
cin >> str1 >> str2;
However I want to filter the input.
What I'm trying to do, is something like this:
cin >> filter1 >> str1 >> filter2 >> str2;
or alternatively
cin >> filter1 >> filter2 >> str1 >> str2;
Where filter1 and filter2 are functions with regexes for filter the input.
My biggest trouble is I can't see how to insert the functions, between the input and the strings. .
Any help appreciated
Your filter function should return object of class that implements filtering using regex. Then you would overload operator>> to accept that object, which would read data and invoke it. Peek into istream header to get idea how to do that. Object must be destroyed or it may be a singleton of some kind
Hi I have a string like this:
word1--tab--word2--tab--word3--tab--word4--tab--word5--tab--word6
I need to extract the third word from the string. I thought of reading character by character and getting the word after reading the second tab. But I guess it is inefficient. Can you show me a more specific way please?
std::string has the find method which returns an index. You can use
find("--", lastFoundIndex + 1)
three times to find the start index of your word, a fourth time for the end index, and then use substr.
assuming "tab" is \t;
std::istringstream str(".....");
std::string temp, word;
str >> temp >> temp >> word;
I have for example
string s = " abc edef";
I create istringstream with this string.
Is there any way to from getline get only "abc"and "edef" ? Beacouse now I get that empty string between pairs of spaces :/
Use the >> operator to get only whitespace-delimited "words".
I'm newbie in boost. I have string delimeted with tab ( '\t' ).
How can i parse it with boost::spirit?
parser code from boost's samples
The boost sample code isn't the same as the actual boost sample, which was comma delimited, so presumably there are your modifications?
The ascii::space parser will handle the tabs for you as delimiters, so something like:
start %=
lit("employee")
>> '{'
>> int_ >>
>> quoted_string >>
>> quoted_string >>
>> double_
>> '}'
;
Should work (minus the 'lit('\t')'). But, this will also parse other spacing characters (e.g. space, tab).
If you actually need there to explicitly be single tabs ONLY between the terms, then leave in the lit('\t') and wrap it in a lexeme[] to disable skipping by the skip parser.