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;
}
OUTPUT:
FILE:
Please upvote if you like my answer and comment below if you have any queries or need any further explanation.
and save it in the same folder as your source Download file: data.txt code file. data.txt...
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....