Advertisement

saving arrays

Started by July 12, 2002 01:27 PM
1 comment, last by lindon 22 years, 7 months ago
how do i save a array to file and then load it back in c i am using the game programming gurus book code. i have made an array called map[110][110] which holds all the map tile id and i want to save it out and read it back please help as i is sending my mad



  #include  < fstream >using namespace std;fstream OutputFile;OutputFile.open("filename.bin");for (int i =0;i<ArraySize;i++)    OutputFile.write(Data,4); //4 bytes for an int    // OutputFile.read(Data,4);OutputFile.flush; //clear bufferOutputFile.close;  


Best check the syntax for read/write, I''m not sure about the parameters. Obviously in a 2D array, use 2 for loops OutputFile.write((Y*Width)+X,4).

,Jay
Advertisement
Or specifically in C


    /* Don't know how many of these you need */#include <stdio.h>#include <conio.h>void main(){   FILE *out, *in;   int map[110][110];   int i, j;   // Write the map to a file called file.map   if((out = fopen("file.map","wb")) == NULL)   {      printf("Couldn't open file to write.");      exit(1);   }   for(j=0; j<110; j++)      for(i=0; i<110; i++)         putc(out,map[j][i]);   printf("Wrote map to file.\n");   // Read the map from a file called file.map   if((in = fopen("file.map","rb")) == NULL)   {      printf("Couldn't open file to read.");      exit(1);   }   for(j=0; j<110; j++)      for(i=0; i<110; i++)         map[j][i]=getc(in);   printf("Read map from file.\n");}    
This is done mostly from memory so there are probably some errors. Search the net for some of these functions for more help.

[edited by - GameCreator on July 12, 2002 3:46:11 PM]

This topic is closed to new replies.

Advertisement