簡體   English   中英

如何從文件中讀取某些字符串? 在丟棄C ++中我不想要的那些時

[英]How do I read in certain strings from a file? While discarding ones I do not want in C++

我正在閱讀的文件包含有關城市的數據。 我想知道如何只提取我想要的數據並將其應用到我的城市對象。 以下是我的城市課程。

    #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
    }  

以下是我想要讀取的文件所包含的文本類型的示例

1063061 | OH | Tobias | ppl | Marion | 39 | 101 | 404118N | 0830359W | 40.68833 | -83.06639 ||||| 985 ||| Monnett 1063062 | OH | Todds | ppl | Morgan | 39 | 115 | 393129N | 0815049W | 39.52472 | -81.84694 ||||| 983 |||斯托克波特

我的問題是如何排除'|' 我的文件輸入流中的字符? 以及如何僅提取我需要的字符串。 (例如Tobias為city_name或OH為州)以創建我的城市對象。 謝謝

您必須將行加載到字符串。 std :: getline()然后你必須添加一些while循環從一個字符串復制到另一個字符串。

while(loadedstring!='|')
{
newstring=newstring+loadedstring[i];
i++;
}

將getline與分隔符'|'一起使用 並編寫適合您格式化輸入數據的輸入序列

ifstream myfile ("input.txt");
string s;
getline (myfile,s,'|')

我在CS類中有一個實現了Logfile Parser的賦值,這對你來說似乎是一個很好的例子。

#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

源文件:

#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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM