The following is a code that my teacher gave me .. I dont know the working of files in c++ :( this program is supposed to copy a datafile into another.. but its is not working ... many many errors.. fname1 and fname2 are already existing files,right? or fname2 is being created.. please help me understand the mechanism

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>

class file
{char fname1[20],fname2[10];
ifstream infile;
ofstream outfile;
cout<<"Enter the first file name ";
cin>>fname1;
infile.open(fname1;ios::ch);
if(!infile)
{
cout<<"File doesnt exist";
getch();
exit(0);
cout<<"Enter the second file name ";
cin>>fname2;
outfile.open(fname2,ios::out/ios::tranc);
while(!infile.ch())
{
infile.get(ch);
outfile.get(ch);
}
cout<<"file written successsfully ";
void file::closefile(){
infile.close();
outfile.close();
}
getch;
}

Recommended Answers

All 6 Replies

You got this code from a C++ Teacher? LOL Does'nt look like

Anyways, I prefer you should first read about files and streams rather than geting a readymade answer.
I can answer you thingy, I cam remake your whole code also, but then there is no point in doing it if you don't know about files in C++.

Go and read a text book. By the way, what text book are you consulting?
The program here is not at all tough, I am sure you will understand it( and will able to correct it too) once you know more about C++

So go and read Files and Streams in C++ as it is a important part in studying C++

This is a character-by-character copy of the source file to a destination file. This is what the code does.

Yes, GDICommander
It is (intend to be) a character by character copy.
But just look at the code, it is crap. Look at the loop, he is using .get() even in the output file.

buddy am from a small village in kerala and I have limited resources .. :( I refer the text prescribed by kerala govt education dept.. and I also refers OOP in c++ by balaguruswami ..The files in c++ section of my text is very brief. Could u refer a good book for me? or a website.. But I prefer a book.. a book which contain all about files an OOP concepts... Please help this little brother

I am not supposed to refer to a Java book in a C++ forum, but I suggest one of the books from Cay A. Hoffman. It is called: Big Java. There are nice chapters in this book that explain object-oriented programming. But be warned: this book is expensive.

The same author wrote an another book about C++. "Big C++" is the name.

cplusplus.com is a good reference for input and output operations with files.

It is good to have a book, but if you want my opinion, the best references are on the Internet. And again, Google is your friend!

on jeevsmyd
See jeevs, I didn't wanted to touch you on that. Just mark my words, you would definitely succeed as you have the hunger to do.
And for sure, I would help you. The book you are reading may be one of the best which is available nearby but it really crap. I would suggest a free online book, it is called Thinking In C++ by Bruce Eckel's http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html#DownloadingTheBook You should start reading the book from start. It will be very beneficial to you. Next is that, your curriculum, would require you to write the old c++ in your exams, do it. But always remember the Standard C++.
So, I hope you are getting me, and do start reading the book.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.