Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected behavior while creating, writing and reading a Random Access File in C
    primarykey
    data
    text
    <p>I have been dealing with a problem with 3 programs from this book: <a href="http://rads.stackoverflow.com/amzn/click/0136123562" rel="nofollow noreferrer">C How to Program - Deitel</a> They are related to the chapter 11 managing Files (Sequential and random access Files) in my studying method I like to test the example programs before I head the exercises, and I couldn't make work the 3 examples of the random access files (creating, reading and writing) I have got three unexpected behaviors. let me show:</p> <ul> <li><p>First I run the random access file creator, nothing seems weird and I get the finish message. <img src="https://i.stack.imgur.com/GkzXi.png" alt="enter image description here"></p></li> <li><p>Second I compile the writing random access values file, then enter the values, nothing seems weird. <img src="https://i.stack.imgur.com/96TjP.png" alt="Entering values"></p></li> <li><p>Third I want to use the hare strategy and read the .dat file with notepad to see if the data got saved properly. And surprise some garbage is showed. And i thought well, maybe the double representation doesn't match with the reading program (notepad). So i moved to the fourth step.</p></li> </ul> <p><img src="https://i.stack.imgur.com/s1dgk.png" alt="Reading File notepad"></p> <ul> <li><p>Fourth I compile the reading random access files program. and surprise the data showed isn't what I entered.</p> <p><img src="https://i.stack.imgur.com/V7LT7.png" alt="results didn&#39;t match"></p></li> </ul> <p>I didn't want to post this question here cause I know there are more important questions, and more interesting but i can't find what I did wrong, and I have been looking for some time and I finally decided to ask the experts. I leave you the SC below (Thanks!):</p> <pre><code>/*To create the file*/ #include &lt;stdio.h&gt; struct clientsData{ int account; char lastname[ 30 ], name[ 30 ]; double balance; }; int main(void) { int i; struct clientsData client = { 0, "", "", 0.0 }; FILE *cfPtr; if( ( cfPtr = fopen( "clients.dat", "wb" ) ) == NULL ) { printf( "File couldn't be opened. " ); } else{ printf( "Generating\n" ); for ( i = 1; i &lt;= 10000; i++ ){ printf( "-" ); fwrite( &amp;client, sizeof( struct clientsData ), 1, cfPtr ); } fclose( cfPtr ); } printf( "\nFile succesfully created\n" ); return 0; } </code></pre> <hr> <pre><code>/* To write the file */ #include &lt;stdio.h&gt; struct clientsData{ int account; char lastname[ 30 ], name[ 30 ]; double balance; }; int main(void) { FILE *cfPtr; struct clientsData client = { 0, "", "", 0.0 }; if( ( cfPtr = fopen( "clients.dat", "rb+" ) ) == NULL ) { printf( "File couldn't be opened. " ); } else{ printf( "Enter account number"" ( 1 to 10000, 0 to end the input )\n?" ); scanf( "%d", &amp;client.account ); while( client.account != 0 ){ printf( "Enter the lastname, firstname and the balance\n?" ); fscanf( stdin, "%s%s%lf", client.lastname, client.name, &amp;client.balance ); fseek( cfPtr, ( client.account - 1 ) * sizeof( struct clientsData ), SEEK_SET ); fwrite( &amp;client, sizeof( struct clientsData ), 1, cfPtr ); printf( "Enter account number\n?" ); scanf( "%d", &amp;client.account ); } fclose( cfPtr ); } return 0; } </code></pre> <hr> <pre><code>/*To read the File*/ #include &lt;stdio.h&gt; struct clientsData{ int account; char lastname[ 30 ], name[ 30 ]; double balance; }; int main(void) { FILE *cfPtr; struct clientsData client = { 0, "", "", 0.0 }; if( ( cfPtr = fopen( "clients.dat", "rb" ) ) == NULL ) { printf( "File couldn't be opened. " ); } else{ printf( "%-6s%-16s%-11s%10s\n", "Acct", "Lastname", "Firstname", "Balance" ); while( !feof( cfPtr ) ){ fread( &amp;client, sizeof( struct clientsData ), 1, cfPtr ); if( client.account != 0 ){ printf( "%-6d%-16s%-11s%10.2f\n", &amp;client.account, client.lastname, client.name, &amp;client.balance ); } } fclose( cfPtr ); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload