c++ - Why isn't anything being written to my file? -
i'm making program opencv , want record x,y coordinates of 2 objects i'm tracking. i've retrieved data fine , using display coordinates on-screen, i've been struggling write log file.
i have function pass relevant data to, , sure program reaching function because file being created.
can please tell me why "test.txt"
empty? thank in advance.
here code:
void savedata(int leftx, int lefty, int rightx, int righty, int distance){ logfile.open("test.txt"); //timer time_t start = time(0); //set initial time point //counter int counter = 0; //string coordinates //string coordinates string coords = "left x: " + inttostring(leftx) + " lefty: " + inttostring(lefty) + " right x: " + inttostring(rightx) + " right y: " + inttostring(righty) + " distance between: " + inttostring(distance) + "\n"; //if 1 minute has passed if (start - time(0) == 60){ //write coordinates log file logfile << coords; //increment counter counter++; //after 30 mins of recording exit program if (counter == 30){ //close log file logfile.close(); //exit no errors exit(0); } //reset time 0 start = time(0); } }
in c , c++ variables created in stack (aka non pointer variables) confined scope. suppose function called inside external loop. counter not seen loop. same start.
moreover there logical issues in code:
- start-time(0)==60 tested once. time(0) greater start.
- counter increment 1 (and won't since start-time(0)<0) since isn't inside loop, in if clause.
i can suggest pseudocode based on i've intended wanna code posted there:
void savedata(data){ open file setup start=time(0) , counter=0 format output string while(counter<=30){ if((int)(time(0)-start)%60==0){ //seconds_passed=60*n, n integer write data counter++ } } close file }
have fun
gf
edit: if call function external infinite loop have pass counter reference (so increments inside function seen outside) , start argument.
Comments
Post a Comment