2021年5月31日 星期一

我不想出錢 Week15

主題: 存檔、讀檔





















程式碼:
///主題: 存檔、讀檔
#include <stdio.h> ///step01 外掛 printf(), fprintf(), fscanf(), fopen()

int main( int argc, char** argv )
{
    FILE * fout=NULL;///step01 檔案指標 file output 變數
    fout = fopen("檔名.txt", "w+");///step01 開啟檔案,檔名.txt, 模式write

    printf("Hello World\n");
    fprintf(fout, "Hello World\n");///step01 改裝 printf() 變 fprintf()
}





















程式不改,只改CodeBlocks Project cbp檔( 用notepad++開 ),裡面的working_dir 改成小數點,看到目錄會改變






















目錄改變了!!






















按comment註解掉程式






















程式碼被註解掉了






















3.3. 
程式碼:
///主題: 存檔、讀檔
#include <stdio.h> ///step01 外掛 printf(), fprintf(), fscanf(), fopen()

int main( int argc, char** argv )
{
///    FILE * fout=NULL;///step01 檔案指標 file output 變數
///    fout = fopen("檔名.txt", "w+");///step01 開啟檔案,檔名.txt, 模式write
///
///    printf("Hello World\n");
///    fprintf(fout, "Hello World\n");///step01 改裝 printf() 變 fprintf()
///先把舊程式 註解 掉
    FILE * fin = NULL;///step03 改成讀檔
    fin = fopen("檔名.txt", "r"); ///step03 開啟step01/step02的檔案,Read

  ///scanf(  "%d", &n ); //int n
  ///scanf(  "%c", &c ); //char c
  ///scanf(  "%f", &f ); //float f
  ///scanf(  "%s", line ); //char line[100];
    char line[100]; ///step03 宣告一個字串 line
    fscanf(fin, "%s", line); ///step03 讀入一個字串
     printf("現在讀到的是 %s \n", line );///step03 順便印出字串

    fscanf(fin, "%s", line); ///step03 讀入一個字串
     printf("現在讀到的是 %s \n", line );///step03 順便印出字串
}

備份的程式!! 要刪掉了!! 以後會用!!👇
😏😏😏😏😏😏😏😏
///主題: 存檔、讀檔
#include <stdio.h> ///step01 外掛 printf(), fprintf(), fscanf(), fopen()

int main( int argc, char** argv )
{
///    FILE * fout=NULL;///step01 檔案指標 file output 變數
///    fout = fopen("檔名.txt", "w+");///step01 開啟檔案,檔名.txt, 模式write
///
///    printf("Hello World\n");
///    fprintf(fout, "Hello World\n");///step01 改裝 printf() 變 fprintf()
///先把舊程式 註解 掉
    FILE * fin = NULL;///step03 改成讀檔
    fin = fopen("檔名.txt", "r"); ///step03 開啟step01/step02的檔案,Read

  ///scanf(  "%d", &n ); //int n
  ///scanf(  "%c", &c ); //char c
  ///scanf(  "%f", &f ); //float f
  ///scanf(  "%s", line ); //char line[100];
    char line[100]; ///step03 宣告一個字串 line
    fscanf(fin, "%s", line); ///step03 讀入一個字串
     printf("現在讀到的是 %s \n", line );///step03 順便印出字串

    fscanf(fin, "%s", line); ///step03 讀入一個字串
     printf("現在讀到的是 %s \n", line );///step03 順便印出字串
}
😏😏😏😏😏😏😏😏






















把bin裡的 freeglut.dll 複製到 week15_file 檔案夾裡






















新程式碼跑出茶壺!
程式碼:
#include <stdio.h>
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week15 many angles");

    glutDisplayFunc(display);
    glutMainLoop();
}///先不放貼圖, 先不放打光






















程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle=0, diff=2; ///上週程式,會增加、會減少!!!
///float angle[20]={};///都是0的陣列 angle
void timer(int t)///上週程式,會增加
{
    glutTimerFunc(30, timer, t+1);///上週程式 設定新的timer
    angle += diff; ///上週程式,會增加、會減少!!!
    if(angle>90) diff = -2;///上週程式,會增加、會減少!!!
    if(angle<0) diff = +2;///上週程式,會增加、會減少!!!
    glutPostRedisplay();///上週教display();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glutSolidTeapot(0.3);
        glutSolidTeapot(0.3);
        glutSolidTeapot(0.3);
        glutSolidTeapot(0.3);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week15 many angles");

    glutDisplayFunc(display);
    glutTimerFunc( 0, timer, 0);
    glutMainLoop();
}///先不放貼圖, 先不放打光






















程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle=0, diff=2;///上週程式,會增加、會減少!!!
///float angle[20]={};//都是0的陣列 angle (下週Week16-1接著教)
//(下週Week16-1接著教) 用 fprintf() 把角度狂印, fscanf() 播動畫 frame-by-frame
void timer(int t)///上週程式,會增加、會減少!!!
{
    glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer
    angle += diff;///上週程式,會增加、會減少!!!
    if(angle>90) diff = -2;///上週程式,會增加、會減少!!!
    if(angle<0) diff = +2;///上週程式,會增加、會減少!!!
    glutPostRedisplay();///上週教display();
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();///Step05-2
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///Step05-2
            glTranslatef(-0.3,0,0);
            glRotatef(angle, 0,0,1);//(下週Week16-1接著教) angle[0]
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle, 0,0,1);//(下週Week16-1接著教) angle[1]
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();///Step05-2
        glPushMatrix();///Step05-2
            glTranslatef(+0.3,0,0);
            glRotatef(-angle, 0,0,1);//(下週Week16-1接著教) angle[2]
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();///step05-3
                glTranslatef(+0.3,0,0);
                glRotatef(-angle, 0,0,1);//(下週Week16-1接著教) angle[3]...
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();///Step05-3
        glPopMatrix();///Step05-2
    glPopMatrix();///Step05-2
    glutSwapBuffers();
}
int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow( "week15 many angles" );

    glutDisplayFunc( display );
    glutTimerFunc( 0, timer, 0);
    glutMainLoop();
}///先不放貼圖, 先不放打光

沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...