Editing Existing Data

Hi guys, still new to C++ programming here but I am trying to copy an existing data from a text file to a temporary text file so as to rewrite the data in it and then output the edited data back again but it isn't fruitful until now. Is there a way to duplicate the data because I am currently stuck at that part?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  ofstream outfile;
	const int MAX=10;	
	int i=0, deposit, newBal;
	int ID[MAX], Balance[MAX], acc;
	cout<<"\nPlease re-enter your Account ID: ";
	cin>>acc;
	ifstream balanceFile,newBalance;
	balanceFile.open("Balance.txt");

	if (!balanceFile)
		cout<<"Unable to open requested file!";
	else
	{
		while (balanceFile>>ID[i]>>Balance[i])
		{
			newBalance.open("NewBalance.txt");	
			balanceFile>>ID[i]>>Balance[i];
			i++;
		}
		
		balanceFile.close();
		newBalance.close();
		bool success = false;
		
			for (int j=0; !success && j<i; j++)
			{
				{
				if (ID[j] == acc )
				success = true;	
				}				
				if (success)
				{ 
					cout<<"Please enter the amount of money you want to deposit : ";
					cin>>deposit;
					
					outfile.open("NewBalance.txt",std::ios_base::app);
					newBal = deposit + Balance[j];
					cout<<"New Balance is : " <<newBal<<endl;
					outfile<<ID[j]<<"\t"<<newBal<<endl;
				}
				outfile.close();					
			}
	}
1
2
3
4
5
6
7
#include <fstream>
#include <string>

bool copy_file( std::string srce_file_name, std::string dest_file_name )
{ 
    return bool( std::ofstream(dest_file_name) << std::ifstream(srce_file_name).rdbuf() ) ; 
}
Topic archived. No new replies allowed.