fstream

i've tried using this code:
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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

int main () {
	string contain;
	ifstream read_file ("D:\example.txt");
	ofstream write_file ("test.txt");
	cout << "this will copy the contain from 'D:\example.txt' to 'C:\test.txt'" << endl;

	if (read_file.is_open()) {
		while (!read_file.eof()) {
			getline (read_file, contain);
		}
	}
	else
		cout << "error!" << endl;

	write_file << contain;
	cout << "press any key to continue...";
	getch();
	return 0;
}


this is the contents of example.txt:


anyway, i wonder, if this could be copied to "test.txt"

can this line included too? i wonder...


but the contents of test.txt after the copy operation is:


can this line included too? i wonder...


can anyone tell me what's wrong?
14
15
16
		while (!read_file.eof()) {
			getline (read_file, contain);
		}
You are just overwritten contain, not appending the next read.
is there's another way to get all contents of example.txt into one variable (contain in this case)...?
Just put line 21 inside your while loop, and remember to close all opened files...
further reading: http://www.cplusplus.com/doc/tutorial/files/

is there's another way to get all contents of example.txt into one variable

plenty, e.g: http://www.cplusplus.com/reference/iostream/istream/read/
Nobody caught the missing '\' on line 9 huh? Or am I the one who is crazy?

I think
 
ifstream read_file ("D:\example.txt");

should be:
 
ifstream read_file ("D:\\example.txt");
You can also use vector of strings.
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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include<vector>
using namespace std;

int main () {
	string temp;
	vector<string>contain;
	ifstream read_file ("D:\\example.txt"); // notice that you must use '\\' instead of '\' if you 
// want to use directories 
	ofstream write_file ("test.txt");
	
	cout << "this will copy the contain from 'D:\example.txt' to 'C:\test.txt'" << endl;


if(read_file.is_open())
	while (getline(read_file,temp)) 
		contain.push_back(temp);

else
{
cerr<<"Error opening file...";
return 1;
}


	for(int i=0;i<contain.size();i++)
	{
		write_file<<contain[i];
	}
	cout << "press any key to continue...";
	getch();

	return 0;
}
Also the phrase on Line 15 is wrong. What this code will do, after it has been fixed, is copy the contents of "D:\example.txt" to ""path"test.txt" "path" being relative to the executable that is created when the code is compiled.

EDIT: You can fix that by changing line 13 of holtaf's code to:
1
2
ofstream write_file("C:\\test.txt", ios_base::app); /*This tags the data onto the end of the file
instead of overwritting it*/


If you do not close the open files then when the program is done executing the scheduler will do it for you. It's good practice to do this yourself though to avoid memory leaks in more complex programs.
Last edited on
@holtaf: but why you use return 1 instead return -1? is it typo? and you forgot your bracket in your if :)

@geek: do you mean this line?

getline (read_file, contain)

is it illegal?
hello?
from the Linux Programmer's Manual
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-Unix environments) than the use of 0 and some non-zero value like 1 or -1. In particular, VMS uses a different convention.
Also check this http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
However instead of 255* I'm receiving exit status modulo 256.

Computergeek01 is referring to holtaf's code. It corresponds to line 11 of your code.

Edit:error codes for Windows http://msdn.microsoft.com/en-us/library/ms681381%28v=vs.85%29.aspx
So I think it should be return ERROR_FILE_NOT_FOUND; //return 2;

Edit2:
ERROR_SUCCESS: If a Window's program terminated correctly, there must be an error.
Last edited on
Topic archived. No new replies allowed.