I need to read from file and print line by line
Here is my file with colon separators
Miami:Sunny:USA
London:Rainy:England
and print as it's below:
City: Miami Weather:Sunny Country: USA
City: London Weather:Rainy Country: England
However I got this:
City: Miami Weather: Sun Country USA
London City: Rain Weather: England Country USA
Here is what I have done so far:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
void read_from_file()
{
string city;
string c;
string w;
fstream file("list.txt");
if (file.is_open())
{
while (file.good())
{
getline(file, city, ':');
cout << " City: " << name;
getline(file, w, ':');
cout << " Weather: " << w;
getline(file, c, ':');
cout << " Country: " << c;
}
file.close();
}
}
int main()
{
read_from_file();
return 0;
}
I think my problem colon in getline(file, w, ':');
but when I put '\n'
instead it crashes.
Is there anybody who can help?