Reverse the output from a txt file

Write a C program to read the list from the file
and store them in the arrays. Your program
should write the list of client’s account
number, client’s name and the client’s balance
to another file called “newdata.txt” in reversed
order and also display them on the screen.


An example of output dialog is shown below
Account Name Balance
800 Stacy 100.10
700 Michael 81.05
600 Dale 1005.30
500 Richard 214.89
400 Stone -45.23
300 White 0.00
200 John 345.67
100 Jones 24.50

--------

This is what I have done so far bellow here....But the only missing part is the reversed order of “newdata.txt”
contents. Please help! Been trying now for few days with no succes. =(

Regards,
Mark

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
FILE *ifpt,*ofpt;
int i;
double account;
double balance;
char name[25],s[60];

if((ifpt=fopen("client.txt","r")) == NULL)
{
printf("ERROR : File could not be opened\n");
exit(1);
}
if ((ofpt = fopen("newdata.txt", "w")) == NULL)
{
printf("ERROR : File could not be opened\n");
exit(1);
}
else
{
printf ("\nAccount\tName\t\tBalance\n");
fprintf (ofpt,"\nAccount\tName\t\tBalance\n");
}
for(i=0;i<8;i++)
{
fgets(s,59,ifpt);
sscanf(s,"%lf %s %lf", &account,&name,&balance);
sprintf(s,"%.2lf\t%s\t\t%0.2lf\n", account,name,balance);
puts(s);
fputs(s,ofpt);
}
fclose(ifpt);
fclose(ofpt);

getch();

system("pause");
return 0;
}
first create a class for your data, define a vector of type your class(data class) , saved data, which you read from file, into the vector, and finally write them into target file in reverse way

some guideline for you

a class for your data
1
2
3
4
5
6
7
struct Customer{
	unsigned int account;
	string 	name;
	double  balance;
	Customer(unsigned int account, string name,double  balance):
	     account(account), name(name),balance(balance){}
};

do some declaration and defination
1
2
3
4
vector< Customer > data;
unsigned int acc;
string name;
double bln;

read data from source file
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream fs;
fs.open ("filename", ifstream::in);
// here a while loop to read data from file
while( fs.good()) // !fs.eof()   or whatever 
{
	// read each line here, do it whatever way is comfortable for you

     // do some reading tasks
 
    // put the data into vector
	data.push_back( Customer(acc, name , bln) );
}
fs.close();

Next write data to the target file
1
2
3
4
5
6
7

fs.open ("targetFile", ios::out | ios::trunc);
// here write to the other file in reserver way
for( unsigned int i = data.size() - 1 ; i >= 0 ; i--){
	// write here the data into file
}
fs.close();

you may need to have a look at http://www.cplusplus.com/reference/iostream/fstream/fstream/

hope it helps
Last edited on
Write a C program


first create a class for your data, define a vector of type your class


C programs don't do C++ classes or vectors.
Sorry did not see C

Oh still C , hehehehe -:) if it is a C than he should go to a C form, here is a C++ form , just kidding :-)

if no class , than no fstream, Oh God, Only FILE and array and char , it means a lot of code that do a little...

@
neoworld
you may write each line into a new file, e,g file1.txt, fil2.txt..., filen.txt) and at the end start from the last filen.txt till file1.txt and write each into the targetfile...... Oh God, I am happy that you created C++ :-) I have no idea about C

Some how I remember that C supports struct, but I am not sure, if it does than


1
2
3
4
5
typedef  struct Customer{
	unsigned int account;
	char[some_length_here] name;
	double  balance;
}data;


and If does than you may also be able to create an array of type Customer, just try it :-)

may be something like

 
data datas[20] ;// no idea about C as said before, but it should work 


if the above does not work you may try
data* datas = (data*)malloc(sizeof(data) * 20) ; // try it yourself, I just suggest it

If all this thing work than you may do the rest easily
hope it help
Last edited on
Topic archived. No new replies allowed.