'printing a line when a certain word is found in a file (c++)
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
Solution 1:[1]
The issue is your if statement. The string::find() method returns the index of the first character in the substring being searched for. In this case, that's 0 (the start of the line), meaning the if evaluates to false. If the substring you're looking for is not present, find() returns std::string::npos, which is non-zero and thus in your if is evaluating to true.
What you should do is switch from if (line.find((yep))) to if (line.find(yep) == 0) if the "From" needs to be at the beginning of the line, or to if (line.find(yep) != string::npos) if it just needs to be contained anywhere in the line.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
