|
Re: random data & access [message #1746972 is a reply to message #1746773] |
Tue, 08 November 2016 08:55 |
Massimo Mancini Messages: 24 Registered: September 2016 |
Junior Member |
|
|
I would add the code in order to be more explicit in my post.
Thanks for any comments.
Massimo
#include <stdio.h>
/* dichiarazione della struttura clientData */
struct clientData {
int acctNum; /* numero del conto */
char lastName[ 15 ]; /* cognome dell'intestatario del cont */
char firstName[ 10 ]; /* nome dell'intestatario del conto */
double balance; /* bilancio del conto */
}; /* fine della struttura clientData */
int main( void )
{
FILE *cfPtr; /* puntatore al file credit.dat */
/* crea clientData con informazioni di default */
struct clientData client = { 0, "", "", 0.0 };
/* fopen apre il file; se non riesce ad aprire il file, provoca l'uscita dal programma */
if ( ( cfPtr = fopen( "credit.dat", "rb+" ) ) == NULL ) {
printf( "File could not be opened.\n" );
fflush(stdout);
} /* fine del ramo if */
else {
/* chiede all'utente di specificare il numero del conto */
printf( "Enter account number"
" ( 1 to 100, 0 to end input )\n? " );
fflush(stdout);
scanf( "%d", &client.acctNum );
/* l'utente inserisce l'informazione che viene copiata nel file */
while ( client.acctNum != 0) {
/* l'utente inserisce il cognome, il nome ed il bilancio */
printf( "Enter lastname, firstname, balance\n? " );
fflush(stdout);
/* imposta il cognome, il nome ed il valore del bilancio del record */
fscanf( stdin, "%s%s%lf", client.lastName,
client.firstName, &client.balance );
/* trova la posizione nel file del record specificato dall'utente */
fseek( cfPtr, ( client.acctNum - 1 ) *
sizeof( struct clientData ), SEEK_SET );
/* scrive l'informazione specificata dall'utente nel file */
fwrite( &client, sizeof( struct clientData ), 1, cfPtr );
/* consente all'utente di specificare un altro numero di conto */
printf( "Enter account number\n? " );
fflush(stdout);
scanf( "%d", &client.acctNum );
} /* fine del comando while */
fclose(cfPtr); /* fclose chiude il file */
} /* fine del ramo else */
return 0;
}
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03673 seconds