2022年5月23日 星期一

RH的學習筆記٩(ˊᗜˋ*)وweek14

練習寫檔

練習1 練習開檔、寫檔write、關檔

1.開啟檔案

FILE * fout=fopen("file.txt","w+");,write+ 順便新增

2.印出來、寫出來  

printf("Hello World\n");    fprintf(fout,"Hello World\n");

3.關閉檔案

 fclose(fout);
printf 和 fprintf 的差別,printf 是在小黑視窗印出hello world,fprintf 則是在記事本顯示
程式碼如下:
```c
#include <stdio.h>
int main()
{
    FILE * fout=fopen("file.txt","w+");

    printf("Hello World\n");
   fprintf(fout,"Hello World\n");

   fclose(fout);
}
```


練習2 練習開檔、讀檔read、關檔

```c
#include <stdio.h>
int main()
{
    FILE * fout=fopen("file2.txt","w+");    "w"=write 寫檔
    fprintf(fout,"angle1 %d\n",999);    ///將數字印到記事本
    fclose(fout);

    char line[200];
    int a;
    FILE * fin = fopen("file2.txt", "r");    "r"=read 讀檔
    fscanf(fin, "%s %d",line,&a);
    printf("讀到了字串:%s 及整數%d\n",line,a);
    fclose(fin);
}
```

把上週寫的關節TRT程式碼拿來改寫

1.新增myWrite()函式,新增指標用來開檔、讀檔,最後在motion函式裡加入myWrite();

程式碼如下:
標頭碼 : #include <stdio.h>
呼叫空指標 : FILE * fout = NULL;
新增函式 : 
```c
void myWrite()
{
    if(fout==NULL)fout=fopen("file.txt","w+");
    for(int i=0;i<20;i++)
    {
        fprintf(fout,"%.2f",angle[i]);
    }
}
```
motion內加入myWrite() :
```c
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    myWrite();
    oldX=x;
    glutPostRedisplay();
}
```

補充

在第10行加入 printf(     "%.2f",angle[i]); 以便在小黑視窗顯示關節移動的數據
在最下方的Log裡可以得知檔案開啟的位置在 C:\Users\XUSER\Desktop\freeglut\bin 裡


2.寫讀檔和重來

先新增一個指標 fin
* fin = NULL;
讀檔函式myRead()
```c
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(); ///重畫畫面
}
```
在keyboard()函式裡加入
if(key=='r'){///在鍵盤上按下"r"鍵時,重畫
        myRead();
 }
按r後

修正工作目錄

1.先到week14_TRT_angle_write_and_read的專案裡,找到.cbp檔用Notepad++打開後修改程式碼,把<Option working_dir=" " />後面的" "改成".",存檔;最後將freeglut\bin裡的freeglut.dll複製到你的專案week_TRT_angle_write_and_read的bin裡

計時器

1.寫計時器程式碼
程式碼如下:
```c
#include <GL/glut.h>
#include <stdio.h>;
void timer(int t){///t的單位是ms
    ///1000代表1秒、1500代表1.5秒
    printf("鬧鐘%d,起床了\n",t);
    printf("設定下一個鬧鐘");
    glutTimerFunc(1000,timer,t+1);
    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();
}

```

2.加入音效 DO
程式碼如下:
```c
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void timer(int t){///t的單位是ms
    ///1000代表1秒、1500代表1.5秒
    printf("鬧鐘%d,起床了\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    printf("設定下一個鬧鐘");
    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();
}
```




沒有留言:

張貼留言