2016年1月15日 星期五

openFrameworks: Reading From And Writing To Files With ofBuffer Class

since: 2016/01/15
update: 2016/01/15
reference:

A. in header file:
    ....
    /* config file */

    bool debug; // debug mode: to show more message or not
    float checkTime_second;
   
    /* record datetime to file */
    ofBuffer readBuffer;
    ofBuffer checkTimeBuffer;
    string nowDateString;


-----------------------------------------------------------------------------------------------

B. in c++ file:
     1. Read File:
   
    ....

    // read config file
    readBuffer = ofBufferFromFile("config.txt");
    //cout << readBuffer.getText(); // let's see what it says
    vector<string> lines = ofSplitString(readBuffer, "\n");
   
    for( int i = 0; i < lines.size(); i++){
        //cout << lines[i] << endl;
       
        if(i == 1) {
            debug = ofToBool(lines[i]);
            if(debug) {
                ofLog(OF_LOG_NOTICE, ">>>>> debug true");
            }
            else {
                ofLog(OF_LOG_NOTICE, ">>>>> debug false");
            }
        }
        else if(i == 4) {
            checkTime_second = ofToFloat(lines[i]);
            ofLogNotice(">>>>>>>>> checkTime_second") << checkTime_second;
        }

   

     2. Write File:
         ....

         // inner some loop
         if(ofGetElapsedTimef() >= checkTime_second) {
             ofResetElapsedTimeCounter();
               
             nowDateString = ofGetTimestampString("%Y%m%d%H%M%S");
             checkTimeBuffer.set(nowDateString.c_str(), nowDateString.size());
             if(ofBufferToFile("dateTime.txt", checkTimeBuffer)) {
                 ofLogNotice(">>>>>>>>> update checking dateTime") << nowDateString;
             }
         }


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。