how to get specific data from a txt file in c++ [duplicate] - c++

This question already has answers here:
use getline and while loop to split a string
(3 answers)
Closed 1 year ago.
it's my frist publication and i have a question-
I have a .txt file that I want to separate by the ",".
Example:
example1,example2,example3,example4
I want to get example1 and store in an array and so on for example2 , example3, example4.
string[0] = example1
string[1] = example2
etc....

getline by default will split input by end-line chatracter, but can do for any character, including ',' - see example (using a stringstream):
std::string input = "word1,word2,word3";
std::stringstream sstream(input);
std::string word;
while(std::getline(sstream, word, ',')) {
std::cout << word << '\n';
}

Related

Replace text \n with actual new line. (C++) [duplicate]

This question already has answers here:
How to find and replace all occurrences of a substring in a string?
(9 answers)
Replace substring with another substring C++
(18 answers)
How to find and replace string?
(11 answers)
How do I replace all instances of a string with another string?
(6 answers)
Closed 6 months ago.
I'm using C++ and I have a problem. Instead of creating a new line it prints \n. My Code:
std::string text;
std::cout << text;
It prints:Hello\nWorld
It was supposed to read \n as a new line and print something like this:
"Hello
World"
So i've tried to use replace(text.begin(), text.end(), '\n', 'a') for testing purposes and nothing happened. It contiuned to print Hello\nWorld
std::replace() won't work in this situation. When called on a std::string, it can replace only single characters, but in your case your string actually contains 2 distinct characters '\' and 'n'. So you need to use std::string::find() and std::string::replace() instead, eg:
string::size_type index = 0;
while ((index = str.find("\\n", index)) != string::npos) {
str.replace(index, 2, "\n");
++index;
}

C++ - How can i split up 3 lines safed in string called "result" and create a oneline "synonym" for all 3 strings. - C++ [duplicate]

This question already has answers here:
Parse (split) a string in C++ using string delimiter (standard C++)
(33 answers)
Closed 2 years ago.
So, Hello Guys.
I have a function, which reads the Diskdrive Serialnumber.
I get a output like this:
SerialNumber A6PZD6FA 1938B00A49382 0000000000000
(thats not my serialnumber, just the format)
So now, i want to split the 3 numbers, or strings, however i call it you know what i mean, and save it in in three independent strings.
string a = {A6PZD6FA this value} string b = {1938B00A49382 this value} string c = {0000000000000 this value}
After that, i want to create a oneline "synonym" for all 3 strings. So i mean,
string synonym = 04930498SV893948AJVVVV34
something like this.
If you have the original text in a string variable, you can use a std::istringstream to parse it into constituent parts:
std::string s = "SerialNumber A6PZ etc etc...";
std::istringstream iss{s};
std::string ignore, a, b, c;
if (iss >> ignore >> a >> b >> c) {
std::string synonym = a + b + c;
...do whatever with synonym...
} else
std::cerr << "your string didn't contain four whitespace separated substrings\n";

Read white spaces in in a file C++ [duplicate]

This question already has answers here:
Reading from ifstream won't read whitespace
(10 answers)
Closed 4 years ago.
How I can read line of a file with a whitespace, in the case that faddress was a string with space I will not able to read all the line.
What I can do ? I try using noskipws but it doesn't work
http://www.cplusplus.com/reference/ios/noskipws/
Code:
ifstream rFile;
rFile.open("list_of_people.txt");
while(rFile >> fname >> flastName >> ocupation>> faddress >> age){
insert(fname,flastName,ocupation,faddress,age);
}
File :
John Smith Engineer Av Roses 25
You could use std::getline() to have the entire line in a string, then use string functions to parse it / tokenize it as you see fit. About doing that, see, for example:
Parse (split) a string in C++ using string delimiter (standard C++)

C++ Strings and delimiters [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 7 years ago.
So i'm doing a program where i input some data into a file and read from it. The problem is i dont know how to handle this. I read from the file and recieve a string containing alot of different data that is seperated by a delimiter "|".
string data ="FirstName|LastName|Signature|Height";
So my question is, is there a way to seperate this string nicely and store each of the values in a seperate variable?
p.s I have tried this so far. I did find this function subrt() and find() which i could use in order to find the delimiter and take out a value but it doesnt give the correct value for me so i think i'm doing something wrong with it. Only the fname value is correct.
const string DELIM = "|";
string fname = data.substr(0, data.find(DELIM));
string lname = data.substr(1, data.find(DELIM));
string signature = data.substr(2, data.find(DELIM));
string height = data.substr(3, data.find(DELIM));
You did not understand how substr() works. The first parameter is not the number of character found, it is the index at which to start. See the doc. You should do the same for the find function. Something like that:
string const DELIM = "|";
auto const firstDelim = data.find(DELIM);
auto const secondDelim = data.find(DELIM, firstDelim + 1); // begin after the first delim
// and so on
auto fname = data.substr(0, firstDelim);
auto lname = data.substr(firstDelim + 1, secondDelim);
// and so on

spliting in c++ like what we do in c# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to split a string in C++?
Splitting a C++ std::string using tokens, e.g. “;”
think I have this string :
string a = "hello,usa,one,good,bad";
I want to splitting this string with ,
so I need a array of string like here :
string *a ; a = { hello , usa , one , good , bad }
what shoudl I do ?
This simple AXE parser will do it:
std::vector<std::string> strings;
auto split = *(*(axe::r_any() - ',') >> e_push_back(strings));
split(a.begin(), a.end());
If you really don't want to code this on your own you can do a web search for "c++ tokenize string" and take, for example, a look here: CPPHOWTO