Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » From std::string to String
From std::string to String [message #49458] Wed, 14 February 2018 14:35 Go to next message
Giorgio is currently offline  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 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1075
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello,

Try to create Upp::String basing on std::string like this:
String(fields[0]);


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 #49463 is a reply to message #49460] Thu, 15 February 2018 09:47 Go to previous messageGo to next message
Giorgio is currently offline  Giorgio
Messages: 218
Registered: August 2015
Experienced Member
Klugier wrote on Wed, 14 February 2018 21:20
Hello,

Try to create Upp::String basing on std::string like this:
String(fields[0]);



Actually, I tried that already: it compiles, but I still have gibberish in the db instead of the correct characters


Klugier wrote on Wed, 14 February 2018 21:20

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.



Do you mean Upp can read data from an Ansi encoded file and convert it to utf-8 automatically? Currently I use stream from standard c++ library to read data from file.
Re: From std::string to String [message #49464 is a reply to message #49463] Thu, 15 February 2018 10:10 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
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 #49466 is a reply to message #49464] Thu, 15 February 2018 11:40 Go to previous messageGo to next message
Giorgio is currently offline  Giorgio
Messages: 218
Registered: August 2015
Experienced Member
Ok, I found a quick & dirty solution: I convert formats using these functions, need to check if using Upp functions to load data solve the problem in a more elegant way.
Re: From std::string to String [message #49468 is a reply to message #49458] Thu, 15 February 2018 15:35 Go to previous messageGo to next message
Giorgio is currently offline  Giorgio
Messages: 218
Registered: August 2015
Experienced Member
I am in the process to use Core functionalities to rewrite my application, I have a couple of questions.

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?

There are a couple of functions that receive and return Upp::Vector<Upp::String> used as follows:

(MyApp.h)
Upp::Vector<Upp::String> SanitizeData(Upp::Vector<Upp::String> fields);

(MyApp.cpp)
Upp::Vector<Upp::String> fields;

(Here the Vector is filled in using data read from a file)
...

Upp::Vector<Upp::String> sanitized = SanitizeData(fields);



Previously, I used std::vector<std::string> and the application compiled, but now I get "error C2280: attempting to reference a deleted function".

Thanks,
Gio
Re: From std::string to String [message #49469 is a reply to message #49468] Thu, 15 February 2018 15:55 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
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 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Hello Giorgio

[It seems Omari has already replied. Smile ]

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


[Updated on: Thu, 15 February 2018 16:06]

Report message to a moderator

Re: From std::string to String [message #49472 is a reply to message #49458] Thu, 15 February 2018 17:10 Go to previous message
Giorgio is currently offline  Giorgio
Messages: 218
Registered: August 2015
Experienced Member
Thank you Oblivion and omari, your input was very helpful!
Previous Topic: Filtering CR from an EditString
Next Topic: MSSQL and codepage
Goto Forum:
  


Current Time: Fri Mar 29 09:56:13 CET 2024

Total time taken to generate the page: 0.01337 seconds