課堂內容 : fprintf()、fscanf() 寫檔和讀檔;fprintf()、fscanf() + project
1.寫檔
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt", "w+");
printf("Hello World\n");
fprintf(fout,"Hello World\n");
fclose(fout);
}
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt", "w+");
printf("Hello World\n");
fprintf(fout,"Hello World\n");
fclose(fout);
}
2.讀檔
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt", "w+");
fprintf(fout,"3.1415926\n");
fclose(fout);
float angle=0;
FILE * fin = fopen("file.txt", "r");
fscanf(fin, "%f", &angle );
printf("角度:%f", angle);
fclose(fin);
}
3.加入上週的程式
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldx=0;
int angleID=0;
FILE*fout=NULL;
void mywrite()
{
if(fout == NULL) fout=fopen("file.txt", "w+");
for(int i=0 ; i<20 ; i++)
{
printf("%.1f\n", angle[i]);
fprintf(fout, "%.1f", 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();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.7,-0.4,0);
glColor3f(0,1,0);
glRectf(0.7,0.5,1.0,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);
glRotatef(-angle[2],0,0,1);
glTranslatef(0.3,0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,-0.5,-0.7,-0.3);
glPushMatrix();
glTranslatef(-0.7,-0.4,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(0.7,0.4,0);
glColor3f(0,1,0);
glRectf(-0.7,-0.5,-1.0,-0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
最後可以做成動畫 :
#include <GL/glut.h>
#include <stdio.h>
float angle[20] , oldx=0;
int angleID=0;
FILE*fout=NULL, *fin=NULL;
void myWrite(){
if( fout == NULL) fout = fopen("file.txt", "w+");
for(int i=0; i<20; i++)
{
printf("%.1f ", angle[i] );
fprintf(fout, "%.1f ", angle[i] );
}
printf("\n");
fprintf(fout,"\n");
}
void myRead()
{
if( fout != NULL ) fclose(fout); fout=NULL;
if( fin == NULL ) fin = fopen("file.txt", "r");
for(int i=0; i<20; i++)
{
fscanf(fin, "%f",&angle[i] );
}
glutPostRedisplay();
}
void keyboard(unsigned char key , int x , int y)
{
if (key=='r') myRead();
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();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.7,-0.4,0);
glColor3f(0,1,0);
glRectf(0.7,0.5,1.0,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0.7,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.7,0.5,-1.0,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week14");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
按 r 可以重複剛剛的動作




沒有留言:
張貼留言