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

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

Related

How to replace a value in a string with another in c++? [duplicate]

This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 2 years ago.
if I have a string eg
2,4,-1,3,-1,
how do I replace all the -1's with the word "wrong"? and how do I remove the comma at the very end?
the output has come from an array
cout<<array[c]<<",";
I need a basic solution please
thank you
Hard to understand what you are trying to say, better if you edit your question. You could do something like this (stackoverflow answer),
std::string subject("2,4,-1,3,-1,");
std::string search("-1");
std::string replace("wrong");
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos)
{
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
// Delete last ,
subject.pop_back();
std::cout << subject;

"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

Clearing a String Stream [duplicate]

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.

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.

Reading delimited files in C++ [duplicate]

This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 8 years ago.
What is the best way to read in a tab delimited file in C++ and store each line as a record? I have been looking for an open source library to help with this, but have been unsuccessful so it looks like I will have to write my own.
typedef vector<vector<string> > Rows;
Rows rows;
ifstream input("filename.csv");
char const row_delim = '\n';
char const field_delim = '\t';
for (string row; getline(input, row, row_delim); ) {
rows.push_back(Rows::value_type());
istringstream ss(row);
for (string field; getline(ss, field, field_delim); ) {
rows.back().push_back(field);
}
}
This will get you started. It doesn't do any checking that each row has the same number of fields, allow for escaping field_delim, etc.
There is no problem in using iostreams - you could read each line with getline into string, and then use stringstream on that string to iterate over fields.
There are a few libraries listed in wikipedia's article CSV_application_support.