Question

and save it in the same folder as your source Download file: data.txt code file. data.txt contains a list of clients account
800 Catherine 100.10 700 Roberto 81.05 600 James 1005.30 500 Sandor 214.89 400 Walter -45.23 300 Aliyah 0.00 200 Johnny 345.6
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <stdio.h>

#include <string.h>

int main(void) {

  FILE *fptr;

    // to count the account of lines

    int count=0;

    // to store account number

int temp_account;

    // to store balance

    double temp_balance;

    // to store name

    char temp_name[100];

    // parallel arrays to store these data

    int account[10];

    double balance[10];

    char name[20][10];

// open file

fptr = fopen("input.txt", "r");

// read contents from file usign fscanf

    // fscanf returns 3 as there are 3 auguments

while (fscanf(fptr,"%d %s %lf",&temp_account,temp_name,&temp_balance) == 3)

{

      // put name in string array using strign copy functino

      strcpy(name[count],temp_name);

      // add account

        account[count]=temp_account;

        // add balance

        balance[count]=temp_balance;

        count++;

}

    //close file

    fclose(fptr);

    // open file to write

    fptr = fopen("newdata.txt", "w");

    int i;

    // print header to console

    // format and print data in reverse order

    printf("Account\t\t\tName\t\tBalance\n\n");

    for(i=count-1;i>=0;i--){

       printf ("%d\t\t\t%10s\t\t%.2lf\n",account[i],name[i],balance[i]);

       fprintf (fptr,"%d\t\t\t%10s\t\t%.2lf\n",account[i],name[i],balance[i]);

    }

fclose(fptr);

return 0;

}

1 2 #include <stdio.h> #include <string.h> int main(void) { FILE *fptr; // to count the account of lines int count=0; // to s

OUTPUT:

Account Name Balance 100 200 300 400 500 600 700 800 Brandon Johnny Aliyah Walter Sandor James Roberto Catherine 24.50 345.67

FILE:

newdata.txt 1 heo 2 200 300 400 500 600 700 800 saved Brandon Johnny Aliyah Walter Sandor James Roberto Catherine inco 24.50

input.txt saved 1 800 Catherine 100.10 2 700 Roberto 81.05 600 James 1905.30 500 Sandor 214.89 5 400 Walter -45.23 6 300 Aliy

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.

Add a comment
Know the answer?
Add Answer to:
and save it in the same folder as your source Download file: data.txt code file. data.txt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Create an application that searches a file of male and female first names. A link...

    C++ Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration. Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT