Haker123 Temat założony przez niniejszego użytkownika |
» 2014-08-02 23:27:11 Juz nie wiem co jest nietak Odczytuje string i unsigned tak Top10_Nick[ i ].resize( Max ); File.read( reinterpret_cast < char *>( & Top10_Nick[ i ] ), Top10_Nick[ i ].size() );
File.read( reinterpret_cast < char *>( & Top10_Points[ i ] ), sizeof( unsigned ) );
A zapisuje je tak File.write( Top10_Nick[ i ].c_str(), Top10_Nick[ i ].size() + 1 ); File.write( reinterpret_cast < const char *>( & Top10_Points[ i ] ), sizeof( unsigned ) );
Takie dane mam w zmiennych po odczycie (wypis z konsoli bo string wcale sie nie pokazuje) Maps->Top10_Points[0] 707406378 Maps->Top10_Nick[0] ☺ ☺ ☺ **** Maps->Top10_Points[1] 707406336 Maps->Top10_Nick[1] ☺ ☺ ☺**** Maps->Top10_Points[2] 707406378 Maps->Top10_Nick[2] ☺ ☺ **** Maps->Top10_Points[3] 707406378 Maps->Top10_Nick[3] ☺ ☺ ☺ **** Maps->Top10_Points[4] 707406378 Maps->Top10_Nick[4] ☺ ☺ ☺ **** Maps->Top10_Points[5] 707406336 Maps->Top10_Nick[5] ☺ ☺ ☺**** Maps->Top10_Points[6] 707406378 Maps->Top10_Nick[6] ☺ ☺ **** Maps->Top10_Points[7] 707406378 Maps->Top10_Nick[7] ☺ ☺ ☺ **** Maps->Top10_Points[8] 707406378 Maps->Top10_Nick[8] ☺ ☺ ☺ **** Maps->Top10_Points[9] 707406336 Maps->Top10_Nick[9] ☺ ☺ ☺****
Wszystkie punkty powinny miec wartosc 0 a string miec 15x * . Dalej robie cos zle przy zapisie i odczycie?? Przepraszam ze tak mecze ale zawsze mialem problem z plikami binarnymi a na textowych to daleko nie zajade ;/ |
|
pekfos |
» 2014-08-03 00:46:12 Odczytuj tyle, ile zapisujesz. Raz dodajesz 1 do size(), a raz nie. Zdecyduj się też, czy zapisujesz blok Max znaków, czy c-string o maksymalnie Max niezerowych znaków. |
|
Haker123 Temat założony przez niniejszego użytkownika |
» 2014-08-03 13:42:32 Obojetne czy mam zarezerwowane miejsce stringa na wielkosc Max ( Top10_Nick .resize( Max )) czy nie oraz jak odczytuje z wielkosc +1 lub bez to caly czas mam taki sam efekt. Zaczynam myslec ze to nie tu jest blad bo co nie wpisze to i tak jest zle.
Obecnie tak wyglada kod Zapis
bool Map::Save_Map( ALLEGRO_DISPLAY * display ) { std::ofstream File( File_Name.c_str(), std::ios::out | std::ios::binary ); if( !File ) { al_show_native_message_box( display, "MAP ERROR", "ERROR", "You can not save the map", NULL, ALLEGRO_MESSAGEBOX_ERROR ); return false; } int What[ 400 ]; for( int y = 0; y < 400; y++ ) { if( Screen_Map[ y ] == Elements[ 15 ] ) { What[ y ] = 1; } else if( Screen_Map[ y ] == Elements[ 16 ] ) { What[ y ] = 2; } else if(( Screen_Map[ y ] == Elements[ 0 ] ) ||( Screen_Map[ y ] == Elements[ 1 ] ) || ( Screen_Map[ y ] == Elements[ 2 ] ) ||( Screen_Map[ y ] == Elements[ 3 ] ) ) { What[ y ] = 3; } else if(( Screen_Map[ y ] == Elements[ 8 ] ) ||( Screen_Map[ y ] == Elements[ 9 ] ) || ( Screen_Map[ y ] == Elements[ 10 ] ) ||( Screen_Map[ y ] == Elements[ 11 ] ) ) { What[ y ] = 4; } } File.write( reinterpret_cast < const char *>( & What ), sizeof( What ) ); File.write( Code_Nick.c_str(), Code_Nick.size() + 1 ); for( int i = 0; i < 10; i++ ) { Top10_Nick[ i ].resize( Max ); File.write( Top10_Nick[ i ].c_str(), Top10_Nick[ i ].size() + 1 ); } al_rest( 5 ); File.write( Code_Point.c_str(), Code_Point.size() + 1 ); for( int i = 0; i < 10; i++ ) File.write( reinterpret_cast < const char *>( & Top10_Points[ i ] ), sizeof( unsigned ) ); File.write( Check_File.c_str(), Check_File.size() + 1 ); File.close(); return true; }
Odczyt
bool Map::Load_Map( ALLEGRO_DISPLAY * display, std::string Name ) { File_Name = "Maps\\" + Name; std::fstream File; File.open( File_Name.c_str(), std::ios::in | std::ios::binary ); if( !File ) { al_show_native_message_box( display, "MAP ERROR", "ERROR", "You can not open the map, possible that the map does not exist", NULL, ALLEGRO_MESSAGEBOX_ERROR ); return false; } int What[ 400 ]; File.read( reinterpret_cast < char *>( & What ), sizeof( What ) ); for( int y = 0; y < 400; y++ ) { if( What[ y ] == 1 ) Screen_Map[ y ] = Elements[ 15 ]; else if( What[ y ] == 2 ) Screen_Map[ y ] = Elements[ 16 ]; else if( What[ y ] == 3 ) { std::cout << "Y W ODCZYCIE MAPY = " << y << std::endl; Snakess->Snake[ 0 ][ 0 ] = y % 20; Snakess->Snake[ 0 ][ 1 ] = y / 20; Screen_Map[ y ] = Elements[ 0 ]; } else if( What[ y ] == 4 ) { Snakess->Snake[ 1 ][ 0 ] = y % 20; Snakess->Snake[ 1 ][ 1 ] = y / 20; Screen_Map[ y ] = Elements[ 8 ]; } } int place = Return_Place_of_String( File, Code_Nick ); if( place == - 1 ) { al_show_native_message_box( display, "ERROR", "ERROR FILE", "FILE DONT HAVE TOP10 NICK", NULL, ALLEGRO_MESSAGEBOX_ERROR ); return false; } File.seekg( place ); for( int i = 0; i < 10; i++ ) { Top10_Nick[ i ].resize( Max ); File.read( reinterpret_cast < char *>( & Top10_Nick[ i ] ), Top10_Nick[ i ].size() + 1 ); } al_rest( 5 ); place = Return_Place_of_String( File, Code_Point ); if( place == - 1 ) { al_show_native_message_box( display, "ERROR", "ERROR FILE", "FILE DONT HAVE TOP10 POINTS", NULL, ALLEGRO_MESSAGEBOX_ERROR ); return false; } File.seekg( place ); for( int i = 0; i < 10; i++ ) File.read( reinterpret_cast < char *>( & Top10_Points[ i ] ), sizeof( unsigned ) ); File.close(); for( int y = 0; y < 20; y++ ) { for( int x = 0; x < 20; x++ ) { if( Screen_Map[( y * 20 ) + x ] == Elements[ 0 ] ) { if( x == 0 && y == 0 ) { std::cout << " x == 0 && y == 0 " << std::endl; if( Screen_Map[( y + 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y + 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 1 ]; Screen_Map[( y + 1 ) * 20 + x ] = Elements[ 9 ]; dir = UP; } else if( Screen_Map[ y * 20 + x + 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x + 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 0 ]; Screen_Map[ y * 20 + x + 1 ] = Elements[ 8 ]; dir = LEFT; } } else if( x == 19 && y == 19 ) { std::cout << " x == 19 && y == 19 " << std::endl; if( Screen_Map[( y - 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y - 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 3 ]; Screen_Map[( y - 1 ) * 20 + x ] = Elements[ 11 ]; dir = DOWN; } else if( Screen_Map[ y * 20 + x - 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x - 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 2 ]; Screen_Map[ y * 20 + x - 1 ] = Elements[ 10 ]; dir = RIGHT; } } else if( x == 19 && y == 0 ) { std::cout << " x == 19 && y == 0 " << std::endl; if( Screen_Map[( y + 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y + 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 1 ]; Screen_Map[( y + 1 ) * 20 + x ] = Elements[ 9 ]; dir = UP; } else if( Screen_Map[ y * 20 + x - 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x - 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 2 ]; Screen_Map[ y * 20 + x - 1 ] = Elements[ 10 ]; dir = RIGHT; } } else if( x == 0 && y == 19 ) { std::cout << " x == 0 && y == 19 " << std::endl; if( Screen_Map[( y - 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y - 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 3 ]; Screen_Map[( y - 1 ) * 20 + x ] = Elements[ 11 ]; dir = DOWN; } else if( Screen_Map[ y * 20 + x + 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x + 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 0 ]; Screen_Map[ y * 20 + x + 1 ] = Elements[ 8 ]; dir = LEFT; } } else if( y == 0 ) { std::cout << " y == 0 " << std::endl; if( Screen_Map[( y + 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y + 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 1 ]; Screen_Map[( y + 1 ) * 20 + x ] = Elements[ 9 ]; dir = UP; } else if( Screen_Map[ y * 20 + x - 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x - 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 2 ]; Screen_Map[ y * 20 + x - 1 ] = Elements[ 10 ]; dir = RIGHT; } else if( Screen_Map[ y * 20 + x + 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x + 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 0 ]; Screen_Map[ y * 20 + x + 1 ] = Elements[ 8 ]; dir = LEFT; } } else if( y == 19 ) { std::cout << " y == 19 " << std::endl; if( Screen_Map[( y - 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y - 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 3 ]; Screen_Map[( y - 1 ) * 20 + x ] = Elements[ 11 ]; dir = DOWN; } else if( Screen_Map[ y * 20 + x - 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x - 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 2 ]; Screen_Map[ y * 20 + x - 1 ] = Elements[ 10 ]; dir = RIGHT; } else if( Screen_Map[ y * 20 + x + 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x + 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 0 ]; Screen_Map[ y * 20 + x + 1 ] = Elements[ 8 ]; dir = LEFT; } } else if( x == 0 ) { std::cout << " X == 0 " << std::endl; if( Screen_Map[( y + 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y + 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 1 ]; Screen_Map[( y + 1 ) * 20 + x ] = Elements[ 9 ]; dir = UP; } else if( Screen_Map[( y - 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y - 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 3 ]; Screen_Map[( y - 1 ) * 20 + x ] = Elements[ 11 ]; dir = RIGHT; } else if( Screen_Map[ y * 20 + x + 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x + 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 0 ]; Screen_Map[ y * 20 + x + 1 ] = Elements[ 8 ]; dir = LEFT; } } else if( x == 19 ) { std::cout << " X == 19 " << std::endl; if( Screen_Map[( y + 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y + 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 1 ]; Screen_Map[( y + 1 ) * 20 + x ] = Elements[ 9 ]; dir = UP; } else if( Screen_Map[( y - 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y - 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 3 ]; Screen_Map[( y - 1 ) * 20 + x ] = Elements[ 11 ]; dir = DOWN; } else if( Screen_Map[ y * 20 + x - 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x - 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 2 ]; Screen_Map[ y * 20 + x - 1 ] = Elements[ 10 ]; dir = RIGHT; } } else { std::cout << "ELSE" << std::endl; if( Screen_Map[( y + 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y + 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 1 ]; Screen_Map[( y + 1 ) * 20 + x ] = Elements[ 9 ]; dir = UP; } else if( Screen_Map[( y - 1 ) * 20 + x ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ (y - 1) * 20 + x ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 3 ]; Screen_Map[( y - 1 ) * 20 + x ] = Elements[ 11 ]; dir = DOWN; } else if( Screen_Map[ y * 20 + x - 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x - 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 2 ]; Screen_Map[ y * 20 + x - 1 ] = Elements[ 10 ]; dir = RIGHT; } else if( Screen_Map[ y * 20 + x + 1 ] == Elements[ 8 ] ) { std::cout << " W Screen_Map[ y * 20 + x + 1 ] == Elements[8]" << std::endl; Screen_Map[ y * 20 + x ] = Elements[ 0 ]; Screen_Map[ y * 20 + x + 1 ] = Elements[ 8 ]; dir = LEFT; } } } } } std::cout << " ZZZZZZZZZZZZZZZZAAAAAAAAAAAAAAAAA " << std::endl; return true; }
|
|
pekfos |
» 2014-08-03 13:46:51 File.read( reinterpret_cast < char *>( & Top10_Nick[ i ] ), Top10_Nick[ i ].size() + 1 );
|
Nie możesz tak zapisać/odczytać. Znowu. |
|
|
Haker123 Temat założony przez niniejszego użytkownika |
» 2014-08-03 13:53:55 Skasowalem to +1 z odczytu i dalej nie dziala. |
|
pekfos |
» 2014-08-03 13:57:25 Nie możesz tak zapisać/odczytać obiektu typu std::string.. Użyj std::getline() z odpowiednimi argumentami, lub odczytuj przez read() do tablicy znaków i potem skopiuj ją do obiektu std::string - zależy od tego, co konkretnie chcesz osiągnąć. |
|
Haker123 Temat założony przez niniejszego użytkownika |
» 2014-08-03 14:31:43 Sprawdzilem z ciekawosci co funkcja int Return_Place_of_String( std::fstream &File , std::string Searches ) zwraca, i okazuje sie ze czy szukaniu Code_Nick zwraca 11 a przy Code_Point zwraca 1622 i w te miejsca ustawiam odczyt danych. Na poczatku mam 400 elementow int potem Code_Nick i niki graczy, to jest o wiele wiecej niz 11. Co ja tutaj robie zle?? Funkcja odszukuje string w pliku i pobiera jego pozycje. int Return_Place_of_String( std::fstream & File, std::string Searches ) { std::string a(( std::istreambuf_iterator < char >( File ) ), std::istreambuf_iterator < char >() ); int i = a.find( Searches ); if( i == - 1 ) return - 1; else return i + Searches.length() + 1; }
|
|
Monika90 |
» 2014-08-03 14:53:46 A wiesz, są biblioteki do serializacji i deserializacji danych, np. cereal http://uscilab.github.io/cereal/nie ma ona problemu z zapisaniem wektora stringów, std::map, itp.., czy to w trybie binarnym, xml czy jason. |
|
1 2 3 « 4 » 5 |