week12
練習寫檔案(fprintf)
week12-1
*讀出Hello World
f 是檔案指標
f 是檔案指標
檔案指標 fout = fopen("檔名", "w") 就會寫檔出去
#include <stdio.h>
int main(){
FILE * fout = fopen("file.txt", "w");
printf("Hello World\n");
fprintf(fout, "Hello World在檔案裡\n");
}
int main(){
FILE * fout = fopen("file.txt", "w");
printf("Hello World\n");
fprintf(fout, "Hello World在檔案裡\n");
}
讀入檔案(fscanf)
week12-2
* 檔案指標 fin = fopen("檔名", "r") 把檔案讀進來
#include <stdio.h>
int main(){
char line[20];
///scanf("%s", line);
FILE * fin = fopen("file.txt", "r");
fscanf(fin, "%s", line);
printf("讀到了:%s\n", line);
fscanf(fin, "%s", line);
printf("讀到了:%s\n", line);
}
在同一程式中有寫檔案&讀檔案
week12-3
#include <stdio.h>
int main(){
int a[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
FILE * fout = fopen("file3.txt", "w");
for(int i=0; i<10; i++){
fprintf(fout, "%d ", a[i]);
printf("%d ", a[i]);
}
fclose(fout);
int b[10];
FILE * fin = fopen("file3.txt", "r");
for(int i = 0; i < 10; i++){
fscanf(fin, "%d", &b[i]);
printf("%d ", b[i]);
}
fclose(fin);
}
mouse
week12-4
* 開啟新的glut專案
用 mouse來移動
用 mouse來移動
#include <stdio.h>
#include <GL/glut.h>
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)
{
teapotX = (x-150)/150.0;
teapotY = (150-y)/150.0;
display();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
keyboard
week12-5
* 用 keyboard來觸發讀檔
#include <stdio.h>
#include <GL/glut.h>
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
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)
{
teapotX = (x-150)/150.0;
teapotY = (150-y)/150.0;
if(state==GLUT_DOWN){
if(fout==NULL) fout = fopen("file4.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);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
調整工作執行目錄
* 先改位址
到四格顏色按右鍵選Properties,或到Project-Properties即可更改
到Build targets 把Debug&Release的位址都改成"."
* 將目錄拉回
到freeflut的bin資料夾把檔案拉回week12-4資料夾
* 在同目錄裡寫期末作業
複製一個week12-4資料夾並改名成Final Project
將專案檔(.cbp)也改名
把Project-Properties的title 也改名並save everything
再丟入github
如何github
1. 開啟 Git Bash
2. cd desktop 進入桌面
git clone https://github.com/jsyeh/2021cce2 把雲端複製下來
cd 2021cce2 進入你的目錄
3. 把你的 桌面\2021cce2 裡面, 放好你本週的程式 ex. week06 裡面有今天的程式
4. 先把你放好的程式,加入 git 的帳冊
git status (看到紅紅的檔案)
git add .
git status (看到綠綠的檔案)
5. 你要 commit 它
git config --global user.email jsyeh@mail.mcu.edu.tw
git config --global user.name jsyeh
git commit -m "add week06"
6. git push 推送上雲端








沒有留言:
張貼留言