Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C programming novice question-Saving data in a structure. 2

Status
Not open for further replies.

metamorphy

Programmer
Dec 16, 2001
34
US


Hello! I am new to programming, and trying to teach myself
C/C++ so bear with me. My problem is this:

In my (dos) program I have an array of structures. There is a function in the program which asks the user to input data into the variables of a structure in the array. I wish to be able to save the data that is entered into these structures so that when I quit the program, then run the program again, the data for each structure that has been manipulated is still in tact. Is there some sort of library function that I can use to do this, or must the data be written to some kind of .ini file and then read by the program on startup? Basically I want to save the records (structures) so that I can use the array
of structures as a database. Thanks in advance to anyone who will take to time to help out a rookie with a desire to learn.
 
/*
Write data to a file example (disclaimer - This is not a complete sample and I haven't compiled it, so it will not run "as is". You'll have to adapt it to your code. It's just meant to give you the idea and to steer you to where to look in a book, such as, "The Absolute Beginner's Guide To C")

I'm assuming that MyStructure exists and that iElements contains the number of elements in the structure.
*/

FILE *fp;

fp = fopen("data.txt", "w+");

if (fp == NULL)
{
/* print an error message */
}
else
{
for (i = 0; i < iElements; i++)
{
fwrite(MyStructure.sFirstName, sizeof(MyStructure.sFirstName), 1, fp);
fwrite(MyStructure.sLastName, sizeof(MyStructure.sLastName), 1, fp);
fwrite(MyStructure.iAge, sizeof(MyStructure.iAge), 1, fp);
}

fclose(fp);

/*
To read the data back in to the structure, do something similar, only use the &quot;r&quot; (read) flag and fread() function. When reading, use feof() to check for the &quot;end of file&quot; each time you read something, so you'll know when you're done. Don't rely on knowing how many how many elements are on disk, as the number of elements may change. There are other flags for file I/O, such as &quot;rb&quot; (read binary) and so on, so you will want to look these up in a reference to become familiar with them. You might also want to look at the &quot;cin <<&quot; and &quot;cout >>&quot; functions.

Hope this helps.
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_OF_STRUCTS 10
#define FILE_NAME &quot;fData.txt&quot;
#define MAX_RAND 100

typedef struct
{
int iRand1;
int iRand2;
int iRand3;
} RandNum;

int Choices();
void Rand_Structs( RandNum *randNum,int iSize );
void Display_Structs( const RandNum *randNum,int iSize );
void Record_Structs( const RandNum *randNum,char *sFile,int iSize );
int Input_Data( RandNum *randNum,char *sFile,int iSize );

int main( void )
{
RandNum randNum[NUM_OF_STRUCTS];
time_t t;
srand( time( &t ) );

while ( 1 )
{
switch ( Choices() )
{
case 0:
Rand_Structs( randNum,NUM_OF_STRUCTS );
break;

case 1:
Record_Structs( randNum,FILE_NAME,NUM_OF_STRUCTS );
break;

case 2:
if ( Input_Data( randNum,FILE_NAME,NUM_OF_STRUCTS ) == -1 )
{
printf( &quot;No data to retrieve.\n&quot; );
break;
}
Display_Structs( randNum,NUM_OF_STRUCTS );
break;

default:
printf( &quot;INVALID OPTIONS.\n&quot; );
}

}
return 0;
}

int Choices()
{
int iTemp;
printf( &quot;0=Insert Rand # into structs.\n&quot; );
printf( &quot;1=Record Stucts to database.\n&quot; );
printf( &quot;2=Retrieve Data from databse.\n&quot; );
printf( &quot;<Ctrl-C> to quit.\n&quot; );
scanf( &quot;%d&quot;,&iTemp );
return iTemp;
}


void Rand_Structs( RandNum *randNum,int iSize )
{
int i;
for ( i=0;i<iSize;i++ )
{
randNum.iRand1= ( rand()%MAX_RAND )+1;
randNum.iRand2= ( rand()%MAX_RAND )+1;
randNum.iRand3= ( rand()%MAX_RAND )+1;
}

}

void Display_Structs( const RandNum *randNum,int iSize )
{
int i;
for ( i=0;i<iSize;i++ )
{
printf( &quot;Struct #%d\n&quot;,i );
printf( &quot;iRand1=%d\n&quot;,randNum.iRand1 );
printf( &quot;iRand2=%d\n&quot;,randNum.iRand2 );
printf( &quot;iRand3=%d\n&quot;,randNum.iRand3 );
printf( &quot;\n\n&quot; );
}

}

void Record_Structs( const RandNum *randNum,char *sFile,int iSize )
{
FILE *fData;
int i;
fData= fopen( sFile,&quot;w+&quot; );

for ( i=0;i<iSize;i++ )
fprintf( fData,&quot;%d %d %d\n&quot;,
randNum.iRand1,
randNum.iRand2,
randNum.iRand3 );

fclose( fData );
}


int Input_Data( RandNum *randNum,char *sFile,int iSize )
{
FILE *fData;
int i;
int iTemp;

fData= fopen( sFile,&quot;r&quot; );
iTemp= 0;
if ( !fData ) return -1;

for ( i=0;i<iSize;i++ )
{
fscanf( fData,&quot;%d&quot;,&iTemp );
randNum.iRand1= iTemp;
fscanf( fData,&quot;%d&quot;,&iTemp );
randNum.iRand2= iTemp;
fscanf( fData,&quot;%d&quot;,&iTemp );
randNum.iRand3= iTemp;
}

fclose( fData );
return 0;
}


Just to add to what programsectets stated above, here is a crude example that could stand to be alot more user friendly. Mike L.G.
mlg400@blazemail.com
 
One thing I noticed after posting my very simplified example was that I had forgotten the [index] value in the loop. Since I had to make a correction anyway, I took a tip from LiquidBinary and decided to modify my example to also use the fprintf() function, which is indeed clearer than fwrite(). The corrected version would look more like this:

Code:
  int i;
  for (i = 0; i < iElements; i++)
  {
    fprintf( fp, &quot;%s %s %i\n&quot;,
           MyStructure[i].sFirstName,
           MyStructure[i].sLastName,
           MyStructure[i].iAge);
  }

I'm not sure how much you are able to comprehend at this point, but you may find the larger, more expanded example from LiquidBinary to be worth the effort.
 
I am looking at this and missing something,

is it not possible to simply use:

Code:
for(index = 0; index < recordcount; index++)
{
  fwrite(struc[index], sizeof(struc[index]), 1, fp);
}

????

DanJC

'You pay peanuts you get .....'
 
Possible in binary mode.
You should open file as &quot;wb+&quot;, but you will not be able to read it in editor for example, at least it will not be in understandable format. If you intend to read this file only within your program then binary read will work quicker and should take less space (it depends how allocated structure filled with string).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top