I have a text file with a series two strings delimited by a colon on each line.
I'm using getline to grab the entire line then string stream to split the two strings and put them onto a vector. The code works fine on the first pass it grabs the strings perfectly. Then after that on the 2nd pass of the while loop and so forth it doesn't grab the new input. The string stream seems to leave the original first values for some reason.
if (infile.is_open()) {
std::stringstream ss;
std::string current_line;
std::string tempProxy;
std::string tempPort;
while (std::getline(infile, current_line)) {
ss << current_line;
std::getline(ss, tempProxy, ':');
std::getline(ss, tempPort);
std::cout << tempProxy << " and " << tempPort << std::endl;
}
Any idea why it doesn't want to grab the strings from current_line on any pass except the first iteration?
You're reusing ss but not resetting it correctly. When you extract the second word from the first line, the stream is exhausted and put in an 'EOF' state. When streams are in this or any other 'error' state they don't do anything. You have to clear the error before you can continue to use them.
If you were to check for errors returned by operator<< and getline in the loop (or if you were to cause ss to throw exceptions on errors*) you would find they are indicating that they are not successful past the first iteration. It's a good general practice to always check for errors, and especially so when you're debugging.
You can clear the error by changing your loop:
while (std::getline(infile, current_line)) {
ss.clear(); // clears the error, not the contents
ss << current_line;
However doing this means that ss will accumulate all the lines in its internal buffer. The code will produce your expected output unless the file is large and you run out of memory or something like that.
You can see the accumulating internal buffer with the following:
while (std::getline(infile, current_line)) {
ss.clear();
ss << current_line;
std::cout << "ss internal buffer: " << ss.str();
Instead of using the formatted input to add ss you are probably better off using the .str() member to set it, which will replace the previous data instead of adding to it.
while (std::getline(infile, current_line)) {
ss.clear();
ss.str(current_line);
Alternatively you can construct a new stringstream in each iteration of the loop. This does ensure that no error states or data are carried over from previous iterations. It may also be slower, but you'll have to profile that for yourself.
while (std::getline(infile, current_line)) {
std::stringstream ss(current_line);
* Exceptions are nice because you don't need to remember to check them... except in cases like this where they're not enabled by default. Also I've noticed some C++ implementations have bugs in their iostreams exception code because people don't use it much.
I think you're looking for something like:
if (infile.is_open()) {
std::stringstream ss;
std::string current_line;
std::string tempProxy;
std::string tempPort;
while (std::getline(infile, current_line)) {
std::stringstream to_split;
to_split.str(current_line);
std::getline(to_split, tempProxy, ':');
std::getline(to_split, tempPort);
std::cout << tempProxy << " and " << tempPort << std::endl;
}
Related
I have to read some data from an input stream, run it through some filters, and then display the context on screen. I'm working with Windows and currently stuck on this. I'm having a hard time understanding the I/O streams.
I don't know what I should do after passing the data through the first filter because currently, the second function calculates 0 chars?
main:
std::ifstream data("somedata.txt");
whitesprem(data);
calc_chars(data);
whitesprem:
// removes extra white spaces
void whitesprem(std::ifstream& fff) {
std::string line;
while(std::getline(fff, line)){
std::regex rgx("\\s{2,}");
line = std::regex_replace(line, rgx, " ")
//How to move on?
}
}
I'd consume each line inside your whiteprem(). Another approach could be to just copy the data to another string stream from the file stream and use the new stream in the rest of the program.
Probably you intend to do something like this:
void whiteprem(std::ifstream& ff, std::stringstream &update){
std::string line;
while(std::getline(fff, line)){
std::regex rgx("\\s{2,}");
line = std::regex_replace(line, rgx, " ")
//How to move on?
update << line << std::endl;
}
}
int main(){
std::ifstream data("somedata.txt");
std::stringstream ss;
whitesprem(data, ss);
calc_chars(ss);
}
I have a file with multiple lines.
lines contain integers separated by commas
In the following code it only parses the first line, but not the renaming lines. Any insight about I am doing wrong ?
void parseLines(std::ifstream &myfile){
std::string line, token;
std::stringstream ss;
int i;
vector<int> r;
while(myfile) {
getline(myfile, line);
ss.str("");
ss.str(line);
if(myfile){
cout << "SS" << ss.str() << endl;
while (std::getline(ss, token, ',')){
std::cout << token << std::endl;
}
}
}
}
Any insight about I am doing wrong?
The state of ss needs to be reset before data from the second line can be read.
Better yet, move the construction of ss inside the loop.
While at it,
replace while(myfile) by while(getline(myfile, line)).
Move the declaration of token inside the loop.
void parseLines(std::ifstream &myfile){
std::string line;
int i;
vector<int> r;
while( getline(myfile, line) )
{
std::stringstream ss(line);
std::string token;
while (std::getline(ss, token, ',')){
std::cout << token << std::endl;
}
}
}
The issue here is the stringstream is not local to the while loop. When you read from the stringstream the first time you exhaust the stream which causes the EOF flag to be set. If you do not clear that then you will never read any more information from it even if you load more. The simplest way to get around this is to make the stringstream local to the loop body so you start off with a fresh one on each iteration and you do no have to worry about cleaning up the flags. That would make your code look like
while(getline(myfile, line)) // also moved the line reading here to control when to stop the loop
{
std::stringstream ss(line);
while (std::getline(ss, token, ',')){
std::cout << token << std::endl;
}
}
I'm trying to insert white space in std::stringstream in this way.
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
and then setting it as a label like that
label->setString(sstr.str().c_str());
but it's only giving me 10, space is not included. I've followed many links to solve problem but of no use. Following link suggests to use getline() but in my case I cannot do that :
stringstream doesn't accept white space?
I've also tried to use std::noskipws but it also not work :
sstr << std::noskipws << " ";
Any help will be appreciated.
std::stringstream should not remove your whitespace when used like this. Are you sure that it is not the label-object that is trimming your string and removing the whitespace?
Try debugging or printing out your string before setting it to the label.
You can use put method to append a single char:
std::stringstream sstr;
sstr.str("");
sstr.put(' ');
sstr.put(10);
I think you're using it wrong:
string labelstr;
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
ss >> noskipws >> labelstr;
label->setString(labelstr);
http://www.cplusplus.com/reference/ios/noskipws/
Your code works as expected for me. Here's a runnable example: http://cpp.sh/8d6.
The culprit must be setString trimming your input. Or possibly some other function that reads the string later.
if(inputFile.is_open()){
while(!inputFile.eof()){
getline(inputFile, line);
ss << line;
while(ss){
ss >> key;
cout << key << " ";
lineSet.insert(lineNumber);
concordance[key] = lineSet;
}
lineNumber++;
}
}
For some reason, the while loop is kicking out after the first iteration and only displays the first sentence of my input file. The rest of the code works fine, I just can't figure out why it thinks the file has ended after the first iteration.
Thanks
Firstly you should be reading the file without using eof , as πάντα ῥεῖ notes (see here for explanation):
while( getline(inputFile, line) )
{
// process the line
}
Note that the preceding if is not necessary either.
The main problem , assuming ss is a stringstream you defined earlier, comes from the logic:
ss << line;
while(ss){
// stuff
}
The while loop here only exits when ss fails. But you never reset ss to be in a good state. So although your outer loop does read every line of the file, all of the lines after the first line never generate any output.
Instead you need to reset the stringstream each time:
ss.clear();
ss.str(line);
while (ss) {
// stuff
}
For example, when parsing a text file, some times this file have stuff like this:
keyword a string here
keyword another string
keyword
keyword again a string
Note that the 3th line have an empty string (nothing or white spaces).. The thing is that when you do stringstream>>laststring, and stringstream have an empty string (null or just white space), it will not overwrite the "laststring", it will do nothing. Theres anyway to check this situation before hand? I dont want to create a temp empty string just to check it is still empty after stringstream>>, seems lame.
When you cannot read from stream - its state changes, so when casting to bool, it returns false:
bool read = static_cast<bool>(ss >> laststring);
Or - in if-expr:
if (ss >> laststring)
cout << "Just read: " << laststring;
See example
You can only know after trying to read whether there was something or not. What you might be able to do is to skip whitespace and see if there is a non-space in the next location:
if ((in >> std::ws).peek() != std::char_traits<char>::eof()) {
...
}
Given that empty strings are cheap to create, I wouldn't bother and try read the string. Note, however, that reading from streams isn't line based, i.e., in your case above you need to split the lines first or use something like std::getline() to read the second part of line.
You can use getline, to read a line from the file. Then, copy the line into a string stream and read words from the string stream one at a time. The streams will automatically stop reading when they run out of lines / words.
// open file
std::ifstream fin("text.txt");
// 'iterate' through all the lines in the file
unsigned lineCount = 1;
std::string line;
while (std::getline(fin, line))
{
// print the line number for debugging
std::cout << "Line " << lineCount << '\n';
// copy line into another stream
std::stringstream lineStream(line);
// 'iterate' through all the words in the line
unsigned wordCount = 1;
std::string word;
while (lineStream >> word)
{
// print the words for debugging
std::cout << '\t' << wordCount++ << ' ' << word << '\n';
}
}
You need to include iostream, fstream, sstream and string.
For checking if string is empty, use foo.size() == 0.
For checking if string stream is empty fooStream.rdbuf()->in_avail() == 0