Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to separate string by character "," or ";".
std::string input = "abc,def;ghi";
std::istringstream ss(input);
std::string token;
while(std::getline(ss, token, ',')) { //how to add here ";"?
std::cout << token << '\n';
}
Use the Boost Tokenizer library:
boost::char_separator<char> sep(",;");
boost::tokenizer<boost::char_separator<char>> tokens(input, sep);
what about old way style?
std::string string = "abc,def;ghi";
std::vector<std::string>strings;
std::string temp;
for(int i=0; i < string.length(); i++)
{
if(string[i] == ',' || string[i] == ';')
{
strings.push_back(temp);
temp.clear();
}
else
{
temp += string[i];
}
}
strings.push_back(temp);
live demo
Using strsep in string.h from C library , you could do it like this:
std::string input = "abc,def;ghi";
const char* ss = intput.c_str();
const char token[] = ",;";
for (const char* part; (part = strsep(&ss, token)) != NULL;) {
std::cout<< part << std::endl;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My problem sounds like this: I have as a input a huge string with numbers separated by whitespaces. I need to split this string and put the components in a vector and then to use its components. (then to transform to integers bla bla...).
I searched here for this but I did not understand some things entirely, so please a bit of explanation.
Also another question: why the following return one more "Substring: " in the end?
int main()
{
string s("10 20 30 50 2000");
istringstream iss(s);
while (iss)
{
string sub;
iss >> sub;
cout << "Substring: " << sub << endl;
}
system("pause");
return 0;
}
why the following return one more "Substring: " in the end?
Because your loop is broken; you're checking the stream state before reading from it. It's the same problem as described under:
Why is iostream::eof inside a loop condition considered wrong?
First count the amount of whitespaces like this:
int i = counter;
for( size_t i = 0; i < s.size(); ++i )
{
if( ' ' == s[i] )
{
++counter;
}
}
After that you have to substring in another for loop the string s.
Try the following approach
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
int main()
{
std::string s( "10 20 30 50 2000" );
std::istringstream is( s );
std::vector<std::string> v( ( std::istream_iterator<std::string>( is ) ),
std::istream_iterator<std::string>() );
for ( const std::string &t : v ) std::cout << t << std::endl;
return 0;
}
The output is
10
20
30
50
2000
You could initially define the vector as having type std::vector<int> and in the vector initialization use iterator std::istream_iterator<int>.
As for your second question then before outputing a string you have to check whether it was read. So the correct loop will look like
string sub;
while ( iss >> sub )
{
cout << "Substring: " << sub << endl;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Say I have a textfile "Employees.txt" with Employee name and ID.
Like so:
John:d4250
Sarah:s5355
Alan:r4350
If I have a very very basic Employee class which has a constructor for Name and ID
and I wish to read from this textfile and insert them into a vector
Am I best to use something like:
void GenericProgram::loadEmployees()
{
string line;
ifstream empFile("employees.txt");
if(empFile.fail())
{
cout << "input file opening failed\n" << endl;
exit(EXIT_FAILURE);
}
while (!empFile.eof())
{
string empName;
string empID;
while (getline(empFile, line, '\n'))
{
// this will give me the line on its own
// now how to delimit again using ':'
// then do something like
Employee e(empName, empID)
employeeVector.push_back(e);
}
empFile.close();
}
}
I apologise that this is so basic. Brain is failing on me. I was wondering if there are better ways to read from files to populate objects with streams.
Just add to your code
int pos = line.find(":", 0);
if (pos != std::string::npos)
{
std::string empName(line.begin(), line.begin() + pos);
std::string empID(line.begin() + pos + 2, line.end());
}
For multiple ":"
std::vector<std::string> str;
std::vector<size_t> positions;
positions.push_back(0);
do
{
positions.push_back(line.find(":", positions.size() - 1));
}
while(positions[positions.size() - 1] != std::string::npos);
for (int i = 0; i < positions.size(); i+=2)
{
str.push_back((line.begin() + positions[i], line.begin() + positions[i+1]));
}
But i haven`t tested it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am attempting to grab ID's from an text file with HTML in it. The ID's are being extracted from URL's in the HTML so I'm looping through the file to find the correct line and then using substrings obtain the correct information. There are two different types of ID so I have two different functions.
The second one (getYearId) works fine, but the first one causes the code to abort on the part that is currently commented out. As you can see, I've tried to output the value of first1 only to find that it's output is alue=", which is part of what I'd assume first was supposed to equal. What am I doing wrong?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
void getSyllabiId() {
string line;
ifstream myfile("syllabi.txt");
if (myfile.is_open()) {
while (getline(myfile, line)) {
if (line.find("View Assignments") != string::npos) {
string startDel = "syllabusid";
string endDel = "View";
unsigned int first1 = line.find(startDel);
unsigned int last1 = line.find(endDel);
cout << first1 + "\n";
//string syllabusID = line.substr(first1, last1 - first1);
//syllabusID = syllabusID.substr(startDel.size());
// cout << syllabusID + "\n";
}
}
myfile.close();
}
else cout << "Unable to open file.";
}
void getYearId() {
string line;
ifstream myfile("syllabi.txt");
if (myfile.is_open()) {
while (getline(myfile, line)) {
if (line.find("2014-2015</option>") != string::npos) {
string startDel = "value=\"";
string endDel = "\" selected";
unsigned int first = line.find(startDel);
unsigned int last = line.find(endDel);
string yearID = line.substr(first, last - first);
yearID = yearID.substr(startDel.size());
cout << yearID + "\n";
}
}
myfile.close();
}
else cout << "Unable to open file";
}
int main () {
getYearId();
getSyllabiId();
string x;
cin >> x;
return 0;
}
The problem was I never checked to see if first1 and last1 had values, so obviously in one (or several instances) one of them didn't causing the code to abort.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
int main() {
string firstFile, secondFile, temp;
ifstream inFile;
ofstream outFile;
cout << "Enter the name of the input file" << endl;
cin >> firstFile;
cout << "Enter the name of the output file" << endl;
cin >> secondFile;
inFile.open(firstFile.c_str());
outFile.open(secondFile.c_str());
while(inFile.good()) {
getline(inFile, temp, ' ');
if ( temp.substr(0,4) != "-----"
&& temp.substr(0,5) != "HEADER"
&& temp.substr(0,5) != "SUBID#"
&& temp.substr(0,5) != "REPORT"
&& temp.substr(0,3) != "DATE"
&& temp != ""
&& temp != "")
{
outFile << temp;
}
}
inFile.close();
outFile.close();
return 0;
}
Hi All. I'm attempting to output all lines from a text file that do not meet the criteria in the control structure -- i.e. no blank lines, no symbols, etc. However, when I run this code it outputs everything, not taking into consideration my specifc requirements. If anyone could tell me what I'm doing wrong it would be greatly appreciated.
If you look at a reference such as this you will see that the second argument to substr is the number of character not the ending position.
This means that e.g. temp.substr(0,5) might return "HEADE" which is indeed not equal to "HEADER". This means that all non-empty string will be output.
Also note that right now, you don't actually read lines but words as you separate the input on space.
When you repeat the same action multiple times, that's a sign you need a function:
bool beginsWith( const std::string &test, const std::string &pattern )
{
if( test.length() < pattern.length() ) return false;
return test.substr( 0, pattern.length() ) == pattern;
}
First of all you can test it separately, then your condition will be much simplier and less error prone:
if ( !beginsWith( temp, "-----" )
&& !beginsWith( temp, "HEADER" )
&& !beginsWith( temp, "SUBID#" )
&& !beginsWith( temp, "REPORT" )
&& !beginsWith( temp, "DATE" )
&& temp != "" )
Short version (C++11):
const std::vector<std::string>> filter {
{"-----"}, {"HEADER"}, ... }; // all accepted line patterns here
while(getline(inFile, temp)) {
for(const auto& s: filter)
if (s.size() == temp.size() &&
std::equal(s.begin(), s.end(), temp.begin()))
outFile << temp;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Splitting a string in C++
I'm trying to split a single string object with a delimeter into separate strings and then output individual strings.
e.g The input string is firstname,lastname-age-occupation-telephone
The '-' character is the delimeter and I need to output them separately using the string class functions only.
What would be the best way to do this? I'm having a hard time understanding .find . substr and similar functions.
Thanks!
I think string streams and getline make for easy-to-read code:
#include <string>
#include <sstream>
#include <iostream>
std::string s = "firstname,lastname-age-occupation-telephone";
std::istringstream iss(s);
for (std::string item; std::getline(iss, item, '-'); )
{
std::cout << "Found token: " << item << std::endl;
}
Here's using only string member functions:
for (std::string::size_type pos, cur = 0;
(pos = s.find('-', cur)) != s.npos || cur != s.npos; cur = pos)
{
std::cout << "Found token: " << s.substr(cur, pos - cur) << std::endl;
if (pos != s.npos) ++pos; // gobble up the delimiter
}
I'd do something like this
do
{
std::string::size_type posEnd = myString.find(delim);
//your first token is [0, posEnd). Do whatever you want with it.
//e.g. if you want to get it as a string, use
//myString.substr(0, posEnd - pos);
myString = substr(posEnd);
}while(posEnd != std::string::npos);