Week12
檔案File
新增一個空檔案 名稱更為week12-1_printf_fprintf.cpp
打上程式碼
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt", "w");
printf("Hello World\n");
fprintf(fout, "Hello World在檔案裡\n");
}
新增一個空檔案 名稱更為week12-2_scanf_fscanf.cpp
打上程式碼
#include <stdio.h>
int main()
{
FILE * fin = fopen("file.txt", "r");
char line[100];
fscanf(fin, "%s", line);
printf("從file.txt讀到了: %s\n", line);
fscanf(fin, "%s", line);
printf("從file.txt讀到了: %s\n", line);
}
重複兩次一樣程式碼是因為字和字之間有空格 它不會讀到空格後的東西
新增一個空檔案 名稱更為week12-3_manynumbers_scanf_printf_fscanf_fprintf.cpp
打上程式碼
#include <stdio.h>
int main()
{
int a[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int b[10] = {};
FILE * fout = fopen("file3.txt", "w");
for(int i=0;i<10;i++){
printf("%d ", a[i]);
fprintf(fout, "%d ", a[i]);
}
printf("\n");
fprintf(fout, "\n");
fclose(fout);
FILE * fin = fopen("file3.txt", "r");
for(int i=0;i<10;i++){
fscanf(fin, "%d", &b[i]);
printf("b%d:%d ", i, a[i]);
}
fclose(fin);
}
fclose是關掉檔案 因為不能同時讀入讀出同一檔案
所以要先關掉再打開
鍵盤、滑鼠
新增一個GLUT專案,名稱更為week12-4_keyboard_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)
{
if(state==GLUT_DOWN){
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();
}
執行後 只要滑鼠點哪裡 茶壺就會在哪裡
因為它的目錄在freeglut資料夾裡的bin資料夾
不在專案裡很麻煩
2.Project > Properties > Build targets > Release > Execution working dir
裡面的目錄改成 " . "
執行後用滑鼠點出茶壺路徑 再按鍵盤就會重複剛才 ( 滑鼠 ) 的動作










沒有留言:
張貼留言