Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: writting std::vector<sf::String> to .txt file and load from it.  (Read 6295 times)

0 Members and 1 Guest are viewing this topic.

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
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


 

Atani

  • Newbie
  • *
  • Posts: 30
    • MSN Messenger - atanisoft@hotmail.com
    • AOL Instant Messenger - AtaniSYSOP
    • Yahoo Instant Messenger - atanisoft
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #1 on: July 23, 2013, 10:46:35 pm »
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;
}
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #2 on: July 23, 2013, 10:50:59 pm »
"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)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #3 on: July 23, 2013, 11:05:00 pm »
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;
        }
« Last Edit: July 23, 2013, 11:08:41 pm by FRex »
Back to C++ gamedev with SFML in May 2023

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #4 on: July 24, 2013, 02:17:50 am »
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. :)

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #5 on: July 24, 2013, 02:24:48 am »
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?
Back to C++ gamedev with SFML in May 2023

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #6 on: July 24, 2013, 02:56:14 am »
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
Code: [Select]
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 :)
« Last Edit: July 24, 2013, 03:01:59 am by wmbuRn »

Atani

  • Newbie
  • *
  • Posts: 30
    • MSN Messenger - atanisoft@hotmail.com
    • AOL Instant Messenger - AtaniSYSOP
    • Yahoo Instant Messenger - atanisoft
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #7 on: July 24, 2013, 03:01:25 am »
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.

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #8 on: July 24, 2013, 03:04:07 am »
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’|

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #9 on: July 24, 2013, 03:05:46 am »
use -> not . on iterator types
Back to C++ gamedev with SFML in May 2023

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #10 on: July 24, 2013, 03:13:45 am »
Quote
use -> not . on iterator types


It compiles, but after clicking on button to save data program crashes with:
Segmentation fault (core dumped)

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #11 on: July 24, 2013, 03:18:22 am »
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.
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #12 on: July 24, 2013, 03:32:39 am »
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.
Back to C++ gamedev with SFML in May 2023

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: writting std::vector<sf::String> to .txt file and load from it.
« Reply #13 on: July 24, 2013, 03:42:56 am »
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
« Last Edit: July 24, 2013, 03:47:40 am by wmbuRn »