This is related to the answer to my previous question by Mats Petersson.
According to his method, I can successfully save the line in the header and print it out later whenever I want. However, I now want to save the whole header to print it later. I am using append() to fulfill that purpose. But it's not working.
Here is the code that is outside the two functions:
static map<string, string> headermap;
static char headerline[1024];
Here is the code for the reading function:
string paragraph;
for (int i=0; i<8; ++i) {//8 lines of the header
fgets(buffer,1024,fp);
if(buffer[0] == '#'){
paragraph = paragraph.append(buffer);
}
}
headermap[filename] = paragraph;
Here is the code for the writing function:
const char *headerline = headermap[filename].c_str();
fprintf(fp, headerline);
Previously I was using headerline[i] = buffer[i];
instead of paragraph.append(buffer);
in the reading function. And headermap[filename] = headerline;
instead of headermap[filename] = paragraph;
.
When I print, nothing is printed to the output file. As i said before, previously when I tried to print just one line, it works fine.