#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
//declaring all the libraries we will be using
int main() {
ifstream input("test.txt");
//opening the file with ifstream because we are trying to read it
string line;
while (getline (input,line)) {
cout << line << endl;
}
//we use this loop to read the file line by line and output each line as we go
return 0;
}
A few things about the above, why use ifstream not fstream? and I'm pretty sure you should check that it opens correctly rather than assuming. Also, can't you directly use the bit shift operators from stream to stream?
Ok, I don't use fstream that often, but Im pretty sure fstream is just the name for the library, not an actual type.
Just as iostream is the name of the library which includes things like cout and cin.
Your absolutely right, but I generally only would do that is the file is complicated, and could potentially be corrupted.
And lastly, I'm not sure if bit shift operators are usable with file reading.
Whenever I have looked up code for opening files, I only find this 'getline' technique.
fstream is a type, and can be used for both read and write operations.
Though unlike <iostream> which can be seperated into <ostream> and <istream> I don't think <fstream> can be split? Would need a second opinion though.
EDIT:
And yes, bit shift operators ( Though I think named "stream extraction operators" when working with streams ) can be used with any stream, such as file, display and string streams.