好啦 那不重要 總之這禮拜繼續上課
第一節課 讀取檔案
///printf_fprintf
#include<stdio.h>
int main()
{///檔案指標fout=開檔名("檔案類型",用甚麼模式)w=write
FILE*fout=fopen("file.txt","w");
printf("Hello World\n");
fprintf(fout,"Hello World在檔案裡\n");
}
新建的空白檔案
跑程式之後 桌面會跑出一個file的文字檔案 裡面會寫你打過的字
1-2 讀檔
///scanf fscanf
#include<stdio.h>
int main()
{
FILE*fin=fopen("file.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("從flie.txt讀到了:%s\n",line);
fscanf(fin,"%s",line);
printf("從flie.txt讀到了:%s\n",line);
}
可以讀出file.txt裡面的文字內容 但是不知道為啥會分成兩行 所以是個廢東西
1-3 合併使用
///scanf fscanf printf_fprintf
#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,b[i]);
}
fclose(fin);
}
混合應用 製作一個內容10 20 30 40 50 60 70 80 90 100 的文字檔
然後能讀出來
第一節課結束!!!
第二節課
一直出錯 反正最後修好了
///week12-2_keyboard_mouse
#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("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();
} ///ESC就會結束
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
總之就是可以用滑鼠移動東西
阿檔案因為存在奇怪的地方 所以要改個位置
第三節講解功課 但因為有夠多 所以改成下禮拜交~
差不多就醬 回家~
沒有留言:
張貼留言