10 Replies - 368 Views - Last Post: 16 July 2011 - 02:52 AM Rate Topic: -----

#1 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Copying contents of one txt file to other

Posted 15 July 2011 - 10:05 PM

Guys , i've got some trouble while copying text files. I used the following code :

void copier()
{
	ifstream fout("LUDODESGIN.txt");
	ofstream fin("Temp.txt");
	char ch;
	while (fout.get(ch))
	{
   	cout<<ch;
	fin<<printf("%c",ch);
	}
        fout.close();
	fin.close();
}



i am not getting any output for the
 cout<<ch; 

also .

I need this to do my school project. Please help

Is This A Good Question/Topic? 0
  • +

Replies To: Copying contents of one txt file to other

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Copying contents of one txt file to other

Posted 15 July 2011 - 10:24 PM

is the file being opend?

try adding a check after opening each file
if(!fout) {
   cout<<"there was an error opening the file\n";
}



look at this
fin<<printf("%c",ch);

i think your a bit confused here. printf returns the length of what it printed out, not what it printed out. you just want
fin<<ch;

Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Copying contents of one txt file to other

Posted 15 July 2011 - 10:26 PM

I'll make it easy.
Don't use printf() in C++ application.
Was This Post Helpful? 1
  • +
  • -

#4 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1572
  • View blog
  • Posts: 2,993
  • Joined: 30-May 10

Re: Copying contents of one txt file to other

Posted 15 July 2011 - 10:34 PM

> 03 ifstream fout("LUDODESGIN.txt");
> 04 ofstream fin("Temp.txt");
The code would be far more readable if you had named your variables like so.

ifstream fin("LUDODESGIN.txt");
ofstream fout("Temp.txt");

Was This Post Helpful? 1
  • +
  • -

#5 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Copying contents of one txt file to other

Posted 15 July 2011 - 10:34 PM

guys i tried it with
 fin<<ch 

but it still doesn't work !

the main thing is
even the statement

 cout<<ch 
doesn't work
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1572
  • View blog
  • Posts: 2,993
  • Joined: 30-May 10

Re: Copying contents of one txt file to other

Posted 15 July 2011 - 10:42 PM

Well post your latest code showing what error checking you've added, and what other fixe(s) have been suggested.

You can't post 1 line and say it doesn't work.
Was This Post Helpful? 0
  • +
  • -

#7 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Copying contents of one txt file to other

Posted 15 July 2011 - 10:49 PM

i've made the new code this way

void copyfile()
{
	ifstream fin("LUDODESIGN.txt");
   ofstream fout("Temp.txt");
   char line[80];
   while(fin.getline(line,80))
   {
   	cout<<line<<endl;
      fout<<line<<endl;
   }
   fin.close();
   fout.close();
}



but now the whole alignment has shifted down by 1 line :nottalkingtoyou:
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Copying contents of one txt file to other

Posted 16 July 2011 - 02:33 AM

Please explain what this means.

View Postsudheesh1995, on 16 July 2011 - 04:49 PM, said:

but now the whole alignment has shifted down by 1 line :nottalkingtoyou:


What are you trying to say?

Maybe an example?
Was This Post Helpful? 1
  • +
  • -

#9 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Copying contents of one txt file to other

Posted 16 July 2011 - 02:37 AM

well ... it means that the first line now has NO output , basically the output starts from the 2nd line , and i want it to go one line 'UP' on my DOS screen.
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Copying contents of one txt file to other

Posted 16 July 2011 - 02:48 AM

Give us a working program and not just a function.

That means you need to give us a main() that calls your function and produces the result you are reporting.
Was This Post Helpful? 0
  • +
  • -

#11 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Copying contents of one txt file to other

Posted 16 July 2011 - 02:52 AM

thanks everyone , figured it out now , i had a
 cout<<endl; 

outside my main() function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1