次のようなテキストファイルがあります。
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"));
}
誰かが私を正しい方向に向けることができますか?
ありがとう!