Mmm, I'm not sure how exactly I can help you since your code is incomplete. But I think I will try to explain you how to work with files. I encourage you to check this tutorial:
http://www.cplusplus.com/doc/tutorial/files/
OK, so at the beginning you were doing fine. You declared your file streams correctly:
1 2 3
|
// declaring variable to read data
ifstream in_stream;
ofstream out_stream;
|
and you also openned the files correctly:
1 2 3 4 5 6 7 8
|
// opening file
in_stream.open ("RocTempDataV1.txt");
//error checking if file exists
if( in_stream.fail( ) )
{ //file opened
cout << "Unable to open file - does not exit in current directory" << endl;
exit(0);
}
|
So what you did here is that you declared in_stream to be an input stream (a stream can be seen as a set of characters that travels to OR from a file), which means that in_stream should ONLY be used for input operations. The same rule applies for out_stream, which you declared to be an output stream, so you can ONLY use it for output operations. And thus, following statement is wrong:
cout << in_stream ;
cout is an output stream and in_stream is an input stream, so they are essentially of incompatible types.
Once, you declare your streams, you need to "open" them. You can somewhat say that this will "link" your stream to a file. Every stream has a source and a destiny. So openning an input stream will link it to its source and openning an output stream will link it to its destiny. I need to stress out that you opened both streams to the same file:
1 2 3 4
|
// opening file
in_stream.open ("RocTempDataV1.txt");
...
out_stream.open("RocTempDataV1.txt");
|
It is more common to have an input file from which you are going to read data and an output file, where you are going to store data after processing it.
Once everything is set up, you can use out_stream just as you use cout for output. It works exactly the same way, the only difference is that instead of putting the data on the screen, it will put it in your file. And of course, using in_stream is exactly the same as using cin, except that you would read the data from the file instead of the keyboard (and thus, there will be no 'pause' when inputing data).
So for example let's say you wish to read the first line of your file:
1 2 3 4 5 6 7
|
// declaring variables
string text;
istream in_file;
// opening the file
in_file.open("file.txt");
// reading the file
getline(in_file, text);
|
or if you wish to read numbers
1 2 3 4 5
|
// declaring variables
int number;
istream in_file;
in_file.open("file.txt");
in_file >> number;
|
And you can store data this way:
1 2 3 4
|
ostream out_file;
out_file.open("output.txt");
out_file << text << endl;
out_file << number;
|
You can basically store anything in an output file:
1 2
|
out_file << "Hello world!" << endl;
out_file << "I'm " << number << " years-old.";
|
I hope, this was helpful. This was just a very brief explanation, so I recommend you still check the tutorial I passed you.
EDIT: Oh yes, one more thing, remember to ALWAYS close the files once you finished using them (e. g. at the end of the program):
1 2
|
in_file.close()
out_file.close()
|