I have structure member which takes the value of delimiter from XML.And I want use that structure member as delimiter . But as getline() function take delimiter as char only so my I'm stuck over there.
istream& getline (istream& is, string& str, char delim)
I have done following thing in my code:-
string ABC = struct.delimiter; // Here struct.delimiter is takes value of
d delimiter from XML delimiter = '='
and I want use as
getline(string1,string2, ABC);
Is it possible?
or
Is there another way to do it?
You cannot achieve this with a getLine, the best way I know of to do this would be to get your line, store it in a string and use the find() function to find your delimiters.
string string; // Your line
string ABC = struct.delimiter; // Delimited
getline(input, string); // Get line from stream - Thx Kevin
string subStr = str.substr(0, str.find(ABC)); // this will get you the first segment
From here on you can develop your loop to go over the read data and find all segments.
If ABC is guaranteed to be a single character in length you can use ABC[0] to get the first character out of the string:
getline(string1, string2, ABC[0]);
Or just make ABC a char to make that guarantee even more apparent.
char ABC = struct.delimiter; // (assuming struct.delimiter is also a char,
// if not use srtuct.delimiter[0])
getline(string1, string2, ABC);
Related
I need a function that convert hexadecimal char pointer to string:
ex:
std::string Myfunction(char* hexacode)
{
std::string output;
//
//
return output;
}
std::string Result = Myfunction("\x55\x8B\xEC\x83\xEC\x14\x53\x56\x8B\x75\x0C");
In short I need to convert in string this parameter or similar.
Because in the output the backslash is a option can be a solution replace backslash with slash if is too complicate keep the backslash.
Many thanks !!
If you want the user to input the string, you don't have to care about anything, string escaping only happens for constant strings in the code file.
If you don't want the constant strings in the code file to be escaped, no function conversion needed, just do this:
"\\x55\\x8B\\xEC\\x83\\xEC\\x14\\x53\\x56\\x8B\\x75\\x0C"
consider a string like string s = "xyz123".
for(char ch : s)
cout<<ch;
and stringstream like
char ch;
stringstream ss(s);
while(ss>>ch)
cout<<ch;
They both give the same solution. Is there any case where the two behave differently?
When should each be used.
The second one will skip any whitespace in the string. That's how >> works.
Unless skipping whitespace is actually a requirement the second version is also unnecessary. Why construct a new object just to iterate through a string when there are methods built into string for iteration.
I'm wanting to save the content of a file to a struct. I've tried to use seekg and read to write to it but it isn't working.
My file is something like:
johnmayer24ericclapton32
I want to store the name, the last name and the age in a struct like that
typedef struct test_struct{
string name;
string last_name;
int age;
} test_struct;
Here is my code
int main(){
test_struct ts;
ifstream data_base;
data_base.open("test_file.txt");
data_base.seekg(0, ios_base::beg);
data_base.read(ts, sizeof(test_struct));
data_base.close();
return 0;
}
It doesn't compile as it don't want me to use ts on the read function. Is there another way - or a way - of doing it?
Serialization/Deserialization of strings is tricky.
As binary data the convention is to output the length of the string first, then the string data.
https://isocpp.org/wiki/faq/serialization#serialize-binary-format
String data is tricky because you have to unambiguously know when the string’s body stops. You can’t unambiguously terminate all strings with a '\0' if some string might contain that character; recall that std::string can store '\0'. The easiest solution is to write the integer length just before the string data. Make sure the integer length is written in “network format” to avoid sizeof and endian problems (see the solutions in earlier bullets).
That way when reading the data back in you know the length of the string to expect and can preallocate the size of the string then just read that much data from the stream.
If your data is a non-binary (text) format it's a little trickier:
https://isocpp.org/wiki/faq/serialization#serialize-text-format
String data is tricky because you have to unambiguously know when the string’s body stops. You can’t unambiguously terminate all strings with a '\n' or '"' or even '\0' if some string might contain those characters. You might want to use C++ source-code escape-sequences, e.g., writing '\' followed by 'n' when you see a newline, etc. After this transformation, you can either make strings go until end-of-line (meaning they are deliminated by '\n') or you can delimit them with '"'.
If you use C++-like escape-sequences for your string data, be sure to always use the same number of hex digits after '\x' and '\u'. I typically use 2 and 4 digits respectively. Reason: if you write a smaller number of hex digits, e.g., if you simply use stream << "\x" << hex << unsigned(theChar), you’ll get errors when the next character in the string happens to be a hex digit. E.g., if the string contains '\xF' followed by 'A', you should write "\x0FA", not "\xFA".
If you don’t use some sort of escape sequence for characters like '\n', be careful that the operating system doesn’t mess up your string data. In particular, if you open a std::fstream without std::ios::binary, some operating systems translate end-of-line characters.
Another approach for string data is to prefix the string’s data with an integer length, e.g., to write "now is the time" as 15:now is the time. Note that this can make it hard for people to read/write the file, since the value just after that might not have a visible separator, but you still might find it useful.
Text-based serialization/deserialization convention varies but one field per line is an accepted practice.
You'll have to develop a specific algorithm, since there is no separator character between the "fields".
static const std::string input_text = "johnmayer24ericclapton32";
static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
static const std::string decimal_digit = "0123456789";
std::string::size_type position = 0;
std::string artist_name;
position = input_text.find_first_not_of(alphabet);
if (position != std::string::npos)
{
artist_name = input_text.substr(0, position - 1);
}
else
{
cerr << "Artist name not found.";
return EXIT_FAILURE;
}
Similarly, you can extract out the number, then use std::stoi to convert the numeric string to internal representation number.
Edit 1: Splitting the name
Since there is no separator character between the first and last name, you may want to have a list of possible first names and use that to find out where the first name ends and the surname starts.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char addi[5];
string name;
string a="ADD:";
fgets(addi,5, stdin);
cin>>name;
addi[5]='\0';
cout<<"addi"<<addi<<endl;
i have a addi char array in which i wanna store "ADD:" and name string in which i wanna store the string to be added .the input will be in format ADD:wolf .why does addi not take the "ADD:" string?? OR alternatively how would i do it since i need to compare the ADD: with some string in further steps.
My guess is that you expect the fgets call to get the first four characters, and then use std::cin to get the remaining line. However it does not work like that. The fgets call gets the complete line, even it it only writes four characters to your buffer. Then the input with std::cin will wait for input that never comes.
Instead I suggest you read the complete line with std::getline, and then split the string at the colon to get the "key" and the "value".
Actually, since std::getline supports simple tokenization, you can use two calls to read the input:
std::string op;
std::string data;
std::getline(std::cin, op, ':');
std::getline(std::cin, data);
Now the string op will contain e.g. ADD (the string before the colon), and data will contain the string after the colon.
currently I am doing to read input with spaces in it.
int main() {
char str[100];
string st;
cin.getline(str,100);
st=str;
}
I want to utilize the functions that come along with the string, so I am reading the input into a string. Is there any other way to read the input directly into the string which also allow space.
If you're going to use std::string objects, just use std::getline.
std::string st;
std::getline(std::cin, st);
using gets () function.
It accepts even space as input.
Eg. )
gets (variable_name);