c++ - Reading multiple files, and keeping a set of data for each file. -
i want read file , save header in variable when rewriting(overwriting) file, can paste header , carry on printing rest of modified file. header, in case, not change can afford print out. here code inside class:
. . . static char headerline[1024]; static int read(const char* filename){ fget(var,...; (int i=0; i<1024; ++i){ headerline[i] = var[i]; } . . . } int write(filename){ fprintf(filename, headerline); //printing rest of file . . . }
the code prints line saved while reading file. however, problem saves header of file read last time. if have 2 files opened , want save first one, header of second file written first one. how can avoid that? if static map solution, that?
secondly, best way print whole header(5-8 lines) instead of 1 line doing now.
so, problem needs solving reading multiple files, , want keep set of data each file.
there many ways solve this. 1 of connect filename
header
. suggested in comment, using std::map<std::string, std::string>
1 way that.
static std::map<std::string, std::string> headermap; static int read(const char* filename){ static char headerline; fget(var,...; (int i=0; i<1024; ++i){ headerline[i] = var[i]; } headermap[std::string(filename)] = std::string(headerline); ... int write(filename){ const char *headerline = headermap[std::string(filename)].c_str(); fprintf(filename, headerline); // note printf based on above post - it's wrong, // i'm not sure actual code does.
Comments
Post a Comment