how to use fout () - c++

Can some help me i have create this command
fout <<"osql -Ubatascan -Pdtsbsd12345 -dpos -i""c:\\temp_pd.sql"""<<endl;
Result Output
osql -Ubatascan -Pdtsbsd12345 -dpos -ic:\temp_pd.sql
Output that i want
osql -Ubatascan -Pdtsbsd12345 -dpos -i"c:\temp_pd.sql"
can some one help?

What you're doing is actually writing multiple string literals next to each other. The expression
"foo""bar"
gets parsed as the two string literals "foo" and "bar". The C and C++ languages say that when you have string literals next to each other, they get pasted together into one big string literal at compile time. So, the above expression is entirely equivalent to the single string literal "foobar".
Hence, your expression gets parsed as the following three string literals:
"osql -Udatascan -Pdtsbsd7188228 -dpos -i"
"c:\\temp_pd.sql"
""
Which when pasted together form the string "osql -Udatascan -Pdtsbsd7188228 -dpos -ic:\\temp_pd.sql" (note that the third string is the empty string""`).
What you want to do is to use the escape sequence \" to include a literal quotation mark within your string literal. Write it like this:
"osql -Udatascan -Pdtsbsd7188228 -dpos -i\"c:\\temp_pd.sql\""
Normally, the quotation mark " gets interpreted as the end of a string literal, except when it's preceded by a backslash, in which case it gets interpreted as a quotation mark character within the string.

Related

python .replace not working properly

My code takes a list of strings from a static website.
It then traverses through each character in the list and uses the .replace method to replace any non utf-8 character:
foo.replace('\\u2019', "'")
It doesn't replace the character in the list correctly and ends up looking like the following:
before
u'What\u2019s with the adverts?'
after
u'What\u2019s with the adverts?'
Why is it
Python 2.7 interprets string literals as ASCII, not unicode, and so even though you've tried to include unicode characters in your argument to foo.replace, replace is just seeing ASCII {'\', 'u', '2', '0', '1', '9'}. This is because Python doesn't assign a special meaning to "\u" unless it is parsing a unicode literal.
To tell Python 2.7 that this is a unicode string, you have to prefix the string with a u, as in foo.replace(u'\u2017', "'").
Additionally, in order to indicate the start of a unicode code, you need \u, not \\u - the latter indicates that you want an actual '\' in the string followed by a 'u'.
Finally, note that foo will not change as a result of calling replace. Instead, replace will return a value which you must assign to a new variable, like this:
bar = foo.replace(u'\u2017', "'")
print bar
(see stackoverflow.com/q/26943256/4909087)
yeah. If your string is foo = r'What\u2019s with the adverts?' will ok with foo.replace('\\u2019', "'"). It is a raw string and begins with r''. And with u'' is Unicode.
Hope to help you.

How to strip a certain string from each the list items

I have list of file paths inside C:\ stored in a list called filepaths, now i have to strip off C:\ from all the filepaths while doing a for loop.
I'm not able to find a strip method while looping, as each element is coming as Type list. Please find my code below.
filepaths = ['C:\folder\file1.jpg','C:\file2.png','C:\file3.xls']
tobestriped = 'C:\'
for filepath in filepaths:
newfilepath = filepath.strip(tobestriped)
print(newfilepath)
Well, to begin with, in tobestriped you'll get an error as \' will be escaped. You can use tobestriped = 'C:\\'.
From this SO answer:
A "raw string literal" is a slightly different syntax for a string
literal, in which a backslash, \, is taken as meaning "just a
backslash" (except when it comes right before a quote that would
otherwise terminate the literal) -- no "escape sequences" to represent
newlines, tabs, backspaces, form-feeds, and so on. In normal string
literals, each backslash must be doubled up to avoid being taken as
the start of an escape sequence.
Next, In your paths list \f will also be escaped. To get rid of this issue, make those strings raw strings:
filepaths = [r'C:\folder\file1.jpg', r'C:\file2.png', r'C:\file3.xls']
An you will have the desired result:
filepaths = [r'C:\folder\file1.jpg', r'C:\file2.png', r'C:\file3.xls']
tobestriped = 'C:\\'
for filepath in filepaths:
newfilepath = filepath.strip(tobestriped)
print(newfilepath)
Output:
folder\file1.jpg
file2.png
file3.xls
An alternative to your solution would be to take advantage of the fact that all your strings begin with C:\ so you can do something like this:
print([x[3:] for x in filepaths])

Parsing Quoted Strings Having Nested Escape Sequences in jsoncpp

I'm using the jsoncpp library here. I am confused by the parsing of single quotation marks (') and double quotation marks (").
Json::Value root;
Json::Reader reader;
const std::string json_str1 = "{\"name\":\"Say \\\"Hello\\\"!\"}";
const std::string json_str2 = "{\"name\":\"Say \"Hello\"!\"}";
const std::string json_str3 = "{\"name\":\"Say \\\'hi\\\'!\"}";
const std::string json_str4 = "{\"name\":\"Say \'hi\'!\"}";
const std::string json_str5 = "{\"name\":\"Say 'hi'!\"}";
reader.parse(json_str1, root, false); // success
reader.parse(json_str2, root, false); // fail
reader.parse(json_str3, root, false); // fail
reader.parse(json_str4, root, false); // success
reader.parse(json_str5, root, false); // success
Why must double quotations be like \\\" but single quotations must be \' or just ', but can't be \\\'?
Escaping Delimiters
The reason for escaping quotation marks with \ is to allow the parser(s) to distinguish between a quotation mark that is intended to be a character within the quoted string, and a delimiting quotation mark that is intended to close the string.
As you know, in the C++ language, double-quotes " are used to delimit character strings. But if you want to create a string that contains a double quotation mark ", the \ is used as an escape so the C++ parser knows to interpret the following character as a character, not as the closing delimiter:
const std::string double_quote = """; // WRONG!
const std::string double_quote = "\""; // good
With Two Parsers
In your code, there are two parsers that are involved: the C++ parser that is part of the C++ compiler that will be compiling this code, and the JSON parser that is part of the jsoncpp library. The C++ parser interprets this code at compile time, while the jsoncpp parser interprets the strings at run time.
Like C++, JSON also uses double quotes " to delimit strings. A simple JSON document as seen by the jsoncpp parser looks something like:
{"name":"Xiaoying"}
To enclose this JSON document into a C++ string, the double quotation marks " within the JSON document need to be escaped with \ as follows:
const std::string json_name = "{\"name\":\"Xiaoying\"}"; // good
This tells C++ to create a string having the contents {"name":"Xiaoying"}.
Nested delimiters
Things start to get complicated when the JSON document itself contains delimiters that must also be escaped. Like C++, JSON also uses the backslash \ as an escape. Now the question becomes, how to distinguish a backslash \ intended as an escape for the jsoncpp parser from a backslash \ intended as an escape for the C++ parser? The way to do this is to use a double backslash \\ sequence, which is translated by the C++ parser into a single backslash '\' character within the string. That single backslash, when passed to the jsoncpp parser at runtime, will at that time be interpreted as an escape character.
Things are further complicated by the fact that the rules for use of the backslash in JSON are different than the rules for C++. In particular, in C++ single quotes ' may be escaped with a backslash (as in \'), but this is not a legal pattern in JSON.
Here is an explanation for each of the five cases you presented:
1. json_str1
The C++ statement
const std::string json_str1 = "{\"name\":\"Say \\\"Hello\\\"!\"}";
produces a JSON document that looks like
{"name":"Say \"Hello\"!"}
When the jsoncpp parser sees this, it will know by the backslashes that "Say \"Hello\"!" means this is a string containing Say "Hello"!
2. json_str2
The C++ statement
const std::string json_str2 = "{\"name\":\"Say \"Hello\"!\"}";
produces a JSON document that looks like
{"name":"Say "Hello"!"}
Since the quotation marks around "Hello" are not escaped, the jsoncpp parser will fail.
3. json_str3
The C++ statement
const std::string json_str3 = "{\"name\":\"Say \\\'hi\\\'!\"}";
produces a JSON document that looks like
{"name":"Say \'hi\'!"}
Since the \' pattern is not recognized in JSON, this will fail in the jsoncpp parser.
4. json_str4
The C++ statement
const std::string json_str4 = "{\"name\":\"Say \'hi\'!\"}";
produces a JSON document that looks like
{"name":"Say 'hi'!"}
This is because the C++ parser interpreted the \' sequence as a single ' character.
5. json_str5
The C++ statement
const std::string json_str5 = "{\"name\":\"Say 'hi'!\"}";
produces a JSON document that looks like
{"name":"Say 'hi'!"}
See also
For the C++ escape sequence rules: http://en.cppreference.com/w/cpp/language/escape
For the JSON escape sequence rules: http://www.json.org/

Parsing as string of data but leaving out quotes

I need to use RegEx to run through a string of text but only return that parts that I need. Let's say for example the string is as follows:
1234,Weapon Types,100,Handgun,"This is the text, "and", that is all."""
\d*,Weapon Types,(\d*),(\w+), gets me most of the way, however it is the last part that I am having an issue with. Is there a way for me to capture the rest of the string i.e.
"This is the text, "and", that is all."""
without picking up the quotes? I've tried negating them, however it just stops the string at the quote.
Please keep in mind that the text for this string is unknown so doing literal matches will not work.
You've given us something very difficult to solve. It's okay that you have nested commas inside your string. Once we come across a double-quote, we can ignore everything until the end quote. This would gooble up commas.
But how will your parser know that the next double-quote isn't ending the string. How does it know that it a nested double-quote?
If I could slightly modify your input string to make it clear what is a nested quote, then parsing is easy...
var txt = "1234,Weapon Types,100,Handgun,\"This is the text, "and", that is all.\",other stuff";
var m = Regex.Match(txt, #"^\d*,Weapon Types,(\d*),(\w+),""([^""]+)""");
MessageBox.Show(m.Groups[3].Value);
But if your input string must have nested quotes like that, then we must come up with some other rule for detecting what is the real end of the string. How about this?
var txt = "1234,Weapon Types,100,Handgun,\"This is the text, \"and\", that is all.\",other stuff";
var m = Regex.Match(txt, #"^\d*,Weapon Types,(\d*),(\w+),""(.+)"",");
MessageBox.Show(m.Groups[3].Value);
The result is...
This is the text, "and", that is all.

How to make ListFind() to consider a string having a comma in it as a complete single string

I got a problem in using listfind().
I have a list of strings.One of my string has a comma.
Now when I use listfind() to compare with another string I don't get the output expected. i.e The string with a comma in it is not detected.
How can the listfind() to work even for the string having comma in it ?
The default delimiter for all CF functions is the comma. In order to use strings with commas you must change the delimiter.
For example:
ListAppend(list, "A value, with comma", "^")
ListFind(list, "A value, with comma", "^")