im trying to write a program that involves a text file. my goal is to get any line that starts with the string "From:" and put that line without the "From" into an array
this is what ive come up so far, but it seems to be doing the exact opposite of what i want and saves every line that DOESNT have a "From:" in the line
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <list>
using namespace std;
int main()
{
ifstream file;
string array[10000];
string line;
string yep = "From:";
int i = 0;
file.open("myFile.txt"); //name of file im trying to open
while(!file.eof())
{
getline(file, line);
if (line.find((yep)))
{
array[i++]=line;
}
}
file.close();
cout<< array[0] << endl;
cout<< array[1] << endl;
cout<< array[2] << endl;
cout<< array[3] << endl;
cout<< array[4] << endl;
cout<< array[5] << endl;
cout<< array[6] << endl;
cout<< array[7] << endl;
cout<< array[9] << endl;
cout<< array[10] << endl;
cout<< array[11] << endl;
cout<< array[12] << endl;
cout<< array[13] << endl;
cout<< array[14] << endl;
cout<< array[15] << endl;
cout<< array[16] << endl;
cout<< array[17] << endl;
}
here is what is in "myFile.txt."
From: Seoul, South Korea
To : Toronto, Canada
Luxembourg, Luxembourg
Seattle, United States
Dhaka, Bangladesh
Guatemala City, Guatemala
From: Tokyo, Japan
To : Ottawa, Canada
Vilnius, Lithuania
Rome, Italy
From: Hong Kong, SAR
To : New York City, United States
New Delhi, India
Washington, United States
Dublin, Ireland
Lisbon, Portugal
Vienna, Austria
Santiago, Chile
Rio de Janeiro, Brazil
Berlin, Germany
From: London, United Kingdom
To : Accra, Ghana
From: Osaka, Japan
To : Guatemala City, Guatemala
Helsinki, Finland
Detroit, United States
Vienna, Austria
Shenzhen, People's Republic of China
Harare, Zimbabwe
Montreal, Canada
Jakarta, Indonesia
Birmingham, United Kingdom
From: Geneva, Switzerland
To : Ljubljana, Slovenia
Kiev, Ukraine
Pittsburgh, United States
Bratislava, Slovakia
Mumbai, India
Luxembourg, Luxembourg
Rome, Italy
Lisbon, Portugal
Abu Dhabi, United Arab Emirates
From: Copenhagen, Denmark
To : Beijing, People's Republic of China
Pittsburgh, United States
From: Zurich, Switzerland
To : Seattle, United States
Caracas, Venezuela
Manila, Philippines
From: Oslo, Norway
To : Dusseldorf, Germany
Shanghai, People's Republic of China
New Delhi, India
White Plains, United States
Pittsburgh, United States
Denver, United States
Rome, Italy
Quito, Ecuador
Buenos Aires, Argentina
From: New York City, United States
To : Abidjan, Cote d'Ivoire
Casablanca, Morocco
Jakarta, Indonesia
Copenhagen, Denmark
Kingston, Jamaica
From: St. Petersburg, Russia
To : Amman, Jordan
From: Milan, Italy
To : Houston, United States
Mexico City, Mexico
From: Beijing, People's Republic of China
To : Ljubljana, Slovenia
Kuala Lumpur, Malaysia
Dhaka, Bangladesh
Melbourne, Australia
Sydney, Australia
Casablanca, Morocco
Munich, Germany
Istanbul, Turkey
here is the output:
To : Toronto, Canada
Luxembourg, Luxembourg
Seattle, United States
Dhaka, Bangladesh
Guatemala City, Guatemala
To : Ottawa, Canada
Vilnius, Lithuania
Rome, Italy
New Delhi, India
Washington, United States
Dublin, Ireland
Lisbon, Portugal
Vienna, Austria
Santiago, Chile
Rio de Janeiro, Brazil
Berlin, Germany
To : Accra, Ghana