- 開啟 CodeBlocks 新建一個檔案(.cpp檔)
- 寫入程式:
- #include <stdio.h>
- int main()
- {
- FILE * fout=fopen("檔名.txt","w+");///write+有就寫檔,沒有就創一個
- printf("Hello world!\n");
- fprintf(fout,"Hello world in file.\n");
- }

- 試試 for 迴圈看看會變成怎樣
- #include <stdio.h>
- int main()
- {
- FILE * fout=fopen("檔名2.txt","w+");
- for(int i=0; i<20; i++){
- printf("Hello %d\n",i);
- fprintf(fout,"Hello %d\n",i);
- }
- }

- 練習開檔存檔
- 先將關節都準備好!!
- #include <GL/glut.h>
- int angle[10]={0,0,0,0,0, 0,0,0,0,0};
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);///白色
- glutSolidTeapot(0.2);///身體
- glPushMatrix();///右側
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);///紅色
- glutSolidTeapot(0.2);///右上手臂
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);///黃色
- glutSolidTeapot(0.2);///右下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glPushMatrix();///左側
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);///綠色
- glutSolidTeapot(0.2);///左上手臂
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);///藍色
- glutSolidTeapot(0.2);///左下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glutSwapBuffers();
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week14 file");
- glutDisplayFunc(display);
- glutMainLoop();
- }

- 加入上禮拜的(用鍵盤控制關節轉動)
- #include <GL/glut.h>
- int angle[10]={0,0,0,0,0, 0,0,0,0,0};
- int angleID=0;
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);///白色
- glutSolidTeapot(0.2);///身體
- glPushMatrix();///右側
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);///紅色
- glutSolidTeapot(0.2);///右上手臂
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);///黃色
- glutSolidTeapot(0.2);///右下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glPushMatrix();///左側
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);///綠色
- glutSolidTeapot(0.2);///左上手臂
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);///藍色
- glutSolidTeapot(0.2);///左下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glutSwapBuffers();
- }
- int oldX, oldY;
- void mouse(int button, int state, int x, int y)
- {
- oldX=x; oldY=y;
- }
- void motion(int x, int y)
- {
- angle[angleID] += (x-oldX);
- oldX = x;
- glutPostRedisplay();
- }
- void keyboard(unsigned char key, int x, int y)///按鍵盤數字控制關節
- {
- if(key=='0') angleID=0;
- if(key=='1') angleID=1;
- if(key=='2') angleID=2;
- if(key=='3') angleID=3;
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week14 file");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- glutMouseFunc(mouse);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- }
![]() |
| [0]右上手臂[1]右下手臂[2]左上手臂[3]左下手臂 |
- 將上面兩組程式碼合併
- #include <GL/glut.h>
- #include <stdio.h>
- FILE * fout=NULL;
- int angle[10]={0,0,0,0,0, 0,0,0,0,0};
- int angleID=0;
- void saveall()
- {
- if(fout==NULL) fout=fopen("motion.txt","w+");
- for(int i=0; i<4; i++){
- printf("%d ",angle[i]);
- fprintf(fout,"%d ",angle[i]);
- }
- printf("\n");
- fprintf(fout, "\n");
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);///白色
- glutSolidTeapot(0.2);///身體
- glPushMatrix();///右側
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);///紅色
- glutSolidTeapot(0.2);///右上手臂
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);///黃色
- glutSolidTeapot(0.2);///右下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glPushMatrix();///左側
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);///綠色
- glutSolidTeapot(0.2);///左上手臂
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);///藍色
- glutSolidTeapot(0.2);///左下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glutSwapBuffers();
- }
- int oldX, oldY;
- void mouse(int button, int state, int x, int y)
- {
- oldX=x; oldY=y;
- }
- void motion(int x, int y)
- {
- angle[angleID] += (x-oldX);
- oldX = x;
- saveall();
- glutPostRedisplay();
- }
- void keyboard(unsigned char key, int x, int y)///按鍵盤數字控制關節
- {
- if(key=='0') angleID=0;
- if(key=='1') angleID=1;
- if(key=='2') angleID=2;
- if(key=='3') angleID=3;
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week14 file");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- glutMouseFunc(mouse);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- }

- 在鍵盤函式裡增加讀檔程式
- #include <GL/glut.h>
- #include <stdio.h>
- FILE * fout=NULL;
- int angle[10]={0,0,0,0,0, 0,0,0,0,0};
- int angleID=0;
- void saveall()
- {
- if(fout==NULL) fout=fopen("motion.txt","w+");
- printf("fout:%d\n", fout);
- for(int i=0; i<10; i++){
- printf("%d ",angle[i]);
- fprintf(fout,"%d ",angle[i]);
- }
- printf("\n");
- fprintf(fout, "\n");
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);///白色
- glutSolidTeapot(0.2);///身體
- glPushMatrix();///右側
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);///紅色
- glutSolidTeapot(0.2);///右上手臂
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);///黃色
- glutSolidTeapot(0.2);///右下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glPushMatrix();///左側
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);///綠色
- glutSolidTeapot(0.2);///左上手臂
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);///藍色
- glutSolidTeapot(0.2);///左下手臂
- glPopMatrix();
- glPopMatrix();///右側
- glutSwapBuffers();
- }
- int oldX, oldY;
- void mouse(int button, int state, int x, int y)
- {
- oldX=x; oldY=y;
- }
- void motion(int x, int y)
- {
- angle[angleID] += (x-oldX);
- oldX = x;
- saveall();
- glutPostRedisplay();
- }
- FILE * fin=NULL;
- void keyboard(unsigned char key, int x, int y)///按鍵盤數字控制關節
- {
- if(key=='0') angleID=0;
- if(key=='1') angleID=1;
- if(key=='2') angleID=2;
- if(key=='3') angleID=3;
- if(key=='r'){
- if(fin==NULL) fin=fopen("motion.txt","r");
- for(int i=0; i<10; i++){
- fscanf(fin,"%d",&angle[i]);
- }
- glutPostRedisplay();
- }
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week14 file");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- glutMouseFunc(mouse);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- }
![]() |
| 長按"r" |
➽Notepad++小技巧
- include都要在最上面,避免出錯
- 變數宣告都在include下面,避免電腦找不到
- 函式可以在最上面先宣告一次,避免因為至順序而出錯
- 可以不用定義!!




沒有留言:
張貼留言