次のような .txt パラメータ ファイルがあります。
#Stage
filename = "a.txt";
...
#Stage
filename = "b.txt";
...
基本的にはパラメータファイルにアクセスするたびに1ステージ読み込みたい。これを行うには、区切り文字「#Stage」を使用して C++ で getline を使用する予定でした。または、これを解決するためのより良い方法はありますか? サンプルコードは役に立ちます。
もっとハッキリ表現したほうがいいのかな。とにかく、私はこのように管理します:
ifstream file1;
file1.open(parfile, ios::binary);
if (!file1) {
cout<<"Error opening file"<<parfile<<"for read"<<endl;
exit(1);
}
std::istreambuf_iterator<char> eos;
std::string s(std::istreambuf_iterator<char>(file1), eos);
unsigned int block_begin = 0;
unsigned int block_end = string::npos;
for (unsigned int i=0; i<stage; i++) {
if(s.find("#STAGE", block_begin)!=string::npos) {
block_begin = s.find("#STAGE", block_begin);
}
}
if(s.find("#STAGE", block_begin)!=string::npos) {
block_end = s.find("#STAGE", block_begin);
}
string block = s.substr(block_begin, block_end);
stringstream ss(block);
....
*struct content{
DATA data;
content* next;
};
struct List{
content * head;
};
static List * list;
char buf[100];
FILE * f = fopen(filename);
while(NULL != fgets(buf,f))
{
char str[100] ={0};
sccanf(buf,"%s",str);
if(strcmp("#Stage",str) == 0)
{
// read content and add to list
cnntent * p = new content();
list->add();
}
else
{
//update content in last node
list->last().data =
}
}*
(または、フォーマット/目標に応じて#
content を含む行) から始まる行を無視して、行ごとに読みます(バージョンがないため、区切り文字として使用します)。#Stage
getline
std::string