4

ファイルから文字列または数値データを読み取り、データを文字列または int/double のベクトルに保存するためのテンプレート関数を作成しました。次に、データを使用して、作成した別のコードで計算を実行します。

これは単純な質問だと思うので、お詫び申し上げます...空白がある文字列データを読み取ることができません...たとえば、名と姓。「トム・スミス」が欲しいときは、「トム」しか出てこない)。グーグルによると、問題は >> であり、代わりに getline を使用する必要があるようです。>> を getline(test,100) に置き換えようとしましたが、「std::basic_istringstream の呼び出しに一致する関数がありません...」というエラーが発生します (エラー: 'std:: の呼び出しに一致する関数がありません)。 basic_ifstream >::getline(double&)')

誰かが私を正してくれたらとてもありがたいです!ストリームについて頭が回らないようです。

これはいくつかのサンプル データと私のコードです。ここで文字列用に構成しました。

labelInFile // 1 つのベクトルのデータのサブセットの識別子

'Tom Smith' 'Jackie Brown' 'John Doe' // これらの名前はベクトルの要素として終了する必要があります

#include <algorithm>  
#include <cctype>     
#include <istream>
#include <fstream>
#include <iostream>    
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

using namespace std; 

template<typename T>  
void fileRead( std::vector<T>& results, const std::string& theFile, const std::string& findMe, T& test )  
{   
    std::ifstream file( theFile.c_str() ); 
    std::string   line;

    while( std::getline( file, line ) )
    {
        if( line == findMe )
        {
            do{
                std::getline( file, line, '\'' );  
                std::getline( file, line, '\'');

                std::istringstream myStream( line );

                myStream >> test;
                results.push_back( test );
            } 
            while ( file.get() != '\n' );
        }
    }
}


int main () 
{
    const std::string theFile               = "test.txt";  // Path to file
    const std::string findMe                = "labelInFile"; 
    std::string test;

    std::vector<string> results;

    fileRead<std::string>( results, theFile, findMe, test );

    cout << "Result: \n";
    std::copy(results.begin(), results.end(), std::ostream_iterator<string>(std::cout, "\n")); 

    return 0;
}
4

2 に答える 2

1

名前の解析という問題の一部を解決するコードを書きました。ニーズに合わせて調整できます。注: これはこれを解決するための最速の方法ではありませんが、理解しやすいと思われる単純な方法です。

テスト.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    const char szFname[] = "Test.dat";
    std::vector<std::string> vData;

    std::ifstream ifstr(szFname);

    while (ifstr.good())
    {
        // find first quote
        ifstr.ignore(0xffff, '\'');
        std::string sData;
        char ch;
        while (ifstr.good() && ('\'' != (ch=ifstr.get())))
            sData += ch;
        if (!sData.empty())
            vData.push_back(sData);
    }
    for (size_t i=0; i<vData.size(); ++i)
        std::cout << vData[i] << std::endl;

    return 0;
}

Test.dat

'Tom Smith' 'Jackie Brown' 'John Doe' 
'Robert Burns'   'James Joyce''Joseph Conrad' 'Dylan Thomas'
'Edgar Allan Poe' 'V.S. Naipaul' 'Vladimir Nabokov'
'William Shakespeare'  'William Langland' 'Robert Greene'

結果

Tom Smith
Jackie Brown
John Doe
Robert Burns
James Joyce
Joseph Conrad
Dylan Thomas
Edgar Allan Poe
V.S. Naipaul
Vladimir Nabokov
William Shakespeare
William Langland
Robert Greene
于 2011-03-19T11:40:04.500 に答える
0

以下は抜粋ですfileRead。行のみを変更しました(1)。この変更により、私が見る限り、コードは期待どおりに動作するようです。

do{
  ....
  std::istringstream myStream( line );

  while ( myStream >> test ) // (1)
    results.push_back( test );
} 
while ( file.get() != '\n' )

お役に立てれば

于 2011-03-19T13:52:27.337 に答える