Doubt about Files

Hello, i was wondering if ofstream, ifstream and fstream are for writing, reading, and writing and reading purposes respectively, for what are the tags ios::in, ios::out, and ios::out | ios::in?.

I mean, if i put the ios::out to a ifstream object i can't put strings in the file, so it seems to me that ofstream, ifstream and fstream make the rules of what you can and can't do.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>

int main() {
	std::ifstream archivo;
	archivo.open("ejemplo.txt", std::ios::out);
	
	if (!archivo.is_open()) {
		std::cout << "Error en la apertura del archivo\n";
		return 0;
	}
	
	std::string linea = "hola como estas";
	archivo << linea;
}


That launch me a lot of errors, but if i just change the ifstream by a fstream or ofstream it compile perfectly, so again, for what are the ios::out, ios::in?.
Last edited on
No one?
There's a little hint in the name of each of those streams.
i = input
o = output
f = file

Therefore, ifstream is input file stream,
fstream is simply file stream, which allows input and output regarding a file stream.

Read more about them at these links:
iostream: input/output stream
http://www.cplusplus.com/reference/istream/iostream/?kw=iostream
fstream: input/output file stream class
http://www.cplusplus.com/reference/fstream/fstream/
ofstream: output file stream
http://www.cplusplus.com/reference/fstream/ofstream/
ifstream: input file stream
http://www.cplusplus.com/reference/fstream/ifstream/

Hope this helps.
Last edited on
Topic archived. No new replies allowed.