Hey im making a tilemap editor and are working on saving and loading tilemaps. Write now im only writing what texture fragment the selected tile has into a file.
Then I read that info into a stand alone SDL_Rect, just for testing.
im using stdio.h file I/O
So im wondering why the "ab+" doesn't update the buffer that read from the file data.
but it works when i open the file TWICE using different modes "wb+" and then "rb+"
Using "ab+": This sets the rect_info once and doesn't update it as the values change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 | if(KeyPressed[mouse_L])
{
engine->saveTileMap = true;
FILE *f_savedTileMap = fopen("SavedMaps\\newTileMap.txt", "ab+");
rewind(f_savedTileMap);
size_t nWritten = fwrite(&world->tile[0].textureFragment, sizeof(SDL_Rect), 1, f_savedTileMap);
SDL_Rect readData = {};
rewind(f_savedTileMap);
fread(&readData, sizeof(SDL_Rect), 1, f_savedTileMap);
fclose(f_savedTileMap);
printf("Elements written: %zu\n", nWritten);
printf("read Rect: {%d, %d, %d, %d}\n", readData.x, readData.y, readData.w, readData.h);
engine->saveTileMap = false;
}
|
Using "wb+" and then "rb+" and this works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | if(KeyPressed[mouse_L])
{
engine->saveTileMap = true;
FILE *f_savedTileMap = fopen("SavedMaps\\newTileMap.txt", "wb+");
size_t nWritten = fwrite(&world->tile[0].textureFragment, sizeof(SDL_Rect), 1, f_savedTileMap);
printf("Elements written: %zu\n", nWritten);
fclose(f_savedTileMap);
SDL_Rect readData = {};
f_savedTileMap = fopen("SavedMaps\\newTileMap.txt", "rb+");
fread(&readData, sizeof(SDL_Rect), 1, f_savedTileMap);
fclose(f_savedTileMap);
printf("read Rect: {%d, %d, %d, %d}\n", readData.x, readData.y, readData.w, readData.h);
engine->saveTileMap = false;
}
|
Im wondring if it would be possible to just open the file once and do both read and write operations on it, and i guess that should be done using "ab+"?