2023年5月3日 星期三

DTang---week12_檔案FILE

topic 1--檔案讀寫
1.寫: 
新增一個空白專案命名為week12-1_printf_fprintf.cpp


#include <stdio.h>

int main(){

    FILE * fout = fopen("file.txt", "w");

    printf("Hello World\n");
    fprintf(fout,"Hello World in the File\n");
}
執行後除了能在CMD裡看到printf的字之外
系統也新增了一個名為file的txt檔,內容為fprintf裡的內容



2.讀:
新增一個空白專案命名為week12-2_scanf_fscanf.cpp

///此為"讀"檔案
#include <stdio.h>

int main(){

    FILE * fin = fopen("file.txt", "r"); ///r表示read

    char line[100];
    fscanf(fin,"%s",line);
    printf("從檔案裡讀到:%s\n",line);

    fscanf(fin,"%s",line);
    printf("從檔案裡讀到:%s\n",line);
}

為什麼需要分兩行/兩次讀呢?
%s讀到空白值時會停止讀取
故在此重複加上第二個fscanf的程式碼



3.讀+寫:
新增一個空白專案命名為week12-1_scanf_printf_fscanf_fprintf.cpp
///week12-3
#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"); ///w表示write
    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"); ///r表示read
    for(int i=0; i<10;i++){
        fscanf(fin, "%d ", &b[i]);
        printf("b%d:%d ", i, b[i]);
    }
    fclose(fin);
}


---------------------------------------------------------------------
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();
}




加入前一節的讀寫程式碼與鍵盤操控的程式碼

FILE * fout = NULL;
FILE * fin = NULL;
.........
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/n", &teapotx, &teapoty);
    display();
}

int main(int argc, char** argv){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week12");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}

此專案生成的file檔位於專案的工作執行目錄
下列路徑能夠修改儲存位置
codeblock--> project--> properties--> build target--> Execute wroking dir
找到工作執行目錄後,修改儲存位置到桌面 ==> .
但是因為專案需要freeglut.dll 這個執行檔方能執行
所以需要將該檔案複製貼在執行專案裡
執行後就會發現,File4檔案就會在專案裡生成


codeBlock存檔有分兩種,一種存只在.cpp等程式執行檔的
另一種就是能夠存檔全部,包含程式執行檔內容與設定-->save everythings































沒有留言:

張貼留言