0

I have a .txt parameter file like this:

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

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

Basically I want to read one stage each time I access the parameter file. I planed to use getline in C++ with delimiter "#Stage" to do this. Or there is a better way to solve this? Any sample codes will be helpful.

colddie
  • 1,029
  • 1
  • 15
  • 28
  • "Or ther eis a better way to solve this?" - better in what way? If `#Stage` is the marker of the end of the file [or end of what you want to read], then that's what you have to do. – Mats Petersson Apr 30 '13 at 09:13
  • any code example: `istream& getline (istream& is, string& str, char delim);` , edit: the better way for this, is always a solid serializer - it amuses me how everyone is starting to read .txt or .csv's first instead of starting with dom/xml or whatever – Najzero Apr 30 '13 at 09:14

3 Answers3

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 = 
    }
}*
0

Maybe I should express more clear. Anyway, I manage like this:

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);
....
colddie
  • 1,029
  • 1
  • 15
  • 28
-1

I'd read line by line, ignoring the lines, starting with # (or the lines, with content #Stage, depending on the format/goal) (as there's no getline version, taking std::string as delimiter).

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187