Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:07:37+00:00 2026-06-14T06:07:37+00:00

I have been dealing with a problem with 3 programs from this book: C

  • 0

I have been dealing with a problem with 3 programs from this book: C How to Program – Deitel 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:

  • First I run the random access file creator, nothing seems weird and I get the finish message.
    enter image description here

  • Second I compile the writing random access values file, then enter the values, nothing seems weird.
    Entering values

  • 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.

Reading File notepad

  • Fourth I compile the reading random access files program. and surprise the data showed isn’t what I entered.

    results didn't match

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!):

/*To create the file*/
#include <stdio.h>

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 <= 10000; i++ ){
            printf( "-" );
            fwrite( &client, sizeof( struct clientsData ), 1, cfPtr );
        }
            fclose( cfPtr );
    }
    printf( "\nFile succesfully created\n" );
    return 0;
}

/* To write the file */
#include <stdio.h>

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", &client.account );

        while( client.account != 0 ){
            printf( "Enter the lastname, firstname and the balance\n?" );
            fscanf( stdin, "%s%s%lf", client.lastname, client.name, &client.balance );
            fseek( cfPtr, ( client.account - 1 ) * sizeof( struct clientsData ), SEEK_SET );
            fwrite( &client, sizeof( struct clientsData ), 1, cfPtr );
            printf( "Enter account number\n?" );
            scanf( "%d", &client.account );
        }
        fclose( cfPtr );
    }
    return 0;
}

/*To read the File*/
#include <stdio.h>

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( &client, sizeof( struct clientsData ), 1, cfPtr );

            if( client.account != 0 ){
                printf( "%-6d%-16s%-11s%10.2f\n", &client.account, client.lastname, client.name, &client.balance );
            }
        }
        fclose( cfPtr );
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:07:38+00:00Added an answer on June 14, 2026 at 6:07 am

    Looks like everything is saved correctly. But you print pointers instead of values here:

    printf( "%-6d%-16s%-11s%10.2f\n", &client.account, client.lastname, client.name, &client.balance );
    

    Should it be like this?

    printf( "%-6d%-16s%-11s%10.2f\n", client.account, client.lastname, client.name, client.balance );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been dealing with a problem for a while. How can I set
I've been dealing with a frustrating problem recently. I have a lot of reflection
I have been dealing with a problem for some time and would really appreciate
I've been dealing with this problem for quite some time. What I am trying
We've been dealing with a problem with our event gateways since we upgraded from
I have a problem I've been dealing with lately. My application asks its users
I have been dealing with strange problem. I am using KryptonForm in a project.
I have been dealing many days with a problem without any success. In my
I've been dealing with this before but it seems like the problem came back.
I have been dealing with Nasm and GNU C inline asm on a Linux

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.