Program to copy .dat file to .out file?

I'm new to programming and I'm trying to understand what my professor is asking me to do.

The instructions say,

"Write a program to copy a data file "lab01.dat" to a new file "lab01.out"

The instructions probably seem self explanatory but someone please explain this to me. I don't know if I understand what exactly he wants me to do or how to do it.

In class all we learned how to do was read, write, open, close a file, etc.

I don't remember him saying anything about copying a data file to a new file. And I can't find anything in the book.


I feel like this is really simple and I'm making it way more complicated than it is.
All you have to do is read from lab01.dat using an ifstream and write every line to lab01.out with an ofstream.
Not as easy as you're making it sound.
You said in class you learned to read from files.
So read from lab01.dat...


You also said in class you learned to write to files
So write to lab01.out

I don't get what you're having trouble with

Here's an example. Since you're using .dat files, i'm guessing that they're binary files.

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
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	ifstream originalfile;
	originalfile.open("lab01.dat",ios::binary);
	ofstream copiedfilecopy;
	copiedfilecopy.open("lab01.out",ios::binary);
	if (originalfile.is_open())
	{
		while (!originalfile.eof())
		{
			char byte;
			byte = originalfile.get();
			copiedfilecopy.put(byte);
		}
		originalfile.close();
		copiedfilecopy.close();
		cout << "File copied." << endl;
	} else //If file not found
	{
		copiedfilecopy.close();
		cout << "Error: File not found." << endl;
	}
	return 0;
}
Last edited on
Thank you for your help.
Or even better:
http://www.cplusplus.com/forum/windows/132701/#msg713637

Also don't rely on is_open or eof.
They're both wrong.
is_open doesn't tell you if an error happened and it is safe to write.
eof is triggered only AFTER it gets "touched", copying the last character twice in this case.
Also copying with such a small buffer (1 byte) is pretty slow.
Topic archived. No new replies allowed.