0

次のようなテキストファイルがあります。

8284734746
pat monahan 
8284735406
pat monahan
8277059735
rolling stones black keys 
rolling stonesnprudential center newark njn121512 
8277167638
keith richards 
prudential centernnewark njn15dec2012 

したがって、次の行に ID とフレーズ、または次の行に ID と 2 つのフレーズがあることに気付いた場合。私の目標は、ID を 1 つのテキスト ファイルに、(その ID に属する) テキストを 1 行にまとめて、別のテキスト ファイルとして保存することです。そのため、テキスト ファイル内の文字列を条件付きで連結する必要があります。

私がこれまでに持っているコードはこれを達成していません。

#include <iostream>
#include <fstream>
#include <string.h>
#include <sstream>
#include <iterator>

using namespace std;

bool is_number(const std::string& s)
{
   return( strspn( s.c_str(), "0123456789" ) == s.size() );
}       

int main()
{
   ifstream inputFile("title_desc_ids.txt");

     string line;
      vector <string> ids;
      vector <string> desc;

      while (getline(inputFile, line))
      {
          if (is_number(line))
          {
              ids.push_back(line);
          }   
          else
              desc.push_back(line);
      }       

      string full_file_name = "./title_desc_ids_all.txt";
      string full_file_name_desc = "./title_desc_combined.txt";
      ofstream output_file(full_file_name.c_str());
      ofstream output_file1(full_file_name_desc.c_str());
      copy(ids.begin(), ids.end(), ostream_iterator<string>(output_file, "\n"));
      copy(desc.begin(), desc.end(), ostream_iterator<string>(output_file1, "\n"));
  }

誰かが私を正しい方向に向けることができますか?

ありがとう!

4

1 に答える 1

0

入力した ID を追跡する必要があります。その後、インデックスが一致します。

int IDCnt = 0;

while (getline(inputFile, line))
{
    if (is_number(line))
    {
        ids.push_back(line);
        IDCnt++;
    }   
    else {
        if(IDCnt > desc.size()) desc.push_back(line);
        else desc[IDCnt-1] = desc[IDCnt-1] + line;
    }

}
于 2013-09-09T19:55:46.253 に答える