0

我有一个这样的 .txt 参数文件:

#Stage
filename = "a.txt";
...

#Stage
filename = "b.txt";
...

基本上我想在每次访问参数文件时读取一个阶段。我打算在 C++ 中使用带有分隔符“#Stage”的 getline 来执行此操作。或者有更好的方法来解决这个问题?任何示例代码都会有所帮助。

4

3 回答 3

0
*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 = 
    }
}*
于 2013-04-30T09:33:02.070 回答
0

也许我应该表达得更清楚些。无论如何,我是这样管理的:

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);
....
于 2013-05-02T10:34:09.993 回答
-1

我会逐行阅读,忽略以#(或以 content 开头的行#Stage,取决于格式/目标)(因为没有getline版本,std::string作为分隔符)。

于 2013-04-30T09:13:14.017 回答