2018年6月2日 星期六

Week13 讀+寫檔

主題: 讀檔 / 寫檔 、 控制關節 、動作內插

(一)讀檔 / 寫檔
FILE * f=fopen("out.txt", "w+"); //讀檔
fprintf(f, "Hello Cat\n"); //寫檔
fclose(f); /* 關檔案  如果在這之前有中斷(像是scanf()),之前寫的data不會寫進檔案裡,
檔案大小為 0K */




(二)控制關節
在glut裡讀檔 讀進用滑鼠移動的旋轉角度 並寫檔

float angle=0, oldx;
FILE * fout=NULL;
void mouse(int  botton, int state, int x, int y){
    oldx=x;
}

void motion(int x, int y){
    angle+=x-oldx;
    oldx=x;
    if(fout==NULL){
        fout=fopen("output.txt", "w+");
    }
    fprintf(fout, "%.2f\n", angle);
    printf("%.2f\n", angle);
    glutPostRedisplay();
}


這可以讀上面的檔案 重複剛剛動作 按著keybroad就可以動
FILE * fin=NULL;
void keyboard(unsigned char key, int x, int y){
    if(fin==NULL){
        fin=fopen("output.txt", "r");
    }
    fscanf(fin, "%f", &angle);
    glutPostRedisplay();
}


沒有留言:

張貼留言