Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » random data & access((file could not be opened))
random data & access [message #1747083] Wed, 09 November 2016 09:31
Massimo Mancini is currently offline Massimo ManciniFriend
Messages: 24
Registered: September 2016
Junior Member
Dear All.

I am here posting a new question for which I did not find other cases similar to mine.

In details, I am writing a code for which it is supposed to create a .dat file in order to use the program, in order to access and create data randomly.
I do suppose that at the console should ask me to enter some info in order to input my questioning.... or I do suppose that the file .dat need to be charged separately in a specific folder.

Isn't it?

I don't know how to figure out the error, even because when I lunch the program @ the console appear: " File could not be opened".

Any idea on what is going on?

I am using Windows 8.1 and the compiler MinGW compiler - Eclipse Mars.

I would add the code in order to be more explicit.


#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;
}

Thank you for any advice.
Massimo
Previous Topic:Random Errors while build file
Next Topic:Adding Default linker Options for MingW compiler
Goto Forum:
  


Current Time: Fri Apr 26 12:28:32 GMT 2024

Powered by FUDForum. Page generated in 0.03024 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top