|
|
Home » Community » Newbie corner » From std::string to String
From std::string to String [message #49458] |
Wed, 14 February 2018 14:35  |
Giorgio
Messages: 218 Registered: August 2015
|
Experienced Member |
|
|
Hi there,
in my application I read a text file with an Ansi codepage (Windows1252) and put part of the text in a vector of std::string. Later, I use those std::string to perform a simple SQL query:
std::vector<std::string> fields;
Insert(My_Table)(My_Field1, (String)fields[0])
(My_Field2, (String)fields[1])
(My_Field3, (String)fields[2]);
I have to cast from std::string to String (that is UTF8, if I remember correctly): I have an error compiling if I use std::string directly. The problem is that with "strange" characters (e.g. Ö) the string is not converted properly and in the DB I get unknown characters(�). How can I properly convert from std::string to String?
Thanks,
Gio
|
|
|
Re: From std::string to String [message #49460 is a reply to message #49458] |
Wed, 14 February 2018 21:20   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello,
Try to create Upp::String basing on std::string like this:
IMO, you should use Core library to deal with text files. The easiest way is to use LoadFile(path):
Upp::String fileContent = Upp::LoadFile("PathToTextFile.txt");
// now you could use some powerfull functions from Core like Split;
Upp::Vector<String> fileContentLines = Upp::Split(fileContent, "\n");
for (const String& line : fileContentLines) {
// Loop over the line...
}
I will start from rewriting you code from standard library into Upp approch. This could save you problems with file encoding conversion.
Sincerely,
Klugier
U++ - one framework to rule them all.
[Updated on: Wed, 14 February 2018 21:27] Report message to a moderator
|
|
|
|
Re: From std::string to String [message #49464 is a reply to message #49463] |
Thu, 15 February 2018 10:10   |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
Quote:
Do you mean Upp can read data from an Ansi encoded file and convert it to utf-8 automatically?
Yes, for your case :
String filecontent = LoadFileBOM(filepath, CHARSET_WIN1252);
...
regards
omari.
|
|
|
|
|
Re: From std::string to String [message #49469 is a reply to message #49468] |
Thu, 15 February 2018 15:55   |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
1 - LoadFile is a function that open a stream and read its content and close it, returning the content.
std::ifstream is a class, the Upp similare is Upp::FileIn, that is used by LoadFile
2 -
Quote:
(MyApp.h)
Upp::Vector<Upp::String> SanitizeData(Upp::Vector<Upp::String> fields);
you have to pass "fields" by reference :
Upp::Vector<Upp::String> SanitizeData(Upp::Vector<Upp::String>& fields);
and if it is readonly, it is recommended to use "const"
Upp::Vector<Upp::String> SanitizeData(const Upp::Vector<Upp::String>& fields);
regards
omari.
|
|
|
Re: From std::string to String [message #49470 is a reply to message #49468] |
Thu, 15 February 2018 16:03   |
Oblivion
Messages: 1206 Registered: August 2007
|
Senior Contributor |
|
|
Hello Giorgio
[It seems Omari has already replied. ]
Quote:
I use LoadFile() and noticed that is has no .close() method or something like that (it replaces a ifstream in c++ standard library and with it I have to open and close the file): my understanding is that I have not to open/close the stream as with ifstream; is my understanding correct?
No, you don't. LoadFile() is a function, it does not have any method. It reads a file into a string, and closes the file/stream automatically.
If you need control over the stream, you can use FileIn, FileOut, FileAppend classes. (In fact, LoadFile() function uses an instance of FileIn)
They'll be closed automatically when they get out of their scope (when they are destroyed). In between you can explicity call Close() if you need to.
Previously, I used std::vector<std::string> and the application compiled, but now I get "error C2280: attempting to reference a deleted function".
Probably because of move/copy semantics. Something is destroyed in the process, before it can be referenced. chech if your data can be copied or moved.
As far as I can see, you are passing a copy of vector, just put an ampersand (&)
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Thu, 15 February 2018 16:06] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Sun May 11 15:37:30 CEST 2025
Total time taken to generate the page: 0.01242 seconds
|
|
|