PDA

View Full Version : Help needed (Newbie) - Exporting data (Visual C)


wandering_shygu
06-23-2006, 02:03 PM
Hey, I am writing a program that stores information in 4 arrays (2 dimensional).
Before exiting from the program, I want to include the option to export all the data (or save) into a file (doesn't matter if it is .dat or txt).
And when I start the program again, I also want to have the option to load all the data onto the program so that I can add more or delete from it.
What command or function do i need to use..?
Can somebody please advice..
I am quite new to this...
By the way, the data need not be formatted.. As long as C it is able to scan the variable into memory from the file when load it it's is ok..
Thanks alot..

oracleguy
06-23-2006, 06:00 PM
Are these arrays a fixed size or dynamic?

wandering_shygu
06-26-2006, 10:41 AM
sorry for the late reply..
the array is of a fixed size, say 4 arrays each with 30 rows and 6 columns.
thanks..

wandering_shygu
06-30-2006, 04:15 PM
Help... anybody...?

oracleguy
06-30-2006, 06:06 PM
You could write the data into a binary file and then read it directly back into the array when you want to load it from the file. I think that will work but I'm not sure, it is at least worth looking into.

morongo
07-02-2006, 04:24 PM
Here's an example of what oracle is talking about, Load and save ints to an array:

int i,j;
int mydat[30][6];
FILE *fp;


//to save-out int data:
fp=fopen("mydata.dat","wb");

for(i=0 ; i<30 ; i++)
{
for(j=0 ; j<6 ; j++)
{
fwrite( mydat[i][j],sizeof(int),1,fp);
}
}
fclose(fp);
//data now saved to file


//read-in int data:
fp=fopen("mydata.dat","rb");
if(fp==NULL)
{
printf("file not found\n");
return 0;
}

for(i=0 ; i<30 ; i++)
{
for(j=0 ; j<6 ; j++)
{
fread( mydat[i][j],sizeof(int),1,fp);
}
}
fclose(fp);
//data now loaded to array

morongo
07-02-2006, 04:37 PM
You can do the same with strings but it's a little different, hope this does the trick.

paulq
07-17-2006, 08:45 PM
If you are using MFC, look into the CArchive class. Saves complex data structures automatically for you. It is a little advance, however. Easiest would be just save to a text file in a predictable format you define.