Here is a simple test:
QSettings *settings = new QSettings("test.ini", QSettings::IniFormat);
QStringList values;
values << "stringwith'quote"
<< "\"stringwithdoublequotes\""
<< "string"
<< "string with spaces"
<< "stringwith\nnewline"
<< "stringwith,comma"
<< "stringwith;semicolon"
<< ";"
<< ","
<< "'"
<< "',";
for (int i=0; i<values.count(); i++){
settings->setValue("value" + QString::number(i), values[i]);
}
This is what the .ini looks like:
[General]
value0=stringwith'quote
value1=\"stringwithdoublequotes\"
value2=string
value3=string with spaces
value4=stringwith\nnewline
value5="stringwith,comma"
value6="stringwith;semicolon"
value7=";"
value8=","
value9='
value10="',"
Is it possible to force all the strings to be saved wrapped in double quotes?
I think you can, but kinda bvy making it yourself. You can make a method that ensures your string are properly quoted
QString quoted(QString word)
{
return "\"" + word.replace('\\', "\\\\").replace('"',"\\\"").replace('\t', "\\t") + "\""
}
That method adds quotes, and escape some special characters.
Then you can call it on any string in your list. If you're going to use it often, you could inherit from QSetting to override method setValue and make it automatic.
Beware, the method QSettings::value is also going to need some adaptation to unquote those.
Related
Is it possible to edit a .mm file before it gets compiled in AppCenter?
In an attempt to fix a build error, I want to find and replace a string in ../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm.
I tried using sed -i 's/oldString/newString/g' ../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm inside appcenter-pre-build.sh but it does not work.
Any help will be appreciated,
Thanks.
Not sure if this is your case, but I needed to update a version number on a complex project. To replace the counter of the current version with a new one, I considered updating the file with each build. After some versions of bash scripts, I realized that it's easier for me to write a console application in C with a couple of parameters to solve this problem. Works almost perfect for me. If you need I can share the simple code of this program.
Here is the C code that looks for a string in the file passed as a parameter and replaces the version number in it.
int main(int argc, char* argv[])
{
cout << "Version changer start\n";
if (argc < 2) {
cout << "There is no path argument. Version was no changed.";
return 1;
}
string sourcePath = argv[1];
string targetPath = sourcePath + ".tmp";
bool firstLine = true;
cout << sourcePath << endl;
ifstream sourceFile(sourcePath); // open file for input
ofstream targetFile(targetPath); // open file for output
string line;
while (getline(sourceFile, line)) // for each line read from the file
{
// looking for the desired line
if (line.find("public static String VER") != std::string::npos) { // replace "public static String VER" to your string definition code
line.replace(0, 32, "");
line.replace(line.length() - 2, 2, "");
int number = atoi(line.c_str());
number++; // In my case, I get an integer and add one to it
string v = to_string(number);
line = " public static String VER = \"" + v + "\";";
cout << v;
}
if (firstLine) {
targetFile << line;
firstLine = false;
}
else
targetFile << endl << line;
}
sourceFile.close();
targetFile.close();
remove(sourcePath.c_str());
if (rename(targetPath.c_str(), sourcePath.c_str()) != 0)
perror("Error renaming file");
else
cout << endl << "----------- done -----------\n";
}
I have a vector of strings, and I fill the first string in it manually character by character
vector <std::string> vec(6);
vec[0][0] = 'h';
vec[0][1] = 'e';
vec[0][2] = 'y';
cout << "vec[0] = " << vec[0] << "\n"
now I want to print the vec[0] which is supposed to be a string "hey" , but it prints empty space.
I can print only if I print it character by character also, like this
for(int i = 0 ; i<1 ; i++)
{
for(int j = 0 ; j < 3 ; j++)
{
cout << vec[i][j];
}
cout << "\n";
}
Why I can't simply print the string as a whole.
vector <std::string> vec(6); gives you a vector of six empty strings. vec[0][0] = 'h'; is trying to assign the character h into the first slot of the first empty string, which is not legal, as the bracket operator can only replace existing characters. Use something like vec[0] += 'h' to append to the string.
Example
For example:
string MyString = "Normal\tString";
cout << MyString << endl;
produces the following: "Normal String"
Appending the raw string modifier to the string like so:
string MyString = R"(Normal\tString)";
cout << MyString << endl;
produces the following: "Normal\tString"
The Question
Is there a way to append the raw string modifier to a variable containing a string in order to print the raw form of the string contained within the variable?
string TestString = "Test\tString";
cout << R(TestString) << endl;
So you get: "Test\tString"
Is there a way to append the raw string modifier to a variable containing a string in order to print the raw form of the string contained within the variable?
No.
However, you can write a function that substitutes the characters that are defined by escape sequences by an appropriate string, i.e. replace the character '\t' by the string "\\t".
Sample program:
#include <iostream>
#include <string>
// Performs only one substitution of \t.
// Needs to be updated to do it for all occurrences of \t and
// all other escape sequences that can be found in raw strings.
std::string toRawString(std::string const& in)
{
std::string ret = in;
auto p = ret.find('\t');
if ( p != ret.npos )
{
ret.replace(p, 1, "\\t");
}
return ret;
}
int main()
{
std::string TestString = "Test\tString";
std::cout << toRawString(TestString) << std::endl;
}
Output:
Test\tString
This question is tagged as C++11, in which case rolling your own conversion function is probably the best call.
However, if you have a C++14 compiler, you can use the std::quoted stream manipulator:
#include <iostream>
#include <iomanip>
int main() {
string s = "Hello\tWorld!";
std::cout << std::quoted(s) << std::endl; // Prints "Hello\tWorld!"
}
Kind of. This works but it's not a pretty solution:
std::string strToInsert = " insert this "
std::string myRawString = R"(
raw string raw string raw string
raw string raw string raw string
)"; myRawString += strToInsert; myRawString += R"(raw string
raw string raw string raw string
)";
Here is my code. The list of characters that do not "work" and continue to say that they are palindromes if wrapped around the cin still say they are correct. The list of characters that don't work are:
single quotes, double quotes, commas, periods, forward slashes, back slashes, dashes, exclamation points, # symbols, # symbols, $ symbols, % symbols, ^ symbols, & symbols, * symbols (asterisk), equals symbols, + symbol
int main()
{
int k = 1;
int i;
int length, halflength;
int yesno = 1;
char string [81];
char end[81] = "END";
while (k = 1)
{
cout << "Please enter a string of characters. " << endl;
cout << "Enter \"END\" in all caps to exit the program." << endl;
cin.getline(string, 81);
if (strcmp(string, "END") == 0)
{
return 0;
}
length = strlen(string);
halflength = length / 2;
for (i = 0; i < halflength; i++)
{
if (string[i] != string[length - i - 1]) // comparing
yesno = 0;
break;
}
if (yesno) {
cout << "You have successfully entered a palindrome." << endl;
}
else
{
cout << "You have not entered a palindrome." << endl;
return main();
}
}
}
I am unsure how to fix this, as a palindrome can not only be a sequence of letters, but a sequence of characters. If there is an easier way to compare the lines, then I would appreciate the help, as I have spent some time being frustrated at this.
your program says,"You have successfully entered a palindrome." for mlaylam!
the problem is not having the break statement in the right place.
the block should be enclosed within braces, otherwise(as you've done), after checking the first character and last, the for loop will break thereby giving wrong result.
if (string[i] != string[length - i - 1]){ // comparing
yesno = 0;
break;
}
I have problems with inserting string variable inside text.
string p="http://www.google.com" ;
system("c:\\progra~1\\intern~1\\iexplore.exe \"http://www.google.com\"");
I need a way to use p instead of "http://www.google.com\"
I tried
system("c:\\progra~1\\intern~1\\iexplore.exe \%p\"");
but it doesn't work. I'm not very good with strings so probably that's the prob.
In C++ you can use the + operator to concatenate strings:
system((std::string("c:\\progra~1\\intern~1\\iexplore.exe ") + p).data());
In this case this is a bit hard to read, so you're better off creating the string before hand.
A better way would be to use stringstream:
#include <sstream>
std::stringstream sstr;
std::string p = "http://www.google.com";
sstr << "c:\\progra~1\\intern~1\\iexplore.exe " << p;
system(sstr.str().data());
Lets assume c#
string blammy = #"c:\progra~1\intern~1\iexplore.exe";
string finalAnswer;
string pikaPika = #"http://www.google.com";
finalAnswer = blammy + " " + pikaPika;
system(finalAnswer);