If anyone wants to load or save arrays, it is not that difficult, but I understand that i/o commands can seem strange sometimes.
This is a function replacement I made for load array:
void load_array(char *file_name, int object_size, int object_count, void *my_array)
{
FILE *my_stream;
size_t op_return;
//open
my_stream = fopen (file_name, "rb");
//read
op_return = fread (my_array, object_size, object_count, my_stream);
//close
fclose (my_stream);
fflush(my_stream);
}
And save array:
void save_array(char *file_name, int object_size, int object_count, void *my_array)
{
FILE *my_stream;
size_t op_return;
//open
my_stream = fopen (file_name, "wb");
//write
op_return = fwrite (my_array, object_size, object_count, my_stream);
//close
fclose (my_stream);
}
This is an example of
integer array usage:
//Read and save array integers example
//Code by MPL3D
#include "DarkSDK.h"
#include "stdio.h"
#define NUMITEMS 5
int my_array[NUMITEMS-1] = {1,2,3,4,5};
int numread=0;
char czText[256];
int row, column;
FILE *my_stream;
int close_error;
char my_filename[] = "file-name.txt";
int object_size = sizeof(int);
int object_count = NUMITEMS;
int op_return;
void savearray(char *, int , int , void *);
void loadarray(char *, int , int , void *);
void printarray_int(int *);
void wait_for_press_key();
void DarkSDK ( void )
{
dbSyncOn();
dbSyncRate (0);
while ( LoopSDK ( ) )
{
if ( dbEscapeKey ( ) ) return;
//print the initial values
printarray_int(my_array);
//save array values
savearray(my_filename, object_size, object_count, my_array);
//Erase initial values
for (int row = 0; row < NUMITEMS; row++)
{
my_array[row]=0;
}
//Reload values for checking
loadarray(my_filename, object_size, object_count, my_array);
//print the loaded values
printarray_int(my_array);
return;
}
}
void printarray_int(int *my_array)
{
char Screen_line[256];
dbCLS();
for (int row = 0 ; row < NUMITEMS; row++)
{
sprintf ( Screen_line , "%d" , my_array[row] );
dbPrint (Screen_line);
dbSync( );
if (dbEscapeKey()) break;
}
wait_for_press_key();
}
void wait_for_press_key()
{
char Screen_line[256];
strcpy ( Screen_line, "PRESS A KEY");
dbText ( 250 , 250 , Screen_line );
dbSync( );
dbSuspendForKey();
}
void loadarray(char *file_name, int object_size, int object_count, void *my_array)
{
FILE *my_stream;
size_t op_return;
//open
my_stream = fopen (file_name, "rb");
//read
op_return = fread (my_array, object_size, object_count, my_stream);
//close
fclose (my_stream);
//just for debugging
char Screen_line[256];
dbCLS();
sprintf ( Screen_line , "items read = %d ," , op_return );
strcat ( Screen_line, &file_name[0]);
dbText ( 10 , 50 , Screen_line );
wait_for_press_key();
}
void savearray(char *file_name, int object_size, int object_count, void *my_array)
{
FILE *my_stream;
size_t op_return;
//open
my_stream = fopen (file_name, "wb");
//write
op_return = fwrite (my_array, object_size, object_count, my_stream);
//close
fclose (my_stream);
//just for debugging
char Screen_line[256];
dbCLS();
sprintf ( Screen_line , "items written = %d ," , op_return );
strcat ( Screen_line, &file_name[0]);
dbText ( 10 , 50 , Screen_line );
wait_for_press_key();
}
By the way, Barbarian, I think the title of the thread is a little misleading, perhaps 'how can i read data from a txt file and assign it to an array' would have been better.
[Edit : Woops, it looks like from a long time ago. Just replace any 'SDK' with 'GDK' to update it
]