how to write to a new txt file after opening a file

read the opened file first then write them again to a new txt file

Page 1 of 1

3 Replies - 1091 Views - Last Post: 21 October 2009 - 05:27 AM Rate Topic: -----

#1 chubzyko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 08

how to write to a new txt file after opening a file

Post icon  Posted 20 October 2009 - 05:00 AM

hello there! im getting insane of this program that i wanna try, i have the read and write code working though but what i wanna do is that after reading a file, it will display the content of the txt file, then writes all the content of the file to a new txt file. I think this needs an array,if the filename exist, then write to a new file name. Right? hehe! here's the read code

Read code
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::ios;
using std::left;
using std::right;
using std::showpoint;

#include <fstream>
using std::ifstream;

#include<iomanip>
using std::setw;
using std::setprecision;

#include <string>
using std::string;

#include <cstdlib>
//using std::exit;

void outputLine( int, const string, double );
//void outputLine( int,const string, double );

int main()

{
	ifstream inClientFile( "clients.txt", ios::in );

	if ( !inClientFile )

	{
		cerr<< "File could not be opened"<<endl;
//		exit( 1 );
	}

	int account;
	//char name[30];
	char name[30];
	double balance;

	cout<< left << setw( 10 ) << "Account" << setw( 13 )
		<< "Name" << "Balance" << endl << fixed << showpoint;

	while ( inClientFile >> account >> name >> balance )
		outputLine( account, name, balance );

	return 0;

}

void outputLine( int account, const string name, double balance )
{

	cout << left << setw(10) << account << setw(13) << name 
		<< setw(7) << setprecision(2) << right << balance << endl;
}



and here's the Write code
#include<iostream>
using std::cin;
 using std::cout;
 using std::endl;
 using std::ios;
 using std::cerr;

 #include<fstream>
 using std::ofstream;

 #include<cstdlib>
 //using std::exit;

 int main()
	 
 {
	ofstream outClientFile("clients.txt", ios::out);
	if (!outClientFile)

	{
		cerr<<"File name could not be opened"<<endl;
		exit(1);

	}
		cout<<"Enter the account,name,balance."<<endl
			<<"Enter end-of-file to end input.\n?";

	int account;
	char name[30];
	double balance;

	while (cin>>account>>name>>balance)
	{

		outClientFile<<account<< ' ' <<name<< ' ' <<balance<<endl;
		cout<<"?";

	}
	return 0;

}



any kind of help would be greatly appreciated. Thanks! B)

Is This A Good Question/Topic? 0
  • +

Replies To: how to write to a new txt file after opening a file

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: how to write to a new txt file after opening a file

Posted 20 October 2009 - 08:21 AM

Sorry, but I don't understand the problem. outputLine() could be changed slightly to accommodate printing to either the screen or to a file. All you have to do is add another parameter.
void outputLine( ostream& out, int account, const string name, double balance )
{

	out << left << setw(10) << account << setw(13) << name
		<< setw(7) << setprecision(2) << right << balance << endl;
}


Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: how to write to a new txt file after opening a file

Posted 20 October 2009 - 09:12 AM

just open two files and output the first to the second?

ifstream in("FirstFile.txt");
ofstream out("SecondFile.txt");
string line = "";
while(getline(in,line))
	 out<<line<<endl;
in.close();
out.close()
//done


This post has been edited by ImaSexy: 20 October 2009 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 chubzyko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 08

Re: how to write to a new txt file after opening a file

Posted 21 October 2009 - 05:27 AM

tnx for the idea ImaSexy.

I had successfully copied some of the content of the first txtfile that has been read to the new textfile. But it's not copying/writing the variable name.

my first notepad the is to be read has this info
eg.
25 Adonis 5000


so after running the program, it should be able to write again what is inside of the first txtfile to the a new txtfile. But when I opened the new textfile, it only cointains this info

25 5000

it's not writing the name!
Someone please help me! I really need this to be done asap!



here are my codes for as now
int main()

{
	ifstream inClientFile( "clients1.txt", ios::in );//Read
	if ( !inClientFile )
	{
		cerr<< "File could not be opened"<<endl;

	}

	ofstream outClientFile("clients2.txt", ios::out); //Write
	if (!outClientFile)	
	{
		cerr<<"File name could not be opened"<<endl;
		exit(1);
	}

	int account;  
	double balance;
	char name[30];


	cout<<"Account		 "		
			  << "Full Name	" << "	Balance" << endl << fixed << showpoint; //Display

	while ( inClientFile >> account >> name >> balance )//Read
		outputLine( account, name, balance );			//Read

	{
	   	outClientFile<<account<< ' ' <<name<< ' ' <<balance<<endl;//Write
	}

 return 0;

}

void outputLine( int account, const string name, double balance ) //Read
{
	  cout <<"   "<< account<<"		 " <<" " <<name<<"	 " //Read
		<<  setprecision(2) << right << balance << endl;		//Read
}


This post has been edited by chubzyko: 21 October 2009 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1