I get a READ error?
- Code: Select all
if(tX[x]==0) {
CreateActor("clone", "TILES", "(none)", "(none)", x, y, true);
tX[x]=1;
}
if(tX[x]==0) {
CreateActor("clone", "TILES", "(none)", "(none)", x, y, true);
tX[x]=1;
}
int map[10][10];
void
map_save (char* filename)
{
FILE* file = fopen(filename, "w");
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
fprintf(file, "%d, ", map[j][i]);
}
fprintf(file, "\n");
}
fclose(file);
}
void
save_txt (char* filename)
{
FILE* file = fopen(filename, "w");
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
fprintf(file, "%d\n", j);
}
fprintf(file, "\n");
}
fclose(file);
}
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
void
load_txt (char* filename)
{
FILE* file = fopen(filename, "r"); // notice the format is switched to "r"
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
int temp = 0;
fscanff(file, "%d\n", &temp); // temp now holds the value that should be read in
// so you would want to do something with it... but didn't actually save anything important
}
fscanf(file, "\n");
}
fclose(file);
}
skydereign wrote:As I mentioned in our pms, you just need to use the opposite operations. Of course, normally when you save information, the information is important (and stored in some variable). That way you can load the information back into the same variable. In your example, you are just printing out j...
- Code: Select all
void
load_txt (char* filename)
{
FILE* file = fopen(filename, "r"); // notice the format is switched to "r"
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
int temp = 0;
fscanff(file, "%d\n", &temp); // temp now holds the value that should be read in
// so you would want to do something with it... but didn't actually save anything important
}
fscanf(file, "\n");
}
fclose(file);
}
So, when you save, you save variables. That way you can load the values back into the variable.
#define WIDTH 5
#define HEIGHT 5
int map[HEIGHT][WIDTH] = {{1, 1, 1, 1, 1},
{0, 0, 0, 0, 1}};
void
map_save (char* filename)
{
FILE* file = fopen(filename, "w");
if(file != NULL)
{
int i, j;
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
fprintf(file, "%d, ", map[i][j]);
}
fprintf(file, "\n");
}
fclose(file);
}
}
void
map_load (char* filename)
{
FILE* file = fopen(filename, "r");
if(file != NULL)
{
int i, j;
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
fscanf(file, "%d, ", &(map[i][j]));
}
fscanf(file, "\n");
}
fclose(file);
}
}
Users browsing this forum: No registered users and 1 guest