Clearing a String Stream [duplicate] - c++

This question already has answers here:
How do you clear a stringstream variable?
(9 answers)
Best way to empty stringstream?
(1 answer)
Closed 9 years ago.
How do I clear a string stream. I am tokenizing a file line by line, and the stringstream is global. I can tokenize the first line, but I can't get the second line working
stringstream currentLine;
while (!inputFile.eof())
{
currentLine << getlinefromfile(); //Gets the next Line
tokenizeLine(); //Tokenizes the line using the global stringstream peek() fnc
currentLine.str("");
currentLine.clear();
}
The above is my current attempt, but it doesnt work.

Related

A problem with cin after a getline makes my while loop never end at the second iteration in c++ [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed last year.
If I replace the getline for a cin, just as a test, it works as I want. But the moment I introduce the getline it creates a never-ending loop at the second iteration. I think it has something to be with the buffer but I don't know how it works so I need help.
This is the code:
while(true)
{
alumno++;
cout<<"Alumno "<<alumno<<":"<<endl;
getline(cin,nombre_alumno);
if(nombre_alumno == "EXIT")break;
cin>>nota;
}
After this statement
cin>>nota;
insert
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
You will need to include the header
#include <limits>

"New Line" connected to value when reading in a CSV? [duplicate]

This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 6 years ago.
I am currently trying to read in a CSV file to place it into an array, but when I execute the code, the program seems to read over the endline to the next comma which messes up my output. Here is the code:
while (!inFile.eof()) {
string line = "";
while (count_1 <= numValuesPerLine) {
getline(inFile, readFromFile, ',');
line.append(readFromFile);
count_1++;
}
cout << line << endl;
count_1 = 0;
}
'line' ends up having the value:
12345678910111213141516171819202122232425\n1
which when I print it, places that newline next to '25' and messes up the output.
(numValuesPerLine = 25 and count_1 is initialized outside of the loop)
I looked around for a similar answer but I could not find anything exactly like what I am trying to do, any help would be greatly appreciated, thank you.
you changed the delimiter from \n to , so of course the newline is kept as part of the input

reading from file to vector- last line gets repeated [duplicate]

This question already has answers here:
Reading from text file until EOF repeats last line [duplicate]
(7 answers)
Testing stream.good() or !stream.eof() reads last line twice [duplicate]
(3 answers)
reading a line in text file twice
(4 answers)
Closed 9 years ago.
I am trying to read values from a file to a vector
std::vector<float> setTimesArray (std::string flName){
int i=0, dummy=0;
float temp;
std::vector<float> pObs;
std::string line;
std::ifstream inFile;
inFile.open(flName.c_str());
if(!inFile){
std::cout<<"\n.obs file not valid. Quitting programme...";
exit(1);
}
while(inFile.good()){
i++;
getline(inFile, line);
if(i>=3){ //I don't want first two lines
std::istringstream in(line);
in>>dummy;//discards first value in the line
in>>temp;
pObs.push_back(temp);
in.str(""); //discards remaining part of the line
}
}
return pObs;
inFile.close();
}
Problem is, the last value gets repeated. For example, flName had total 975 lines. Thus pObs must be having size=973 (975-2 initial lines). But the size is 974 and I see that the last value is repeating. What mistake have I made?
try:
while (getline(inFile,line))
instead of while(inFile.good())
and remove the getline() call from within the method.
You may also want to change your last two lines of codes to this, as per Daniel Kamil Kozar's suggestion:
inFile.close();
return pObs;
After the last line, good() is still allowed to return true. It doesn't have to return false until after a failed read. Thus, if it returns true, and then fails the read, your line variable won't take a new value. The correct solution would probably be to correct the bounds checking, but in this case, moving the declaration of line into the scope of you while loop and checking for and empty string should correct the issue.

string variable to open file [duplicate]

This question already has answers here:
c++: ifstream open problem with passing a string for text file name [duplicate]
(3 answers)
Closed 10 years ago.
My code should open 100 files (and do something with them) with next indexes in path like:
"c:\Naprzeme\NAPRZ100.IN" next one is "c:\Naprzeme\NAPRZ101.IN" and etc. :
for (int as=100;as<159;as++){
ostringstream ss;
ss << as;
string cherk = ss.str();
string supremeCounter = "c:\\Naprzeme\\NAPRZ"+cherk+".IN";
fstream infile(supremeCounter);
//....other code here
}
and fstream infile(supremeCounter) returns error
28 31 C:\Users\talent\Documents\File.cpp [Error] no matching
function for call to 'std::basic_fstream<char>::basic_fstream(std::string&)'
candidates are: //(here some libs)...
fstream infile(supremeCounter.c_str());

How to break a line at each ';', saved in char* [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
Can sombody help me dividing a line in parts?
I want to break the line at each ';', it is stored in myLine.
Example of a line:
surname firstName;6;7;4;10;5;9;8;3;6;7;4;10;5;9;8;6;7;4;10;5;9;6;7;4;10;5;9;
fgets(line[i], LAENGE, datei);
char* myLine = line[i];
I'm thankful for every tip! :)
std::istringstream iss(myLine);
std::vector<std::string> v;
std::string current;
while(std::getline(iss, current, ';'))
v.push_back(current));
I may have mixed up the parameter order in getline