寫檔、讀檔
1.練習開檔、寫檔
-> 不用開專案,直接開新檔
-> 打程式碼
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt","w+");
printf("Hello World\n");
fprintf(fout,"Hello World\n");
fclose(fout);
}
3.開檔、寫檔 , 最後關檔
-> 把上週的week_rect_many_TRT拿來繼續寫
-> 開新的GLUT專案,把上週的程式貼上
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0; ///0:第0個關節,1:第1個關節
FILE * fout = NULL;
void my Write()
{
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
fprintf(fout,"%.2f",angle[i]);
}
}
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;
}
void mouse(int button,int state,int x,int y)
{
oldX = x ;
}
void motion(int x,int y)
{
angle[angleID] += (x-oldX);
myWrite();
oldX = x;
glutPostRedisplay();
}
#include <math.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);
glPushMatrix();
glTranslatef(-0.8,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(+0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.8,0.5,-1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit( &argc , argv );
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Homework14_TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
-> file f為檔案 , 建立一個file.txt 記事本 , w+新增
-> printf 在小黑視窗印出內容
-> fprintf 在記事本上印出內容
4. r 讀檔
-> 按下r 來一行一行讀入剛剛錄下來的動作
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0; ///0:第0個關節,1:第1個關節
FILE * fout = NULL , * fin=NULL;
void myWrite()
{
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf( "%.2f",angle[i]);
fprintf( fout,"%.2f",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void myRead()
{
if(fout != NULL) fclose(fout); ///還在讀的檔案要關掉
if(fin==NULL) fin = fopen("file.txt","r");///打開記事本才能讀檔
for(int i=0;i<20;i++){
fscanf("%f",&angle[i]);
}
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;
if(key=='r') {
myRead();
}
}
void mouse(int button,int state,int x,int y)
{
oldX = x ;
}
void motion(int x,int y)
{
angle[angleID] += (x-oldX);
myWrite();
oldX = x;
glutPostRedisplay();
}
#include <math.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);
glPushMatrix();
glTranslatef(-0.8,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(+0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.8,0.5,-1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit( &argc , argv );
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Homework14_TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
5. timer控制
-> 實作鬧鐘
#include <GL/glut.h>
#include <stdio.h>
void timer (int t) ///t的單位是ms
{
///1000代表1秒 1500代表1.5秒
printf("鬧鐘%d,我起床了\n",t);
printf("設定下一個鬧鐘\n");
glutTimerFunc(2000 , timer , t+1); ///2秒後跳一次
printf("設好鬧鐘,再回去睡\n");
}
void display()
{
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14 timer");
glutTimerFunc(3000,timer,0);///3秒後叫timer
glutDisplayFunc(display);
glutMainLoop();
}


沒有留言:
張貼留言