c++ - How to populate file attributes from a directory into a struct vector? -
i'm brand new c++ , i'm trying learn on own, i'm running lot of simple issues. right want @ directory, containing files end in ".img", , push file name , creation time struct vector.
how push_back vector work? each struct pushed populate vector or possible make mistake , overwriting previous push_back? don't know if i'm on thinking this...
could please take @ code , let me know if i'm doing correctly or may need make changes? thank much!
void filtercontextbyhsitime(const char *context_path, int hsi_create_time) { struct contextfilestruct { //define struct containing key elements of file double createtime; string filename; }; std::vector<contextfilestruct> contextvector; //initialize resizable vector array of type contextfilestruct contextfilestruct tempstruct; _finddata_t allfiles; //structure holds file specific elements int findfirstcontextimage = _findfirst(context_path, &allfiles); //int variable holding result of findfirst function //where -1 = no image fround , other number = image found tempstruct.filename = allfiles.name; tempstruct.createtime = allfiles.time_create; contextvector.push_back(tempstruct); if (findfirstcontextimage != -1 ) { int findnextcontextimage = 0; //iterator next context image in dir while (findnextcontextimage != -1) { findnextcontextimage = _findnext(findfirstcontextimage, &allfiles); tempstruct.filename = allfiles.name; tempstruct.createtime = allfiles.time_create; contextvector.push_back(tempstruct); } _findclose(findfirstcontextimage); //close findfirst file function prevent memory leaks } else { cout << "there no .img files in directory!" << endl; //error handling when there no context files } }
i looking @ incorrectly in debugger. instead of reviewing populating of vector, looking @ struct populating , adding next file, looked if overwriting. silly error on part. code working intended, thanks!
Comments
Post a Comment