diff --git a/book.md b/book.md index 0db5ff49..37940c88 100644 --- a/book.md +++ b/book.md @@ -990,6 +990,8 @@ Sometimes material incentives are also very helpful, e.g. a promise 5$ gift card [day-351 tic tac toe](#day-351-tic-tac-toe) +[day-352 files](#day-352-files) + ## [DAY-0] The Computer All modern computers(laptops, phones, pc master race rgb monsters, etc) have somewhat similar components: Processor, Memory, Video Card, Disk and USB controller, WiFi card etc. Some of them are in one single chip and you cant even see them anymore, but they are there. For example there are chips that have Processor and Video Card together. The term for processor is actually CPU - Central processing unit, but we called it processors when we were kids and it kind of make sense, since it processes stuff. @@ -24787,7 +24789,7 @@ int main(void){ .c0 = '-', .c1 = '-', .c2 = '-', - }; + }; while(1) { board_print(game_board); @@ -24931,7 +24933,7 @@ int main(void) { *p++; printf("%d\n",x) - + return 0; } @@ -24942,18 +24944,18 @@ int main(void) { *p = 3; printf("%d\n",x) - + return 0; } #include int main(void) { int x = 7; - + scanf("%d",&x); printf("%d\n",x); - + return 0; } ``` @@ -25110,7 +25112,7 @@ now you can see we can call the function, but we cant go back in order to beep, ``` ┌────┬────┬────┬────┐ -0 │ 9 │ 15 │ 10 │ 6 │ <- load the value of addr 8 into R1, +0 │ 9 │ 15 │ 10 │ 6 │ <- load the value of addr 8 into R1, ├────┼────┼────┼────┤ so we can go back to wherever it points 4 │ 13 │ 40│ 8 │ 7 │ <- we want to go back to addr 8 ├────┼────┼────┼────┤ where we have the beep instruction @@ -25165,7 +25167,7 @@ void print_board(char *b) { } int check_win(char *b){ if (b[0] != '-' && b[0] == b[1] && b[1] == b[2]) { - return 1; + return 1; } if (b[3] != '-' && b[3] == b[4] && b[4] == b[5]) { return 1; @@ -25201,31 +25203,31 @@ int main(void) { char str[3]; printf("%c>",symbol); scanf("%2s",str); - if (str[0] == 'a' && str[1] == '1') { + if (str[0] == 'a' && str[1] == '1') { board[0] = symbol; } - if (str[0] == 'b' && str[1] == '1') { + if (str[0] == 'b' && str[1] == '1') { board[3] = symbol; } - if (str[0] == 'c' && str[1] == '1') { + if (str[0] == 'c' && str[1] == '1') { board[6] = symbol; } - if (str[0] == 'a' && str[1] == '2') { + if (str[0] == 'a' && str[1] == '2') { board[1] = symbol; } - if (str[0] == 'a' && str[1] == '3') { + if (str[0] == 'a' && str[1] == '3') { board[2] = symbol; } - if (str[0] == 'b' && str[1] == '2') { + if (str[0] == 'b' && str[1] == '2') { board[4] = symbol; } - if (str[0] == 'b' && str[1] == '3') { + if (str[0] == 'b' && str[1] == '3') { board[5] = symbol; } - if (str[0] == 'c' && str[1] == '2') { + if (str[0] == 'c' && str[1] == '2') { board[7] = symbol; } - if (str[0] == 'c' && str[1] == '3') { + if (str[0] == 'c' && str[1] == '3') { board[8] = symbol; } @@ -25244,3 +25246,34 @@ int main(void) { } ``` + +## [DAY-352] files + +Watch Bro Code's reading and writing files: https://www.youtube.com/watch?v=UqB4EgUxapM and https://www.youtube.com/watch?v=Hzg3kCHJcxI + +> the code she wrote + +``` + +#include + +int main() { + FILE *pF = fopen("test.txt","w"); + + fprintf(pF, "spongebob sqquerpants KIRA AND RYUK"); + fclose(pF); + return 0; +} + + +#include +int main() { + FILE *pF = fopen("/Users/jack/book/poem.txt","r"); + char buffer[255]; + while(fgets(buffer,255,pF) != NULL) { + printf("%s", buffer); + } + fclose(pF); +} +``` + diff --git a/examples/week-047/001.txt b/examples/week-047/001.txt index b88defac..66877435 100644 --- a/examples/week-047/001.txt +++ b/examples/week-047/001.txt @@ -80,7 +80,7 @@ int main(void){ .c0 = '-', .c1 = '-', .c2 = '-', - }; + }; while(1) { board_print(game_board); diff --git a/examples/week-047/005.txt b/examples/week-047/005.txt index c686d76a..f721581c 100644 --- a/examples/week-047/005.txt +++ b/examples/week-047/005.txt @@ -5,7 +5,7 @@ int main(void) { *p++; printf("%d\n",x) - + return 0; } @@ -16,17 +16,17 @@ int main(void) { *p = 3; printf("%d\n",x) - + return 0; } #include int main(void) { int x = 7; - + scanf("%d",&x); printf("%d\n",x); - + return 0; } diff --git a/examples/week-047/012.txt b/examples/week-047/012.txt index dc59bfd8..147088b8 100644 --- a/examples/week-047/012.txt +++ b/examples/week-047/012.txt @@ -1,5 +1,5 @@ ┌────┬────┬────┬────┐ -0 │ 9 │ 15 │ 10 │ 6 │ <- load the value of addr 8 into R1, +0 │ 9 │ 15 │ 10 │ 6 │ <- load the value of addr 8 into R1, ├────┼────┼────┼────┤ so we can go back to wherever it points 4 │ 13 │ 40│ 8 │ 7 │ <- we want to go back to addr 8 ├────┼────┼────┼────┤ where we have the beep instruction diff --git a/examples/week-047/013.txt b/examples/week-047/013.txt index ec0a25e4..5d9a5eea 100644 --- a/examples/week-047/013.txt +++ b/examples/week-047/013.txt @@ -7,7 +7,7 @@ void print_board(char *b) { } int check_win(char *b){ if (b[0] != '-' && b[0] == b[1] && b[1] == b[2]) { - return 1; + return 1; } if (b[3] != '-' && b[3] == b[4] && b[4] == b[5]) { return 1; @@ -43,31 +43,31 @@ int main(void) { char str[3]; printf("%c>",symbol); scanf("%2s",str); - if (str[0] == 'a' && str[1] == '1') { + if (str[0] == 'a' && str[1] == '1') { board[0] = symbol; } - if (str[0] == 'b' && str[1] == '1') { + if (str[0] == 'b' && str[1] == '1') { board[3] = symbol; } - if (str[0] == 'c' && str[1] == '1') { + if (str[0] == 'c' && str[1] == '1') { board[6] = symbol; } - if (str[0] == 'a' && str[1] == '2') { + if (str[0] == 'a' && str[1] == '2') { board[1] = symbol; } - if (str[0] == 'a' && str[1] == '3') { + if (str[0] == 'a' && str[1] == '3') { board[2] = symbol; } - if (str[0] == 'b' && str[1] == '2') { + if (str[0] == 'b' && str[1] == '2') { board[4] = symbol; } - if (str[0] == 'b' && str[1] == '3') { + if (str[0] == 'b' && str[1] == '3') { board[5] = symbol; } - if (str[0] == 'c' && str[1] == '2') { + if (str[0] == 'c' && str[1] == '2') { board[7] = symbol; } - if (str[0] == 'c' && str[1] == '3') { + if (str[0] == 'c' && str[1] == '3') { board[8] = symbol; } diff --git a/examples/week-047/014.txt b/examples/week-047/014.txt new file mode 100644 index 00000000..e900541f --- /dev/null +++ b/examples/week-047/014.txt @@ -0,0 +1,21 @@ + +#include + +int main() { + FILE *pF = fopen("test.txt","w"); + + fprintf(pF, "spongebob sqquerpants KIRA AND RYUK"); + fclose(pF); + return 0; +} + + +#include +int main() { + FILE *pF = fopen("/Users/jack/book/poem.txt","r"); + char buffer[255]; + while(fgets(buffer,255,pF) != NULL) { + printf("%s", buffer); + } + fclose(pF); +} diff --git a/toc.md b/toc.md index 7740580c..bcb1dd55 100644 --- a/toc.md +++ b/toc.md @@ -872,3 +872,5 @@ [day-350 machine code; functions](#day-350-machine-code-functions) [day-351 tic tac toe](#day-351-tic-tac-toe) + +[day-352 files](#day-352-files)