I am trying to understand how reading from a file using ifstream in C++ can be modified to work as needed. Right now, from my understanding, a single string is read if I use an std::string, where the definition of a string is text delimited by whitespace.
So this would read the work "this" into my std::string.
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::string fname("file.txt");
std::string data;
std::ifstream myfile(fname);
myfile >> data;
myfile.close();
std::cout << "read from file: " << data << std::endl;
exit(0);
}
If file.txt contained the line "this is a string".
What I am wondering, and cannot find, is if I can modify the behaviour of the >> operator, or possibly overload it in a custom class, to change the delimiter. For example, I could read an entire line until a \n is found. I could read delimiting by commas in a CSV file.
Etc.
Is this possible? I'm used to working in C and Python, and my C++ is rather weak.
Thanks.