今天教檔案讀寫
先試著開檔,寫檔
fopen("檔案 ","模式 ")
打上程式碼:
如果想要在程式中又讀又寫,要用fopen配上fclose把程式關起來讓後面的檔案繼續做事開新專案
#include <stdio.h>
#include <GL/glut.h>
FILE * fout =NULL;
FILE * fin = NULL;
float teapotX=0,teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x ,int y)
{
if(state==GLUT_DOWN){
teapotX = (x-150)/150.0;
teapotY=(150-y)/150.0;
if(fout==NULL) fout = fopen("flie4.txt","w");
fprintf(fout,"%f %f\n",teapotX,teapotY);
}
display();
}
void keyboard(unsigned char key,int x,int y )
{
if(fin==NULL){
fclose(fout);
fin = fopen("file4.txt","r");
}
fscanf(fin,"%f %f",&teapotX,&teapotY);
display();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言