Working with files (ifstream and ofstream)...help!

Sep 27, 2015 at 5:10pm
My instructions are as follows...
1) Start a program to work with points. Begin by defining the data type Point that has two coordinate members 'x' and 'y'.
2) Prompt the user to input seven (x,y) pairs. As the data is entered, store it in a vector of Points called original_points.
3) Print the data in original_points to see what it looks like.
4) Open an ofstream and output each point to a file named mydata.txt.
...

I'm extremely new to working with files and I don't know how to have my program output the data to a given file. Do I have to make a file mydata.txt to output to, or does the program do it itself? Where can I view the file that the program makes?

I'll put my current source code here:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "std_lib_facilities.h";

struct Point
{
	int x;
	int y;
};

istream& operator>>(istream& is, Point& p)
{
	int  x, y;
	char ch1, ch2, ch3;
	is >> ch1 >> x >> ch2 >> y >> ch3;
	if (!is) return is;
	if (ch1 != '(' || ch2 != ',' || ch3 != ')'){
		is.clear(ios_base::failbit);
		return is;
	}
	p.x = x;
	p.y = y;
	return is;
}
ostream& operator<<(ostream& os, Point& p)
{
	return os << '(' << p.x << ',' << p.y << ')';
}

int main()
{
	try
	{
		vector<Point> points;
		for (int i = 0; i < 7; i++){
			cout << "Enter a point using the format: (x,y): ";
			Point p;
			cin >> p;
			points.push_back(p);
		}

		for (Point p : points)
			cout << p << endl;

		cout << "Please enter name of output file: ";
		string oname;
		cin >> oname;
		ofstream ost{ oname };
		if (!ost) error("can't open out file ", oname);

		for (Point p : points)
			ost << '(' << p.x << ',' << p.y << ')' << endl;
	}
	catch (exception& e)
	{
		cerr << e.what() << endl;
		keep_window_open("~1");
		return 1;
	}
	catch (...)
	{
		cerr << "unknown exception" << endl;
		keep_window_open("~2");
		return 2;
	}
}
Last edited on Sep 27, 2015 at 5:11pm
Sep 27, 2015 at 5:49pm
> Do I have to make a file mydata.txt to output to, or does the program do it itself?

The program ( ofstream ost{ oname }; ) will create the file if it does not exist.


> Where can I view the file that the program makes?

Open the file in a text editor and have a look at at.

Or if you want its contents to be displayed on the console at the end of the program,
add this line just before the last closing brace on line 64:
std::cout << "\n-----------\n\n" << std::ifstream( "mydata.txt" ).rdbuf() ;
Sep 27, 2015 at 5:55pm

> Do I have to make a file mydata.txt to output to, or does the program do it itself?

The program ( ofstream ost{ oname }; ) will create the file if it does not exist.


> Where can I view the file that the program makes?

Open the file in a text editor and have a look at at.

Or if you want its contents to be displayed on the console at the end of the program,
add this line just before the last closing brace on line 64:
std::cout << "\n-----------\n\n" << std::ifstream( "mydata.txt" ).rdbuf() ;


where can I find the file to open it in a text editor? I couldn't find it anywhere I looked.
Sep 27, 2015 at 5:58pm
It should be in the working directory (your project location); if not there, run a find (if Linux/Unix OS do an updatedb first)
Sep 27, 2015 at 6:03pm
I found it! Thank you very much! I could've sworn I looked there though @.@. Anyways, very much appreciated :)
Topic archived. No new replies allowed.