I have a phone.txt like:
09236235965
09236238566
09238434444
09202645965
09236284567
09236235965
..and so on..
How can I process this data
Simple. First, note that you want an ifstream
, not an ofstream
. When you're reading from a file, you're using it as input - hence the i
in ifstream
. You then want to loop, using std::getline
to fetch a line from the file and process it:
std::ifstream file("phone.txt");
std::string phonenum;
while (std::getline(file, phonenum)) {
// Process phonenum here
std::cout << phonenum << std::endl; // Print the phone number out, for example
}
The reason why std::getline
is the while loop condition is because it checks the status of the stream. If std::getline
fails in anyway (at the end of your file, for example), the loop will end.