WEEK_14寫檔/讀檔
codeblocks>file>Empty file>桌面 建新資料夾
1.寫檔
開啟檔案 fopen( "檔名", "模式" );
寫檔 fprintf( 指標 , "文件內容" );
關閉檔案 fclose( 指標 );
#include <stdio.h>
int main(){
///檔案指標fout 開啟檔案(檔名,write模式)
FILE*fout=fopen("file.text","w+");
printf( "Helloe World\n");
fprintf(fout,"Hellow World\n");
fclose(fout);
}
2.讀檔
fscanf(指標, "資料型態", &位置);
#include <stdio.h>
int main(){
///檔案指標fout 開啟檔案(檔名,write模式)
FILE * fout=fopen("file.text","w+");
fprintf(fout,"3.1415926\n");
fclose(fout);///關閉檔案
float angle=0;
FILE * fin=fopen("file.text","r");
fscanf(fin,"%f",&angle);
printf("讀到了角度:%f",angle);
fclose(fin);
}
3.開上週程式碼
week13_rect_many_TRT 修改使(小黑+txt)顯示多個關節的角度
///week13_rect_many_TRT
#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.text","w+");
for(int i=0;i<20;i++){
printf("%.1f",angle[i]);///小黑視窗印出來
fprintf(fout,"%.1f",angle[i]);///檔案印出來
}
printf("\n");
fprintf(fout,"\n");
}
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);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
4.利用重播:製作動畫
加上鍵盤控制>滑鼠先控制手臂動>程式記錄存成.txt>>持續按r>>>撥放
///week13_rect_many_TRT
#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.text","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()
{
.....
5.計時器&引入多媒體檔(聲音)
///week14_timer
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
void timer(int t){
printf("起床,現在時間: %d\n",t);
PlaySound("do.wav",NULL,SND_ASYNC);
glutTimerFunc(2000,timer,t+1);
}
void display(){
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14_timer");
glutTimerFunc(5000,timer,0);///設定每5秒後叫一次
glutDisplayFunc(display);
glutMainLoop();
}





沒有留言:
張貼留言