0

以下のような行を含むファイルがあります。

25 1 0 0 0 0
27 1 0 0 0 0
20 0 0 0 0 0
32 1 0 0 0 0
23 1 0 0 0 0
16 0 0 0 0 0
28 1 0 0 0 0

まず、value = 1の各行を、文字列配列の要素として2番目の列に格納します。保存された要素をforループを介して呼び出し、要素の内容を整数に解析します。C ++でそれをどのように行うのですか?

#include <vector> 
#include <iostream>
#include <string>
using namespace std;
   .....
   .....
   .....
   .....
char line[100];
vector<string> vec_line;
string Vline;
int trimVal, compa, nil0, nil1, nil2, nil3;
  ......
  ......
  ......
//then store lines as strings in vec_line as required by the premise above
// after that, retrieve the contents of the elements of the vector
for (int iline=0; iline<vec_line.size(); iline++) {
   cout<<"iline  "<<iline<<"  vec_line[iline]";
   //then I want to parse contents of each string element and store them integer formats
   sscanf(vec_line[iline], "%d %d %d %d %d %d", &trimVal, &compa, &nil0, &nil1, &nil2, &nil3); //how would one do this simple parsing task in c and c++?
}

ありがとうございました。

4

4 に答える 4

2

sscanfはC関数です。すでにC++ストリームを使用して出力している(そしておそらくファイルから読み取っているのですか?)のでstringstream、文字列とデータ型の間の変換にはaを使用する方がよいでしょう。

例えば:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
   stringstream sstr;
   string mystr = "123";
   int i;

   sstr << mystr;

   sstr >> i;

   cout << i << endl;

   return 0;
}

123stdoutに出力します。もちろんifstream、演算子を使用して>>直接intまたはに読み取ることもできdoubleます。

お役に立てれば。

于 2012-08-31T13:07:22.457 に答える
1

これは正しくありません:

sscanf(vec_line[iline], "%d %d ...

の最初の引数sscanf()はaであり、ではconst char*ありませんstd::string。への変更:

sscanf(vec_line[iline].c_str(), "%d %d ...

の戻り値をチェックして、sscanf()予想されるすべての割り当てが行われたことを確認することをお勧めします。

if (6 == sscanf(vec_line[iline].c_str(), "%d %d ...
于 2012-08-31T12:56:36.597 に答える
1

各行を文字列として格納する必要がない限り、次のようなことを行います。

struct row { 
    static const int columns = 6;
    std::vector<int> column_data;

    // should we keep or ignore a row? Ignore if column 1 != 1.
    struct filter {
        bool operator()(row const &r) { return r.column_data[1] != 1; }
    };

    // read in a row.
    friend std::istream &operator>>(std::istream &is, row &r) {
        r.column_data.resize(row::columns);
        for (int i=0; i<row::columns; i++)
            is >> r.columns[i];
        return is;
    }

    // write a row out to a file.
    friend std::ostream &operator<<(std::ostream &os, row const &r) { 
        std::copy(r.column_data.begin(), r.column_data.end(),
                  std::ostream_iterator<int>(os, " "));
        return os;
};

次に、データを読み取って表示すると、次のようになります。

std::vector<row> data;

std::remove_copy_if(std::istream_iterator<row>(input_file),
                    std::istream_iterator<row>(),
                    std::back_inserter(data),
                    row::filter());

std::copy(data.begin(), data.end(), 
          std::ostream_iterator<row>(output_file, "\n"));

または、正しい行を入力ファイルから出力ファイルにコピーするだけの場合は、正しいイテレーターを使用して直接行うことができます。

std::remove_copy_if(std::istream_iterator<row>(input_file),
                    std::istream_iterator<row>(),
                    std::ostream_iterator<row>(output_file),
                    row::filter());
于 2012-08-31T13:26:17.630 に答える
0

常にlexとyacc(またはFlexBison)があります

于 2012-08-31T14:16:20.840 に答える