-
So i have variable
std::vector<sf::String> variableName; // dont mind the name
And i want to write its contents to a file. here is function:
void writte2file(std::vector<sf::String> Text) // problem is here
{
ofstream saveIt;
saveIt.open ("Data.txt", ios::app);
saveIt << Text; // and problem here, will post it after code
saveIt << "\n";
saveIt.close();
}
Here is how i called function from for loop:
for (unsigned int i = 0; i < 30; ++i)
{
writte2file(variableName);
}
Error: no match for ‘operator<<’ in ‘saveIt << Text’ .
I think thats becouse standard c++ doesnt know of sf::String. Any idea how to solve this? And sfml addon for file system or anything?
Thank you in Advance
wmbuRn
-
Error: no match for ‘operator<<’ in ‘saveIt << Text’ .
I think thats becouse standard c++ doesnt know of sf::String. Any idea how to solve this? And sfml addon for file system or anything?
Looks more like a misuse of std::vector. This is a collection type and you would likely need to iterate it to get the sf::String objects and write those individually instead of trying to write the collection object.
something like this maybe (not exactly right, not in front of an IDE currently):
for(std::iterator *iter = Text.start(); iter != Text.end(); iter++) {
saveIt << *iter;
}
-
"Text " should rather be called "texts" or "strings", since there are multiple. Iteration then looks as follows:
for (std::vector<sf::String>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
C++11:
for (auto itr = strings.begin(); itr != strings.end(); ++itr)
for (sf::String& s : strings)
-
If you want to just use normal ascii then use .toAnsiString(); and save that and load it as std::string.
If you want real unicode support you can use this code to save and load them as UTF-8, it's most painless, locales might mess with it thought :-\ so don't use them.
What I (and many others) do and recommend doing to cut all the crap with locales, codepages and UTF other than UTF-8 is treat text as binary, use \n line endings and encode it in UTF-8 without BOM. This is arguably the best way.
sf::String sf32FromStd8(const std::string& str)
{
sf::String ret;
sf::Uint32 c;
auto it=str.begin();
while(it!=str.end())
{
it=sf::Utf8::decode(it,str.end(),c,0u);
if(c!=0u) ret+=c;
}
return ret;
}
std::string std8FromSf32(const sf::String& str)
{
std::string ret;
char buf[5];
for(auto it=str.begin();it!=str.end();++it)
{
auto end=sf::Utf32::encode(*it,buf);
*end='\0';
ret+=buf;
}
return ret;
}
-
Code that you posted is not working it didnt helped me. Anyway i never learned iterators, so i will have to learn it. Thank you for your answers guys. :)
-
It does work for me, what is the problem?
You know you have to include:
#include <SFML/System/String.hpp>
#include <SFML/System/Utf.hpp>
#include <string>
before it, because it uses these classes, right?
-
for(std::iterator *iter = Text.start(); iter != Text.end(); iter++) {
saveIt << *iter;
}
I get error: missing template arguments before ‘*’ token.
error no2. iter was not declared in this scope.
error no3. about Text.start() it should be Text.begin()
Never learned iterator so i have no idea what i am doing :)
for (std::vector<sf::String>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
Is working, but as i can see it does not save any value to .txt
this i add:
for (std::vector<sf::String>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
{
saveIt << *itr // or saIt << &itr or saveIt << itr i get error
}
error: no match for ‘operator<<’ in ‘saveIt << itr.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::String*, _Container = std::vector<sf::String>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = sf::String&]()’|
for c++11
for (auto itr = strings.begin(); itr != strings.end(); ++itr)
for (sf::String& s : strings)
I dont have c++11, since i have gcc 4.6.3. Gcc site stated that c++11 is partialy included in gcc4.7, and fully in 4.8.1.
FRex i havent tested your code, [yet] becouse i have no idea where to put it. Never did transfer from utf8 to anscii ... And i just looking how to write vector to .txt file noobish style :)
-
for (std::vector<sf::String>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
{
saveIt << *itr // or saIt << &itr or saveIt << itr i get error
}
error: no match for ‘operator<<’ in ‘saveIt << itr.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::String*, _Container = std::vector<sf::String>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = sf::String&]()’|
Change this to:
for (std::vector<sf::String>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
{
saveIt << itr.toAnsiString();
}
Or, pass "itr" to the functions that FRex put up.
-
for (std::vector<sf::String>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
{
saveIt << itr.toAnsiString();
}
error: ‘std::vector<sf::String>::iterator’ has no member named ‘toAnsiString’|
-
use -> not . on iterator types
-
use -> not . on iterator types
It compiles, but after clicking on button to save data program crashes with:
Segmentation fault (core dumped)
-
That's a complicated issue, you should debug your code, you might be deleting something twice, or accessing something that is already deleted or uninitialized.
-
Yes but you have to find out in some way(debugging with gdb, valgrind, putting printfs in code etc.) which line exactly crashes and why.
-
Yup. GDB told me where the bug is. I solved it but make new one. It does save now, it just saves same data about 300 times :) lol will fix it. Function to load data from txt into vector will be pain. :) Thank you for your help guys
Edit: Saving works as it should. Thank you guys