I need cut string stream according custom separator. Current code cuts just acording to several standart separators. How to define and cut stringstream to string line according to custom delimiter?
std::istringstream input;
input.str("1\n2\n3\n4\n5\n6\n7\n");
int sum = 0;
for (std::string line; std::getline(input, line); )
{
cout<<line;
}
If you have one delimiter you want to use and it's a single character, you can just pass it to the 3-parameter overload of std::getline():
std::istringstream input;
input.str("1;2;3;4;5;6;7;");
int sum = 0;
for (std::string field; std::getline(input, field, ';'); )
{
std::cout<<field;
}
Live example
For other situations (multi-character delimiter, multiple delimiters), you might want to consider using Boost.Tokenizer.
Use third argument of overloaded std::getline
for (std::string line; std::getline(input, line, delimiter ); )
{
std::cout<< line <<'\n';
}
Related
i have a text file called builders.txt that contains some data
Reliable Rover:70:1.
Sloppy Simon:20:4.
Technical Tom:90:3.
Within my main file i have a function declaration related to this specific text file
void Builder() {
std:string name;
int ability;
int variability;
}
this is my read file function
std::vector<std::string> lines;
std::string inputFile1 = "Builders.txt";
std::string inputFile2 = "Parts.txt";
std::string inputFile3 = "Customers.txt";
std::string outputFile = "output.txt";
std::string input;
void readFile(std::string const& inputFile1, std::string const& inputFile2, std::string const& inputFile3,
std::vector<std::string>& lines) //function to read Builders, Customers and Parts text file
{
std::ifstream file1(inputFile1);
std::ifstream file2(inputFile2);
std::ifstream file3(inputFile3);
std::string line;
while(std::getline(file1, line))
{
lines.push_back(line);
}
while(std::getline(file2, line))
{
lines.push_back(line);
}
while(std::getline(file3, line))
{
lines.push_back(line);
}
}
This is my attempt
std::vector<std::string> lines;
std::string inputFile1 = "Builders.txt";
std::istringstream newStream(inputFile1);
std::string input;
void readFile(std::string const& newStream,std::vector<std::string>& lines)
{
std::ifstream file1(newStream);
std::string line;
while(std::getline(file1, line,":"))
{
lines.push_back(line);
}
When i run this code i recieve the error "no instance of overload function getline"
My question is given the text file how can i split the text file so that, for example, Reliable Rover is the name, 70 is the ability and 1 is the variability for the 1st record. Another example would be Sloppy Simon being the name, 20 being the ability and 4 being variaiblity. If the question is to vague or requires futher details please let me know
Thankyou
As #thomas-sablik mentioned, a simple solution is to read the file line by line and read each element from the line:
std::ifstream f("builder.txt");
std::string line;
// read each line
while (std::getline(f, line)) {
std::string token;
std::istringstream ss(line);
// then read each element by delimiter
while (std::getline(ss, token, ':'))
std::cout << token << std::endl;
}
don't forget to include sstream for using stringstreams.
Note: refer to cppreference, third parameter of std::getline is delim and is a character but you pass it as a string. So change:
while(std::getline(file1, line,":"))
to:
while(std::getline(file1, line,':'))
Here is a naive approach I came up with:
std::string name;
int ability;
int variability;
char read;
while (ifs >> read) { // read until the end of the file
// adding read into name
while (read != ':') {
name += read;
ifs >> read;
}
ifs >> ability;
ifs >> read; // Remove ':'
ifs >> variability;
ifs >> read; // Remove '.'
// Code to deal with the three variables
name = "";
}
Hope it helps.
I want to read one line of text from an istream and split it into words. My current code is
vector<string>((istream_iterator<string>(f)),
istream_iterator<string>());
where f is an ifstream object. However, this reads to the end of the file, and I want to stop it from reading when it reaches a newline. Is there any way to do something like istream_iterator<string>("\n") so the vector constructor will stop pushing back when it reaches a newline?
Use std::getline() to read a single line, and then use std::istringstream to read words from that line, eg:
std::string line;
std::getline(f, line);
std::istringstream iss(line);
std::vector<std::string> words(
std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>()
);
Use std::getline to read each line and push them to your result container
std::vector<std::string> lines;
for (std::string line; std::getline(f, line); lines.push_back(line));
Edit: Remy Lebeau already answered to OP's question, I want to clarify my code:
Normaly, to read lines in a file we would write something like
while (f.good())
{
std::string line;
std::getline(f, line);
lines.push_back(line);
}
Since std::istream can be converted to boolean, we can rewrite it as:
while (f)
{
std::string line;
std::getline(f, line);
lines.push_back(line);
}
And since std::getline return the input stream itself, we can use it as the loop condition:
std::string line;
while (std::getline(f, line))
{
lines.push_back(line);
} // `line` still can be used after this line, outside its scope
But we have to pull the temporary variable line outside of its scope. To avoid doing so we use for instead of while loop and declare line inside for loop:
for (std::string line; std::getline(f, line); )
{
lines.push_back(line);
}
Finally, since the body of the loop contains only 1 line, we can put it inside for loop too:
for (std::string line; std::getline(f, line); lines.push_back(line));
How do I read in lines from a file and assign specific segments of that line to the information in structs? And how can I stop at a blank line, then continue again until end of file is reached?
Background: I am building a program that will take an input file, read in information, and use double hashing for that information to be put in the correct index of the hashtable.
Suppose I have the struct:
struct Data
{
string city;
string state;
string zipCode;
};
But the lines in the file are in the following format:
20
85086,Phoenix,Arizona
56065,Minneapolis,Minnesota
85281
56065
Sorry but I still cannot seem to figure this out. I am having a really hard time reading in the file. The first line is basically the size of the hash table to be constructed. The next blank line should be ignored. Then the next two lines are information that should go into the struct and be hashed into the hash table. Then another blank line should be ignored. And finally, the last two lines are input that need to be matched to see if they exist in the hash table or not. So in this case, 85281 is not found. While 56065 is found.
As the other two answers point out you have to use std::getline, but this is how I would do it:
if (std::getline(is, zipcode, ',') &&
std::getline(is, city, ',') &&
std::getline(is, state))
{
d.zipCode = std::stoi(zipcode);
}
The only real change I made is that I encased the extractions within an if statement so you can check if these reads succeeded. Moreover, in order for this to be done easily (you wouldn't want to type the above out for every Data object), you can put this inside a function.
You can overload the >> operator for the Data class like so:
std::istream& operator>>(std::istream& is, Data& d)
{
std::string zipcode;
if (std::getline(is, zipcode, ',') &&
std::getline(is, d.city, ',') &&
std::getline(is, d.state))
{
d.zipCode = std::stoi(zipcode);
}
return is;
}
Now it becomes as simple as doing:
Data d;
if (std::cin >> d)
{
std::cout << "Yes! It worked!";
}
You can use a getline function from <string> like this:
string str; // This will store your tokens
ifstream file("data.txt");
while (getline(file, str, ',') // You can have a different delimiter
{
// Process your data
}
You can also use stringstream:
stringstream ss(line); // Line is from your input data file
while (ss >> str) // str is to store your token
{
// Process your data here
}
It's just a hint. Hope it helps you.
All you need is function std::getline
For example
std::string s;
std::getline( YourFileStream, s, ',' );
To convert a string to int you can use function std::stoi
Or you can read a whole line and then use std::istringstream to extract each data with the same function std::getline. For example
Data d = {};
std::string line;
std::getline( YourFileStream, line );
std::istringstream is( line );
std::string zipCode;
std::getline( is, zipCode, ',' );
d.zipCode = std::stoi( zipCode );
std::getline( is, d.city, ',' );
std::getline( is, d.state, ',' );
I have the following code, and I know how it works and what it does, however, not at all. I don't understand how these three lines work
std::stringstream lineStream(line);
std::string cell;
std::getline(lineStream, cell, ';')
Especially lineStream one;
I found them in google, but no sufficient explanation. Could you explain me please their behavior or share a good link please? Thanks in advance, have a nice day :)
container *begin = new container;
begin->beginBox = new box;
container *last = NULL;
std::ifstream data(filename);
std::string line;
std::getline(data, line);
for (container *i = begin; !data.eof() && std::getline(data, line);)
{
std::stringstream lineStream(line);
std::string cell;
std::getline(lineStream, cell, ';');
i->ID = atoi(cell.c_str());
for (box *j = i->beginBox; std::getline(lineStream, cell, ';'); j->next = new box, j = j->next)
{
j->apples = atoi(cell.c_str());
i->lastBox = j;
}
i->lastBox->next = NULL;
i->nextCont = new container(), last = i, i = i->nextCont, i->beginBox = new box;
}
setAutoIncrement(begin->ID + 1);
last->nextCont = NULL;
return begin;
std::stringstream lineStream(line);
This declares a variable called lineStream of type std::stringstream. It passes the line string to its constructor (2). The std::stringstream type wraps a string with a stream interface. It means you can treat it like cout and cin, using << and >> to insert and extract things from the string. Here, lineStream is being created so you can later extract its contents using std::getline.
std::string cell;
This just declares an empty std::string called cell.
std::getline(lineStream, cell, ';');
The function std::getline (1) takes a stream that it will extract a line from as its first argument. The second argument is a std::string that it will extract the line into. Without a third argument, the ending of a "line" is considered to be where we see a newline character. However, by passing a third argument, this code is making so that a line ends at ;. So this call to std::getline will extract everything from the stream up until it finds a ; character and puts that content into cell. The ; character is then discarded.
This is all very similar to the above code:
std::ifstream data(filename);
std::string line;
std::getline(data, line);
Here, the stream is a file stream instead of a string stream, and std::getline will extract everything up to a newline character because no third argument is given.
I'm trying to read a file word by word and do some implementation on each word. In future I want to know where was the position of each word. Position is line number and character position in that line. If character position is not available I only need to know when I'm reading a file when I go to the next line. This is the sample code I have now:
string tmp;
while(fin>>tmp){
mylist.push_back(tmp);
}
I need to know when fin is going to next line?!
"I need to know when fin is going to next line"
This is not possible with stream's operator >>. You can read the input line by line and process each line separately using temporary istringstream object:
std::string line, word;
while (std::getline(fin, line)) {
// skip empty lines:
if (line.empty()) continue;
std::istringstream lineStream(line);
for (int wordPos = 0; lineStream >> word; wordPos++) {
...
mylist.push_back(word);
}
}
just don't forget to #include <sstream>
One simple way to solve this problem would be using std::getline, run your own counter, and split line's content into words using an additional string stream, like this:
string line;
int line_number = 0;
for (;;) {
if (!getline(fin, line)) {
break;
}
istringstream iss(line);
string tmp;
while (iss >> tmp) {
mylist.push_back(tmp);
}
line_number++;
}