C++ 如何从文件中读取特定字符串?在丢弃我不想要的C++;
我正在读取的文件包含有关城市的数据。我想知道如何提取我想要的数据并将其应用于我的城市对象。下面是我的城市班C++ 如何从文件中读取特定字符串?在丢弃我不想要的C++;,c++,file-io,inputstream,C++,File Io,Inputstream,我正在读取的文件包含有关城市的数据。我想知道如何提取我想要的数据并将其应用于我的城市对象。下面是我的城市班 #include <iostream> #include <string> #include <cstdlib> #include "City.h" using namespace std; /* * Define the constructor for the class */
#include <iostream>
#include <string>
#include <cstdlib>
#include "City.h"
using namespace std;
/*
* Define the constructor for the class
*/
City::City(string city_name, string state, int longitude, int latitude, int population) {
this->city_name = city_name;
this->state = state;
this->longitude = longitude;
this->latitude = latitude;
this->population = population;
}
/*
*Accessors
*/
string City::getCity() {
return city_name;
}
string City::getState() {
return state;
}
int City::getLongitude() {
return longitude;
}
int City::getLatitude() {
return latitude;
}
int City::getPopulation() {
return population;
}
/*
* Mutators
*/
void City::setCity(string city_name) {
this->city_name = city_name;
}
void City::setState(string state) {
this->state = state;
}
void City::setLongitude(int longitude) {
this->longitude = longitude;
}
void City::setLatitude(int latitude) {
this->latitude = latitude;
}
void City::setPopulation(int population) {
this->population = population;
}
/*
* Sorting methods
*/
void City::sortByCity() {
// Code to sort cities by city name
}
void City::sortByLongitude() {
// Code to sort cities by longitude
}
#包括
#包括
#包括
#包括“City.h”
使用名称空间std;
/*
*定义类的构造函数
*/
城市::城市(字符串城市名称、字符串状态、整数经度、整数纬度、整数人口){
此->城市名称=城市名称;
这个->状态=状态;
这->经度=经度;
这个->纬度=纬度;
这->人口=人口;
}
/*
*访问者
*/
字符串City::getCity(){
返回城市名称;
}
字符串City::getState(){
返回状态;
}
int City::getLongitude(){
返回经度;
}
int City::getLatitude(){
返回纬度;
}
int City::getPopulation(){
返回人口;
}
/*
*突变子
*/
void City::setCity(字符串City\u name){
此->城市名称=城市名称;
}
无效城市::设置状态(字符串状态){
这个->状态=状态;
}
无效城市::设置经度(整数经度){
这->经度=经度;
}
无效城市::设置纬度(整数纬度){
这个->纬度=纬度;
}
无效城市::集合人口(整数人口){
这->人口=人口;
}
/*
*分类方法
*/
void City::sortByCity(){
//按城市名称对城市进行排序的代码
}
虚空之城::sortByLongitude(){
//按经度排序城市的代码
}
下面是一个我想从中读取的文件包含的文本类型的示例
1063061 | OH | Tobias | ppl | Marion | 39 | 101 | 404118N | 0830359W | 40.68833 |-83.06639 | | 985 | | |蒙内特
1063062 | OH | Todds | ppl | Morgan | 39 | 115 | 393129N | 0815049W | 39.52472 |-81.84694 | 983 |斯托克波特
我的问题是如何从文件输入流中排除“|”字符?以及如何只提取所需的字符串。(例如Tobias作为城市名称或OH代表州)以创建我的城市对象。谢谢您必须将行加载到字符串。std::getline() 然后,您必须添加一些while循环,以便从一个字符串复制到另一个字符串
while(loadedstring!='|')
{
newstring=newstring+loadedstring[i];
i++;
}
我在一个CS类中分配了一个任务,该类实现了一个日志文件解析器,这似乎是一个很好的例子
#ifndef _LOGFILE_PARSE_H_
#define _LOGFILE_PARSE_H_
#include string
#include vector
#include fstream
#include sstream
#include time.h
#include "lphashtable.h"
#include "schashtable.h"
/**
* LogfileParser class: Provides an interface for querying logfiles of a
* particular format.
*/
class LogfileParser {
public:
/**
* Constructs a new LogfileParser from the name of a log file.
*
* @param fname The name of the log file to open.
*/
LogfileParser( const std::string & fname );
/**
* Determines if a given customer has ever visited the given url.
*
* @param customer The customer name.
* @param url The url.
* @return A boolean value indicating whether the customer visited
* the url.
*/
bool hasVisited( const std::string & customer, const std::string & url ) const;
/**
* Determines *when* a customer last visited a given url. If the
* customer has not visited the given url, the output of this
* function should be the default time_t.
*
* @param customer The customer name.
* @param url The url.
* @return A time_t representing when the customer last visited the
* given url.
*/
time_t dateVisited( const std::string & customer, const std::string & url ) const;
/**
* Gets all of the unique urls that have been visited.
*
* @return A vector of urls that were visited in the logfile. Note
* that **there should be no duplicates in this vector**.
*/
std::vector< std::string > uniquePages() const;
private:
/**
* LogLine structure: Represents the information contained in a
* single line of the logfile.
*/
class LogLine {
public:
/**
* Constructs a LogLine from a string (actual physical line
* in the logfile).
*
* @param line The line in the file to extract info from.
*/
LogLine( const std::string & line );
std::string customer; /**< The customer for this line, */
std::string url; /**< The url for this line. */
time_t date; /**< The date for this line. */
};
/**
* HashTable used to determine when a customer visited a given url.
*
* Hint: think about what your key should be for this. How could
* you construct a unique, string key to find information for a
* given customer and url?
*/
LPHashTable< std::string, time_t > whenVisitedTable;
/**
* Vector containing the unique urls found in the logfile. You
* should fill this in the constructor.
*
* @note This vector **should not contain duplicates!**
*/
std::vector< std::string > uniqueURLs;
};
#endif
\ifndef\u日志文件\u解析\u H_
#定义日志文件解析_
#包含字符串
#包含向量
#包括fstream
#包括sstream
#包括时间
#包括“lphashtable.h”
#包括“schashtable.h”
/**
*LogfileParser类:提供查询日志文件的接口
*特定格式。
*/
类LogfileParser{
公众:
/**
*根据日志文件的名称构造新的LogfileParser。
*
*@param fname要打开的日志文件的名称。
*/
LogfileParser(const std::string&fname);
/**
*确定给定客户是否访问过给定url。
*
*@param customer客户名称。
*@param url返回url。
*@返回一个布尔值,指示客户是否访问
*url。
*/
布尔访问了(const std::string和customer,const std::string和url)const;
/**
*确定客户上次访问给定url的*时间*。如果
*客户尚未访问给定的url,此url的输出
*函数应为默认时间。
*
*@param customer客户名称。
*@param url返回url。
*@return表示客户上次访问
*给定的url。
*/
访问时间(const std::string&customer,const std::string&url)const;
/**
*获取已访问的所有唯一URL。
*
*@返回日志文件中访问的URL向量。注意
*该向量**中不应存在重复项**。
*/
std::vectoruniquePages()常量;
私人:
/**
*LogLine结构:表示包含在日志中的信息
*日志文件的单行。
*/
类对数线{
公众:
/**
*从字符串(实际物理行)构造日志行
*在日志文件中)。
*
*@param line文件中要提取信息的行。
*/
对数线(常数标准::字符串和线);
std::string customer;/**<此行的客户*/
std::string url;/**<此行的url*/
time\u t date;/**<此行的日期*/
};
/**
*用于确定客户何时访问给定url的哈希表。
*
*提示:想想你的钥匙应该是什么。你怎么能
*您可以构造一个唯一的字符串键来查找
*给定客户和url?
*/
LPHashTableuniqueurl;
};
#恩迪夫
源文件:
#include "logfile_parser.h"
#include iostream
using std::string;
using std::vector;
using std::ifstream;
using std::istringstream;
/**
* Constructs a LogLine from a string (actual physical line in the
* logfile).
*
* @param line The line in the file to extract info from.
*/
LogfileParser::LogLine::LogLine( const string & line ) {
istringstream iss( line );
iss >> customer;
customer = customer.substr(1, customer.length()-3);
iss >> url;
string dte = "";
string dline;
do {
iss >> dline;
dte += dline;
} while( iss );
date = time(NULL);
tm * tme = localtime( &date );
strptime( dte.c_str(), "%c", tme );
// force correct DST
tme->tm_isdst = 1;
date = mktime( tme );
}
/**
* Constructs a new LogfileParser from the name of a log file.
*
* @param fname The name of the log file to open.
*/
LogfileParser::LogfileParser( const string & fname ) : whenVisitedTable( 256 ) {
SCHashTable< string, bool > pageVisitedTable( 256 );
ifstream infile( fname.c_str() );
string line;
while( infile.good() ) {
getline( infile, line );
// if the line length is 0, move on to the next loop iteration
if( line.length() == 0 )
continue;
// otherwise parse the line and update the hash tables and vector
LogLine ll( line );
string uniqueString =(ll.customer + ll.url);
if(whenVisitedTable.keyExists(uniqueString))
{
if(whenVisitedTable[uniqueString] < ll.date)
whenVisitedTable[uniqueString] = ll.date;
}
else if (!whenVisitedTable.keyExists(uniqueString))
whenVisitedTable.insert(uniqueString, ll.date);
if(pageVisitedTable.keyExists(ll.url))
pageVisitedTable[ll.url] = true;
else if (!pageVisitedTable.keyExists(ll.url))
{
pageVisitedTable.insert(ll.url, true);
uniqueURLs.push_back(ll.url);
}
/*
* Given the LogLine above, you should be able to update the member
* variable hash table and any other hash tables necessary to solve
* this problem. This should also build the uniqueURLs member
* vector as well.
*/
}
infile.close();
}
/**
* Determines if a given customer has ever visited the given url.
*
* @param customer The customer name.
* @param url The url.
* @return A boolean value indicating whether the customer visited the url.
*/
bool LogfileParser::hasVisited( const string & customer, const string & url ) const {
string myString = (customer + url);
if(whenVisitedTable.keyExists(myString))
return true;
else return false;
}
/**
* Determines *when* a customer last visited a given url. If the customer
* has not visited the given url, the output of this function should be the
* default time_t.
*
* @param customer The customer name.
* @param url The url.
* @return A time_t representing when the customer last visited the given
* url.
*/
time_t LogfileParser::dateVisited( const string & customer, const string & url ) const {
string myString = (customer + url);
if(whenVisitedTable.keyExists(myString))
return whenVisitedTable.find(myString);
else
return time_t(); // replaceme
}
/**
* Gets all of the unique urls that have been visited.
*
* @return A vector of urls that were visited in the logfile. Note
* that **there should be no duplicates in this vector**.
*/
vector<string> LogfileParser::uniquePages() const {
return uniqueURLs;
}
I removed the <> from the #include statements for some so they showed. I did not write this whole class, a TA did some of it, and I was responsible for the rest. There are many more files to this, but this is the class that seems to be most relevant for you.
#包括“logfile_parser.h”
#包括iostream
使用std::string;
使用std::vector;
使用std::ifstream;
使用std::istringstream;
/**
*从字符串构造日志行(在
*日志文件)。
*
*@param line文件中要提取信息的行。
*/
LogfileParser::LogLine::LogLine(常量字符串和行){
伊斯特林溪一号
ifstream myfile ("input.txt");
string s;
getline (myfile,s,'|')