CHAPTER 12 - 0083tony/Core-C-Programming GitHub Wiki
- 1๋ฒ ๋ฌธ์
ํ ์คํธ ํ์ผ์ ์ด๋ฆ์ ์ ๋ ฅ๋ฐ์ ๋ผ์ธ ๋ฒํธ์ ํจ๊ป ๋ด์ฉ์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ
README.txt
This is text file for input test.
This file contains multiple lines of text.
The program displays the content of file with line number.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> // strlen() ํจ์
#include <stdlib.h> // exit()ํจ์
#define SIZE 100
int main(void) {
char text_filename[SIZE];
char text_contents[SIZE];
int count = 1; // ๋ผ์ธ ๋ฒํธ
FILE* fp;
// ํ์ผ ์ด๋ฆ์ ์
๋ ฅ๋ฐ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ ๋ถ๋ฌ์ด
printf("ํ์ผ๋ช
? ");
gets_s(text_filename, SIZE);
fp = fopen(text_filename, "r");
// ํ์ผ ์ด๋ฆ์ด ์ผ์นํ์ง ์์ผ๋ฉด Error! ์ถ๋ ฅ ํ ์ข
๋ฃ
if (fp == NULL) {
printf("Error!\n");
exit(1);
}
// fgetsํจ์๋ ๊ฐํ๋ฌธ์(\n)๊น์ง ๋ฐ๋๋ค ๊ทธ๋ฌ๋ฏ๋ก ๊ฐํ๋ฌธ์๋ฅผ NUL๋ฌธ์(\0)๋ก ๋ณ๊ฒฝ
while (!feof(fp)) {
fgets(text_contents, SIZE, fp);
if (text_contents[strlen(text_contents) - 1] == '\n') {
text_contents[strlen(text_contents) - 1] = '\0';
}
printf("%3d: ", count); // ๋ผ์ธ ๋ฒํธ ์ถ๋ ฅ
puts(text_contents); // ํ
์คํธ ๋ด์ฉ ์ถ๋ ฅ
count++; // ๋ผ์ธ ๋ฒํธ ์ฆ๊ฐ
}
fclose(fp);
}
[์คํ ๊ฒฐ๊ณผ]
ํ์ผ๋ช
? README.txt
1: This is text file for input test.
2: This file contains multiple lines of text.
3: The program displays the content of file with line number.
- 2๋ฒ ๋ฌธ์
ํ ์คํธ ํ์ผ์ ์ด๋ฆ์ ์ ๋ ฅ๋ฐ์ ๋ชจ๋ ๋ด์ฉ์ ์๋ฌธ์๋ก ๋ณํํด ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ
README.txt
This is text file for input test.
This file contains multiple lines of text.
The program displays the content of file with line number.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> //strlen(), _strlwr()ํจ์
#define SIZE 300
int main(void) {
char text_filename[SIZE];
char text_contents[SIZE];
FILE* fp;
printf("ํ์ผ๋ช
? ");
// ํ์ผ ์ด๋ฆ์ ์
๋ ฅ๋ฐ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ ๋ถ๋ฌ์ด
gets_s(text_filename, SIZE);
fp = fopen(text_filename, "r");
// ํ์ผ ์ด๋ฆ์ด ์ผ์นํ์ง ์์ผ๋ฉด Error! ์ถ๋ ฅ ํ ์ข
๋ฃ
if (fp == NULL) {
printf("Error!\n");
return 1;
}
// fgetsํจ์๋ ๊ฐํ๋ฌธ์(\n)๊น์ง ๋ฐ๋๋ค ๊ทธ๋ฌ๋ฏ๋ก ๊ฐํ๋ฌธ์๋ฅผ NUL(\0)๋ก ๋ณ๊ฒฝ
while (!feof(fp)) {
fgets(text_contents, SIZE, fp);
if (text_contents[strlen(text_contents) - 1] == '\n') {
text_contents[strlen(text_contents) - 1] = '\0';
}
_strlwr(text_contents); // strlwrํจ์๋ฅผ ์ด์ฉํด ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ณ๊ฒฝ
puts(text_contents); // ํ
์คํธ ๋ด์ฉ ์ถ๋ ฅ
}
fclose(fp);
}
[์คํ ๊ฒฐ๊ณผ]
ํ์ผ๋ช
? README.txt
this is text file for input test.
this file contains multiple lines of text.
the program displays the content of file with line number.
- 3๋ฒ ๋ฌธ์
ํ ์คํธ ํ์ผ์ ์ด๋ฆ์ ์ ๋ ฅ๋ฐ์ ๋ชจ๋ ๋ด์ฉ์ ์ถ๋ ฅํ๊ณ ํ์ผ ๋ด์ ์๋ฌธ์์ ๊ฐฏ์๋ฅผ ์ธ์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ
README.txt
This is text file for input test.
This file contains multiple lines of text.
The program displays the content of file with line number.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> //strlen()ํจ์
#define SIZE 300
int main(void) {
char text_filename[SIZE];
char text_contents[SIZE];
FILE* fp;
printf("ํ์ผ๋ช
? ");
// ํ์ผ ์ด๋ฆ์ ์
๋ ฅ๋ฐ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ ๋ถ๋ฌ์ด
gets_s(text_filename, SIZE);
fp = fopen(text_filename, "r");
// ํ์ผ์ ์ฐพ์ง ๋ชปํ๋ฉด Error! ์ถ๋ ฅ ํ ์ข
๋ฃ
if (fp == NULL) {
printf("Error!\n");
return 1;
}
// ์๋ฌธ์๋ฅผ ๋น๊ตํ๊ณ ์ ์ฅํ ๋ฐฐ์ด ์ ์ธ
char ABC[21] = { 'A','B','C','D','E','F','G','H','I','L','M','N','O','P','R','S','T','U','W','X','Y' };
char abc[21] = { 'a','b','c','d','e','f','g','h','i','l','m','n','o','p','r','s','t','u','w','x','y' };
int ABC_num[21];
for (int i = 0; i < 21; i++) {
ABC_num[i] = 0;
}
// fgetsํจ์๋ ๊ฐํ๋ฌธ์(\n)๊น์ง ๋ฐ๋๋ค ๊ทธ๋ฌ๋ฏ๋ก ๊ฐํ๋ฌธ์๋ฅผ NUL(\0)๋ก ๋ณ๊ฒฝ ํ ์ถ๋ ฅ
while (!feof(fp)) {
fgets(text_contents, SIZE, fp);
if (text_contents[strlen(text_contents) - 1] == '\n') {
text_contents[strlen(text_contents) - 1] = '\0';
}
puts(text_contents);
// ์๋ฌธ์ ์๋ฌธ์์ ๋๋ฌธ์๋ฅผ ๋น๊ตํด ABC_num์ ๊ทธ ๊ฐฏ์๋ฅผ ์ ์ฅ
for (int i = 0; i < 21; i++) {
for (int k = 0; k < SIZE; k++) {
if ((text_contents[k] == abc[i]) || (text_contents[k] == ABC[i]))
ABC_num[i] = ABC_num[i] + 1;
}
}
}
// ์ ์ฅํ ์๋ฌธ์์ ๊ฐฏ์๋ฅผ ์ถ๋ ฅ
for (int i = 0; i < 21; i++) {
printf("%3c:%2d", ABC[i], ABC_num[i]);
if ((i % 10 == 0) && (i != 0))
printf("\n");
}
fclose(fp);
}
ํ์ผ๋ช
? README.txt
This is text file for input test.
This file contains multiple lines of text.
The program displays the content of file with line number.
A: 3 B: 1 C: 2 D: 1 E:13 F: 6 G: 1 H: 5 I:13 L: 8 M: 3
N: 8 O: 6 P: 4 R: 4 S: 8 T:16 U: 3 W: 1 X: 2 Y: 1
- 4๋ฒ ๋ฌธ์
์์ด๋์ ๋น๋ฐ๋ฒํธ๊ฐ ์ ์ฅ๋ ํ ์คํธ ํ์ผ์ ๊ตฌ์กฐ์ฒด์ ์ ์ฅํด ๋ก๊ทธ์ธ ํ๋ก๊ทธ๋จ์ ๊ตฌํ
Password.txt
admin abc123
pizza beer
coke pepsi
Mac Ms
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> // strcmp()ํจ์
#define SIZE 10
struct Login {
char ID[SIZE];
char Password[SIZE];
};
int main(void) {
struct Login a[4];
char ID[SIZE];
char Password[SIZE];
FILE* fp;
// ํ์ผ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ๋ถ๋ฌ์ด
fp = fopen("password.txt", "r");
// ํ
์คํธ์ ์ ์ฅ๋ ์ ๋ณด๋ฅผ ๊ตฌ์กฐ์ฒด๋ก ๊ฐ์ ธ์ด
for (int i = 0; i < 4; i++) {
fscanf(fp, "%s %s", &a[i].ID, &a[i].Password);
}
// ํ์ผ ๋ซ๊ธฐ
fclose(fp);
for (;;) {
start:
printf("ID? ");
scanf("%s", ID);
// .์ ์
๋ ฅํ๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
if (*ID == '.')
break;
for (int i = 0; i < 4; i++) {
// ID ๋น๊ต
if (!strcmp(ID, a[i].ID)) {
getchar();
printf("Password? ");
scanf("%s", Password);
// Password ๋น๊ต
if (!strcmp(Password, a[i].Password)) {
printf("Login Successful!\n");
goto start;
}
printf("Password Error!\n");
goto start;
}
}
printf("ID Error!\n");
}
}
[์คํ ๊ฒฐ๊ณผ]
ID? hello?
ID Error!
ID? admin
Password? 123123
Password Error!
ID? admin
Password? abc123
Login Successful!
ID? .
- 5๋ฒ ๋ฌธ์
4๋ฒ ๋ฌธ์ ์ ํ๋ก๊ทธ๋จ์ ๋ก๊ทธ์ธ ์คํจ์ ์์ด๋๋ฅผ ์๋ก ๋ฑ๋กํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐ
Password.txt
admin abc123
pizza beer
coke pepsi
Mac Ms
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> // strcmp()ํจ์
#define SIZE 10
struct Login {
char ID[SIZE] = "";
char Password[SIZE] = "";
};
int main(void) {
struct Login a[20];
char ID[SIZE];
char Password[SIZE];
char yesno;
int count = 4;
FILE* fp;
// ํ์ผ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ๋ถ๋ฌ์ด
fp = fopen("password.txt", "r");
// ํ
์คํธ์ ์ ์ฅ๋ ์ ๋ณด๋ฅผ ๊ตฌ์กฐ์ฒด๋ก ๊ฐ์ ธ์ด
for (int i = 0; i < 4; i++) {
fscanf(fp, "%s %s", &a[i].ID, &a[i].Password);
}
fclose(fp);
for (;;) {
start:
printf("ID? ");
scanf("%s", ID);
// .์ ์
๋ ฅํ๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
if (*ID == '.')
break;
for (int i = 0; i < 20; i++) {
// ID ๋น๊ต
if (!strcmp(ID, a[i].ID)) {
getchar();
printf("Password? ");
scanf("%s", Password);
// Password ๋น๊ต
if (!strcmp(Password, a[i].Password)) {
printf("Login Successful!\n");
goto start;
}
printf("Password Error!\n");
goto start;
}
}
printf("Add ID? ");
getchar();
scanf("%c", &yesno);
// ID ์ถ๊ฐ Y/N
if ((yesno == 'Y') || (yesno == 'y')) {
// Y ์
๋ ฅ์ ID๋ฅผ ๊ตฌ์กฐ์ฒด์ ์ ์ฅ
strncpy(a[count].ID, ID, SIZE);
PW:
// Password ์
๋ ฅ
printf("Password? ");
scanf("%s", a[count].Password);
// Password๋ฅผ ํ๋ฒ ๋ ์
๋ ฅ๋ฐ์ ๊ตฌ์กฐ์ฒด์ ์ ์ฅ๋ Pasword์ ๋น๊ต
printf("Password again? ");
scanf("%s", Password);
// Password ์ผ์น ์ ID PW ์ถ๊ฐ ์ฑ๊ณต๋ฌธ์ ์ถ๋ ฅ
if (!strcmp(a[count].Password, Password)) {
printf("ID and Password registered Successful!\n");
count++;
goto start;
}
printf("Password doesn't match!\n");
goto PW;
}
}
// Login ๊ตฌ์กฐ์ฒด์ ์ ์ฅ๋ ๋ด์ฉ์ ํ์ผ๋ก ์ถ๋ ฅ
fp = fopen("password.txt", "w");
fwrite(&a, sizeof(struct Login), count, fp);
fclose(fp);
return 0;
}
[์คํ ๊ฒฐ๊ณผ]
ID? Love_Pepsi
Add ID? Y
Password? cokecoke
Password again? pepsipepsi
Password doesn't match!
Password? noob_coke
Password again? noob_coke
ID and Password registered Successful!
ID? .
- 6๋ฒ ๋ฌธ์
์ปคํผ์์ ๊ณ์ฐ์๋ฅผ ํ ์คํธ ํ์ผ๋ก ์ ์ฅํ๋ ํ๋ก๊ทธ๋จ
receipt.txt
์ ํ๋ช
๋จ๊ฐ ์๋ ๊ธ์ก
-------------------------------------
์๋ฉ๋ฆฌ์นด๋
ธ 4000 2 8000
์นดํ๋ผ๋ผ 4500 1 4500
-------------------------------------
๊ฒฐ์ ๊ธ์ก : 12500
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE 100
int main(void) {
int arr[5];
int price[3] = { 4000,4500,5000 };
char menu[4][SIZE] = {
{"์๋ฉ๋ฆฌ์นด๋
ธ"},
{"์นดํ๋ผ๋ผ"},
{"ํ๋ซํ์ดํธ"},
{"๊ฒฐ์ ๊ธ์ก"}
};
// ๋ฉ๋ด๋ฅผ ์ถ๋ ฅํ๊ณ ์ฃผ๋ฌธ์ ๋ฐ์
printf("[๋ฉ๋ด] %5s:%d, %5s:%d, %5s:%d\n", menu[0], price[0], menu[1], price[1], menu[2], price[2]);
printf("%-10s ์๋? ", menu[0]);
scanf("%d", &arr[0]);
printf("%-10s ์๋? ", menu[1]);
scanf("%d", &arr[1]);
printf("%-10s ์๋? ", menu[2]);
scanf("%d", &arr[2]);
arr[3] = (arr[0] * 4000) + (arr[1] * 4500) + (arr[2] * 5000);
printf("%-10s: %d", menu[3], arr[3]);
// receipt.txt ํ์ผ์ ์ด๊ณ ์ฃผ๋ฌธ๋ฐ์ ๋ด์ฉ์ ํ ๋๋ก ๊ณ์ฐ์ ์ถ๋ ฅ
FILE* fp;
fp = fopen("receipt.txt", "w");
fputs("์ ํ๋ช
๋จ๊ฐ ์๋ ๊ธ์ก\n", fp);
fputs("-------------------------------------\n", fp);
// ์ฃผ๋ฌธ ๋ฐ์ ์๊ฐ 0์ธ ๋ฉ๋ด๋ ์ถ๋ ฅํ์ง ์์
if (arr[0] != 0)
fprintf(fp, "%-10s %d %4d %4d\n", menu[0], price[0], arr[0], arr[0] * price[0]);
if (arr[1] != 0)
fprintf(fp, "%-10s %d %4d %4d\n", menu[1], price[1], arr[1], arr[1] * price[1]);
if (arr[2] != 0)
fprintf(fp, "%-10s %d %4d %4d\n", menu[2], price[2], arr[2], arr[2] * price[2]);
fputs("-------------------------------------\n", fp);
fprintf(fp,"%-10s:%27d", menu[3], arr[3]);
fclose(fp);
return 0;
}
[์คํ ๊ฒฐ๊ณผ]
[๋ฉ๋ด] ์๋ฉ๋ฆฌ์นด๋
ธ:4000, ์นดํ๋ผ๋ผ:4500, ํ๋ซํ์ดํธ:5000
์๋ฉ๋ฆฌ์นด๋
ธ ์๋? 2
์นดํ๋ผ๋ผ ์๋? 1
ํ๋ซํ์ดํธ ์๋? 0
๊ฒฐ์ ๊ธ์ก : 12500
- 7๋ฒ ๋ฌธ์
6๋ฒ ๋ฌธ์ ์ ๊ณ์ฐ์ ํ๋ก๊ทธ๋จ์์ ๊ธฐ๋ฅ์ ์ถ๊ฐํด ๋ชจ๋ ์ ํ์ ์๋์ด 0์ ์ ๋ ฅ ๋ฐ์๋๊น์ง ๋ฐ๋ณต ์ํํ๊ณ ๊ทธ ๋ชจ๋ ๊ฐ์ ํ ์คํธ ํ์ผ๋ก ์ ์ฅํด ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ
receipt.txt
์ ํ๋ช
๋จ๊ฐ ์๋ ๊ธ์ก
--------------------------------------------------
์๋ฉ๋ฆฌ์นด๋
ธ 4000 1 4000
์นดํ๋ผ๋ผ 4500 2 9000
ํ๋ซํ์ดํธ 5000 3 15000
--------------------------------------------------
๊ฒฐ์ ๊ธ์ก : 28000
์ ํ๋ช
๋จ๊ฐ ์๋ ๊ธ์ก
--------------------------------------------------
์๋ฉ๋ฆฌ์นด๋
ธ 4000 1 4000
ํ๋ซํ์ดํธ 5000 1 5000
--------------------------------------------------
๊ฒฐ์ ๊ธ์ก : 9000
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE 100
int main(void) {
int arr[4];
int price[3] = { 4000,4500,5000 };
char menu[4][SIZE] = {
{"์๋ฉ๋ฆฌ์นด๋
ธ"},
{"์นดํ๋ผ๋ผ"},
{"ํ๋ซํ์ดํธ"},
{"๊ฒฐ์ ๊ธ์ก"}
};
FILE* fp;
fp = fopen("receipt.txt", "w");
for (;;) {
// ๋ฉ๋ด๋ฅผ ์ถ๋ ฅํ๊ณ ์ฃผ๋ฌธ์ ๋ฐ์
printf("[๋ฉ๋ด] %5s:%d, %5s:%d, %5s:%d\n", menu[0], price[0], menu[1], price[1], menu[2], price[2]);
printf("%-10s ์๋? ", menu[0]);
scanf("%d", &arr[0]);
printf("%-10s ์๋? ", menu[1]);
scanf("%d", &arr[1]);
printf("%-10s ์๋? ", menu[2]);
scanf("%d", &arr[2]);
arr[3] = (arr[0] * 4000) + (arr[1] * 4500) + (arr[2] * 5000);
printf("%-10s: %d\n", menu[3], arr[3]);
// arr[3]์ ๊ธ์ก์ด 0์ผ๋ breakํด๋ ๊ฐ์ ๊ฒฐ๊ณผ
if ((arr[0] == 0) && (arr[1] == 0) && (arr[2] == 0))
break;
// receipt.txt ํ์ผ์ ์ด๊ณ ์ฃผ๋ฌธ๋ฐ์ ๋ด์ฉ์ ํ ๋๋ก ๊ณ์ฐ์ ์ถ๋ ฅ
fputs("์ ํ๋ช
๋จ๊ฐ ์๋ ๊ธ์ก\n", fp);
fputs("--------------------------------------------------\n", fp);
// ์ฃผ๋ฌธ ๋ฐ์ ์๊ฐ 0์ธ ๋ฉ๋ด๋ ์ถ๋ ฅํ์ง ์์
if (arr[0] != 0)
fprintf(fp, "%-10s %d %4d %4d\n", menu[0], price[0], arr[0], arr[0] * price[0]);
if (arr[1] != 0)
fprintf(fp, "%-10s %d %4d %4d\n", menu[1], price[1], arr[1], arr[1] * price[1]);
if (arr[2] != 0)
fprintf(fp, "%-10s %d %4d %4d\n", menu[2], price[2], arr[2], arr[2] * price[2]);
fputs("--------------------------------------------------\n", fp);
fprintf(fp, "%-10s:%35d\n\n", menu[3], arr[3]);
}
fclose(fp);
return 0;
}
[์คํ ๊ฒฐ๊ณผ]
[๋ฉ๋ด] ์๋ฉ๋ฆฌ์นด๋
ธ:4000, ์นดํ๋ผ๋ผ:4500, ํ๋ซํ์ดํธ:5000
์๋ฉ๋ฆฌ์นด๋
ธ ์๋? 1
์นดํ๋ผ๋ผ ์๋? 2
ํ๋ซํ์ดํธ ์๋? 3
๊ฒฐ์ ๊ธ์ก : 28000
[๋ฉ๋ด] ์๋ฉ๋ฆฌ์นด๋
ธ:4000, ์นดํ๋ผ๋ผ:4500, ํ๋ซํ์ดํธ:5000
์๋ฉ๋ฆฌ์นด๋
ธ ์๋? 1
์นดํ๋ผ๋ผ ์๋? 0
ํ๋ซํ์ดํธ ์๋? 1
๊ฒฐ์ ๊ธ์ก : 9000
[๋ฉ๋ด] ์๋ฉ๋ฆฌ์นด๋
ธ:4000, ์นดํ๋ผ๋ผ:4500, ํ๋ซํ์ดํธ:5000
์๋ฉ๋ฆฌ์นด๋
ธ ์๋? 0
์นดํ๋ผ๋ผ ์๋? 0
ํ๋ซํ์ดํธ ์๋? 0
๊ฒฐ์ ๊ธ์ก : 0
- 8๋ฒ ๋ฌธ์
2๊ฐ์ ํ ์คํธ ํ์ผ์ ๋น๊ตํ๋ ํ๋ก๊ทธ๋จ
readme.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
READMEPZ.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE 100
int main(void) {
char text_filename_A[SIZE];
char text_filename_B[SIZE];
char A, B;
FILE* fpA;
FILE* fpB;
// ํ์ผ์ ์ด๋ฆ์ ์
๋ ฅ๋ฐ์
printf("์๋ณธ ํ์ผ? ");
scanf("%s", text_filename_A);
printf("ํ๊ฒ ํ์ผ? ");
scanf("%s", text_filename_B);
// ํ์ผ์ ๋ถ๋ฌ์ด
fpA = fopen(text_filename_A, "r");
fpB = fopen(text_filename_B, "r");
// ํ์ผ ๋ถ๋ฌ์ค๊ธฐ ์คํจ์ Error! ์ถ๋ ฅ๊ณผ ํจ๊ป ์ข
๋ฃ
if (fpA == NULL || fpB == NULL) {
printf("Error!\n");
return 1;
}
for (;;) {
if (feof(fpA) == 0 && feof(fpB) == 0) {
// ํ์ผ์ ์ ์ฅ๋ ๋ฌธ์๋ฅผ ํ ๋ฌธ์์ฉ A,B๋ก ์ ๋ฌ
A = fgetc(fpA);
B = fgetc(fpB);
// foeof() ํจ์๋ฅผ ์ฌ์ฉํด ๋น๊ต
if (A != B) {
printf("๋ ํ์ผ์ ๋ค๋ฆ
๋๋ค.\n");
break;
}
else if (feof(fpA) != 0 && feof(fpB) == 0) {
printf("๋ ํ์ผ์ ๋ค๋ฆ
๋๋ค.\n");
break;
}
else if (feof(fpA) == 0 && feof(fpB) != 0) {
printf("๋ ํ์ผ์ ๋ค๋ฆ
๋๋ค.\n");
break;
}
else {
printf("๋ ํ์ผ์ด ๊ฐ์ต๋๋ค.\n");
break;
}
}
}
fclose(fpA);
fclose(fpB);
}
[์คํ ๊ฒฐ๊ณผ]
์๋ณธ ํ์ผ? readme.txt
ํ๊ฒ ํ์ผ? READMEPZ.txt
๋ ํ์ผ์ด ๊ฐ์ต๋๋ค.
- 9๋ฒ ๋ฌธ์
ํ ์คํธ์ ํค๋ฅผ ์ ๋ ฅ๋ฐ์ ์์ ์ํธ๋ก ์ํธํํ ํ ํ ์คํธ ํ์ผ๋ก ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ
์์ ์ํธ๋ ๊ฐ ์ํ๋ฒณ์ ํน์ ๊ฐฏ์๋งํผ ๋ํ๊ณ ๋นผ์ ๋ค๋ฅธ ์ํ๋ฒณ์ผ๋ก ์นํํ๋ ๊ฐ๋จํ ์ํธํ ๋ฐฉ์์ด๋ค.
cipher.txt
Ixq Ixq F Surjudpplqj.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> //strlen()ํจ์
#define SIZE 100
int main(void) {
char text_contents[SIZE];
int key, k = 0;
FILE* fp;;
fp = fopen("cipher.txt", "w");
printf("์ํธ ํค? ");
scanf("%d", &key);
getchar();
printf("์ํธํํ ํ
์คํธ?\n");
// %[^\n]s ๋ฅผ ์ฌ์ฉํด \n์ ์ ์ธํ ๋ชจ๋ ๋ฌธ์๋ฅผ ์
๋ ฅ๋ฐ์
scanf("%[^\n]s", text_contents);
// text_contents[i]์ A(ASCII 65)๋ฅผ ๋นผ์ A๋ฅผ 0์ผ๋ก ๋ง๋ค์ด ๊ธฐ์ค์ ์ก๊ณ key์ ๊ฐ๋งํผ ๋ํ๋ค.
for (size_t i = 0; i < strlen(text_contents); i++) {
if (text_contents[i] >= 'A' && text_contents[i] <= 'Z') {
text_contents[i] -= 'A';
if (text_contents[i] + key < 0) {
text_contents[i] += 26;
}
// ์ํ๋ฒณ์ 26๋ฌธ์๋ก ์ด๋ฃจ์ด์ ธ์๊ธฐ์ 26๋ณด๋ค ํฐ๊ฐ์ ๋ฒ๋ฆฌ๋๊ฒ
text_contents[i] = (text_contents[i] + key) % 26;
// ๋๋จธ์ง๊ฐ์ A๋ฅผ ๋ํด ASCII์ฝ๋๋ฅผ ๋ง์ถ๋ค.
text_contents[i] += 'A';
}
if (text_contents[i] >= 'a' && text_contents[i] <= 'z') {
text_contents[i] -= 'a';
if (text_contents[i] + key < 0) {
text_contents[i] += 26;
}
text_contents[i] = (text_contents[i] + key) % 26;
text_contents[i] += 'a';
}
}
// ํ์ผ์ ์ํธํํ ํ
์คํธ๋ฅผ ์ ์ฅํ๊ณ ํ์ผ์ ๋ซ์.
fputs(text_contents, fp);
fclose(fp);
}
[์คํ ๊ฒฐ๊ณผ]
์ํธ ํค? 3
์ํธํํ ํ
์คํธ?
Fun Fun C Programming.
- 10๋ฒ ๋ฌธ์
9๋ฒ ๋ฌธ์ ์์ ๋ง๋ ์์ ์ํธํ๋ ํ์ผ์ ๋ณตํธํ ์ํค๋ ํ๋ก๊ทธ๋จ
cipher.txt
Ixq Ixq F Surjudpplqj.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(void) {
char text_filename[SIZE];
char text_contents[SIZE];
int key;
FILE* fp;
// ํ์ผ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ณ ๋ถ๋ฌ์ด
printf("๋ณตํธํํ ํ์ผ? ");
gets_s(text_filename, SIZE);
fp = fopen(text_filename, "r");
// ํ์ผ์ ์ฐพ์ง ๋ชปํ๋ฉด Error! ์ถ๋ ฅ ํ ์ข
๋ฃ
if (fp == NULL) {
printf("Error!\n");
return 1;
}
// ๋ณตํธ ํค๋ฅผ ์
๋ ฅ๋ฐ์
printf("๋ณตํธ ํค? ");
scanf("%d", &key);
key = -key;
// text_contents[i]์ A(ASCII 65)๋ฅผ ๋นผ์ A๋ฅผ 0์ผ๋ก ๋ง๋ค์ด ๊ธฐ์ค์ ์ก๊ณ key์ ๊ฐ๋งํผ ๋บ๋ค.
while (!feof(fp)) {
fgets(text_contents, SIZE, fp);
for (size_t i = 0; i < strlen(text_contents); i++) {
if (text_contents[i] >= 'A' && text_contents[i] <= 'Z') {
text_contents[i] -= 'A';
if (text_contents[i] + key < 0) {
text_contents[i] += 26;
}
// ์ํ๋ฒณ์ 26๋ฌธ์๋ก ์ด๋ฃจ์ด์ ธ์๊ธฐ์ 26๋ณด๋ค ํฐ๊ฐ์ ๋ฒ๋ฆผ
text_contents[i] = (text_contents[i] + key) % 26;
text_contents[i] += 'A';
}
if (text_contents[i] >= 'a' && text_contents[i] <= 'z') {
text_contents[i] -= 'a';
if (text_contents[i] + key < 0) {
text_contents[i] += 26;
}
text_contents[i] = (text_contents[i] + key) % 26;
text_contents[i] += 'a';
}
}
}
// ๋ณตํธํํ ์ํธ๋ฅผ ์ถ๋ ฅํ๊ณ ํ์ผ์ ๋ซ์
printf("%s\n", text_contents);
fclose(fp);
}
[์คํ ๊ฒฐ๊ณผ]
๋ณตํธํํ ํ์ผ? cipher.txt
๋ณตํธ ํค? 3
Fun Fun C Programming.
- 11๋ฒ ๋ฌธ์
๊ตฌ์กฐ์ฒด ํ์์ ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๊ณ ์ฐ๋ฝ์ฒ๊ฐ ์ ์ฅ๋ ํ์ผ์ ๋ถ๋ฌ์ ๋์ ๋ฉ๋ชจ๋ฆฌ์ ์ ๋ ฅ์ํจ ์ฐ๋ฝ์ฒ๋ฅผ ์ฐพ๋ ํ๋ก๊ทธ๋จ
mycontact.txt
๋
ธ์ง๊ตฌ 01012345678
ํํ์ด 01000000000
์ด์ฌ์ด 01011111111
๋น์ค์ด 01022222222
๋๋ผ์๋ชฝ 01033333333
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // malloc()ํจ์
#include <string.h> // strcmp()ํจ์
#define SIZE 100
struct CONTACT {
char name[SIZE];
unsigned int num;
};
int main(void) {
char text_filename[SIZE];
char name[SIZE];
int struct_size = 5;
// ์ฐ๋ฝ์ฒ๊ฐ ์ ์ฅ๋์ด์๋ ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
FILE* fp;
printf("์ฐ๋ฝ์ฒ ํ์ผ๋ช
? ");
gets_s(text_filename, SIZE);
fp = fopen(text_filename, "r");
// ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
CONTACT* arr;
arr = (struct CONTACT*)malloc(sizeof(struct CONTACT) * struct_size);
// ๋์ ๋ฉ๋ชจ๋ฆฌ์ ํ์ผ์ ์ ์ฅ๋ ์ฐ๋ฝ์ฒ ์
๋ ฅ
for (int i = 0; i < struct_size; i++) {
fscanf(fp, "%s %d", &arr[i].name, &arr[i].num);
}
fclose(fp);
printf("%d๊ฐ์ ์ฐ๋ฝ์ฒ๋ฅผ ๋ก๋ฉํ์ต๋๋ค.\n", struct_size);
for (;;) {
start:
printf("์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ");
scanf("%s", name);
// . ์
๋ ฅ์ ํ๋ก๊ทธ๋จ ์ข
๋ฃ
if (*name == '.')
break;
// ๋์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋ ์ด๋ฆ๊ณผ ์
๋ ฅ๋ฐ์ ์ด๋ฆ์ ๋น๊ต
for (int i = 0; i < struct_size; i++) {
if (!strcmp(name, arr[i].name)) {
printf("%s์ ์ ํ๋ฒํธ %d๋ก ์ ํ๋ฅผ ๊ฒ๋๋ค....\n", arr[i].name, arr[i].num);
goto start;
}
}
printf("์ฐ๋ฝ์ฒ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.\n");
}
free(arr);
}
[์คํ ๊ฒฐ๊ณผ]
์ฐ๋ฝ์ฒ ํ์ผ๋ช
? mycontact.txt
5๊ฐ์ ์ฐ๋ฝ์ฒ๋ฅผ ๋ก๋ฉํ์ต๋๋ค.
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ๋
ธ์ง๊ตฌ
๋
ธ์ง๊ตฌ์ ์ ํ๋ฒํธ 01012345678๋ก ์ ํ๋ฅผ ๊ฒ๋๋ค....
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ๋๋ผ์๋ชฝ
๋๋ผ์๋ชฝ์ ์ ํ๋ฒํธ 01033333333๋ก ์ ํ๋ฅผ ๊ฒ๋๋ค....
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? .
- 12๋ฒ ๋ฌธ์
11๋ฒ ๋ฌธ์ ์ ํ๋ก๊ทธ๋จ์์ ์ฐ๋ฝ์ฒ๋ฅผ ์ฐพ์ ์ ์์ผ๋ฉด ์ฐ๋ฝ์ฒ๋ฅผ ์ถ๊ฐํ๊ณ 'CONTACT' ๊ตฌ์กฐ์ฒด์ ๋ด์ฉ์ ํ ์คํธ ํ์ผ๋ก ์ถ๋ ฅํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ํ๋ก๊ทธ๋จ
List.txt
๋
ธ์ง๊ตฌ 01012345678
ํํ์ด 01000000000
์ด์ฌ์ด 01011111111
๋น์ค์ด 01022222222
๋๋ผ์๋ชฝ 01033333333
ํ๊ธธ๋ 01099999999
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // malloc()ํจ์
#include <string.h> // strcmp()ํจ์
#define SIZE 100
struct CONTACT {
char name[SIZE];
unsigned int num;
};
int main(void) {
char text_filename[SIZE];
char name[SIZE];
int struct_size = 5;
char yesno;
// ์ฐ๋ฝ์ฒ๊ฐ ์ ์ฅ๋์ด์๋ ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
FILE* fp;
printf("์ฐ๋ฝ์ฒ ํ์ผ๋ช
? ");
gets_s(text_filename, SIZE);
fp = fopen(text_filename, "r");
// ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
CONTACT* arr;
arr = (struct CONTACT*)malloc(sizeof(struct CONTACT) * struct_size);
// ๋์ ๋ฉ๋ชจ๋ฆฌ์ ํ์ผ์ ์ ์ฅ๋ ์ฐ๋ฝ์ฒ ์
๋ ฅ
for (int i = 0; i < struct_size; i++) {
fscanf(fp, "%s%d", &arr[i].name, &arr[i].num);
}
fclose(fp);
printf("%d๊ฐ์ ์ฐ๋ฝ์ฒ๋ฅผ ๋ก๋ฉํ์ต๋๋ค.\n", struct_size);
for (;;) {
start:
printf("์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ");
scanf("%s", name);
// . ์
๋ ฅ์ ํ๋ก๊ทธ๋จ ์ข
๋ฃ
if (*name == '.')
break;
// ๋์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋ ์ด๋ฆ๊ณผ ์
๋ ฅ๋ฐ์ ์ด๋ฆ์ ๋น๊ต
for (int i = 0; i < struct_size; i++) {
if (!strcmp(name, arr[i].name)) {
printf("%s์ ์ ํ๋ฒํธ %d๋ก ์ ํ๋ฅผ ๊ฒ๋๋ค....\n", arr[i].name, arr[i].num);
goto start;
}
}
printf("์ฐ๋ฝ์ฒ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.\n");
printf("์ฐ๋ฝ์ฒ๋ฅผ ๋ฑ๋กํ์๊ฒ ์ต๋๊น? (Y/N)\n");
getchar();
scanf("%c", &yesno);
// Y๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด ์ด๋ฆ์ ๋ณต์ฌํ๊ณ ์ ํ๋ฒํธ๋ฅผ ์
๋ ฅ๋ฐ์
if ((yesno == 'Y') || (yesno == 'y')) {
strncpy(arr[struct_size].name, name, SIZE);
printf("์ ํ๋ฒํธ? ");
scanf("%d", &arr[struct_size].num);
struct_size++;
goto start;
}
}
// ๋์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋ ์ด๋ฆ, ์ ํ๋ฒํธ๋ฅผ ํ์ผ๋ก ์ถ๋ ฅ
fp = fopen(text_filename, "w");
for (int i = 0; i < struct_size; i++) {
fprintf(fp, "%s %d\n", arr[i].name, arr[i].num);
}
fclose(fp);
free(arr);
}
[์คํ ๊ฒฐ๊ณผ]
์ฐ๋ฝ์ฒ ํ์ผ๋ช
? List.txt
5๊ฐ์ ์ฐ๋ฝ์ฒ๋ฅผ ๋ก๋ฉํ์ต๋๋ค.
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ๋๋ผ์๋ชฝ
๋๋ผ์๋ชฝ์ ์ ํ๋ฒํธ 01033333333๋ก ์ ํ๋ฅผ ๊ฒ๋๋ค....
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ํ๊ธธ๋
์ฐ๋ฝ์ฒ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
์ฐ๋ฝ์ฒ๋ฅผ ๋ฑ๋กํ์๊ฒ ์ต๋๊น? (Y/N)
Y
์ ํ๋ฒํธ? 01099999999
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? ํ๊ธธ๋
ํ๊ธธ๋์ ์ ํ๋ฒํธ 01099999999๋ก ์ ํ๋ฅผ ๊ฒ๋๋ค....
์ด๋ฆ(. ์
๋ ฅ ์ ์ข
๋ฃ)? .
- 13๋ฒ ๋ฌธ์
N์ ์ ๋ ฅ๋ฐ์ N ํฌ๊ธฐ์ ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํด ๋์๋ก ์ฑ์ฐ๊ณ ํ ์คํธ ํ์ผ๊ณผ 2์ง ํ์ผ๋ก ๊ฐ๊ฐ ์ ์ฅํ๋ ํ๋ก๊ทธ๋จ
A.txt
12db
7f0d
50a9
2cf1
29b4
A.dat
์ด์ด๋ณผ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด๊ฒ ์..
ํฌ๊ธฐ๋ ๋์ผํ ๋ด์ฉ์์๋ .dat์ด ์์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> //malloc()ํจ์, srand()ํจ์, atoi()ํจ์
#include <time.h> // time()ํจ์
#include <string.h> // strcat()ํจ์, strtok()ํจ์
#define SIZE 50
int main(void) {
srand(time(NULL));
int num;
int* N = NULL;
char name[SIZE];
char ext[2][SIZE] = {
{".txt"},
{".dat"}
};
printf("์ ์์ ๊ฐ์? ");
scanf("%d", &num);
printf("ํ์ผ๋ช
? ");
scanf("%s", name);
// ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
N = (int*)malloc(sizeof(int) * num);
if (N == NULL){
printf("Error!\n");
return 1;
}
// .txt ํ์ฅ์ ์ถ๊ฐ
strcat(name, ext[0]);
FILE* fp10;
fp10 = fopen(name, "w");
if (fp10 == NULL) {
printf("Error!\n");
return 1;
}
// name.txt์์ .txt ์ญ์ ํ name.dat์ผ๋ก ๋ณ๊ฒฝ
char* ptr = strtok(name, ".");
strcat(ptr, ext[1]);
FILE* fp2;
fp2 = fopen(ptr, "wb");
if (fp2 == NULL) {
printf("Error!\n");
return 1;
}
// rand()ํจ์๋ก 0~99์ฌ์ด์ ๋์๋ฅผ ์
๋ ฅ
for (int i = 0; i < num; i++) {
N[i] = rand();
fprintf(fp10, "%d", N[i]);
fprintf(fp2, "%02x", N[i]);
}
// ํ์ฅ์ ์ง์ฐ๊ธฐ
char* ptr2 = strtok(name, ".");
printf("%s.txt์ %s.dat์ ์์ฑํ์ต๋๋ค.\n", ptr2, ptr2);
// ํ ๋น๋ ๋์ ๋ฉ๋ชจ๋ฆฌ์ ํ์ผ ์ด๊ธฐํ
fclose(fp10);
fclose(fp2);
free(N);
}
[์คํ ๊ฒฐ๊ณผ]
์ ์์ ๊ฐ์? 5
ํ์ผ๋ช
? A
A.txt์ A.dat์ ์์ฑํ์ต๋๋ค.
- 14๋ฒ ๋ฌธ์
13๋ฒ ๋ฌธ์ ์ ํ๋ก๊ทธ๋จ์์ ์์ฑํ 2๊ฐ์ .dat ํ์ผ์ ๋ถ๋ฌ์ค๊ณ ์ ์์ ๊ฐฏ์๋ฅผ ์ถ๋ ฅํ๊ณ ๋ ํ์ผ์ ํฉ์น ํ์ผ์ ์์ฑํ๋ ํ๋ก๊ทธ๋จ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // qsort()ํจ์, atoi()ํจ์
#include <string.h> // strcmp()ํจ์
#define SIZE 300
#define MAX 32767
int compare(const void* a, const void* b) {
return (strcmp((char*)a, (char*)b));
}
int main(void) {
char dat_filename1[SIZE];
char dat_filename2[SIZE];
char dat_filename3[SIZE];
char temp[SIZE];
int size1 = 0, size2 = 0, size3 = 0;
int arr[MAX];
int i = 0;
FILE* fp1;
printf("์ฒซ ๋ฒ์งธ ํ์ผ๋ช
? ");
gets_s(dat_filename1, SIZE);
fp1 = fopen(dat_filename1, "rb");
// ์ผ์นํ๋ ํ์ผ๋ช
์ ๋ฐ๊ฒฌํ์ง ๋ชปํ๋ฉด Error!
if (fp1 == NULL) {
printf("Error!\n");
return 1;
}
// \n์ ์ฐพ์ ์นด์ดํ
ํ๊ณ arr์ ์ ์ ์ ์ฅ
while (!feof(fp1)) {
fgets(temp, SIZE, fp1);
arr[i] = atoi(temp);
i++;
if (temp[strlen(temp) - 1] == '\n') {
size1++;
}
}
printf("์ ์ %d๊ฐ๋ฅผ ์ฝ์์ต๋๋ค.\n", size1);
FILE* fp2;
printf("๋ ๋ฒ์งธ ํ์ผ๋ช
? ");
gets_s(dat_filename2, SIZE);
fp2 = fopen(dat_filename2, "rb");
// ์ผ์นํ๋ ํ์ผ๋ช
์ ๋ฐ๊ฒฌํ์ง ๋ชปํ๋ฉด Error!
if (fp2 == NULL) {
printf("Error!\n");
return 1;
}
// \n์ ์ฐพ์ ์นด์ดํ
ํ๊ณ arr์ ์ ์ ์ ์ฅ
while (!feof(fp2)) {
fgets(temp, SIZE, fp2);
arr[i] = atoi(temp);
i++;
if (temp[strlen(temp) - 1] == '\n') {
size2++;
}
}
printf("์ ์ %d๊ฐ๋ฅผ ์ฝ์์ต๋๋ค.\n", size2);
FILE* fp3;
printf("์ ์ฅํ ํ์ผ๋ช
? ");
gets_s(dat_filename3, SIZE);
fp3 = fopen(dat_filename3, "w+b");
// arr์ ์ ์ฅ๋ ์ ์๋ฅผ qsortํจ์๋ฅผ ์ฌ์ฉํด ์ ๋ ฌ
qsort((void*)arr, i, sizeof(arr[0]), compare);
// ์ ๋ ฌ๋ arr๋ฅผ fp3์ ์
๋ ฅ
for (int k = 0; k < i; k++) {
fprintf(fp3, "%d\n", arr[k]);
}
printf("์ ์ %d๊ฐ๋ฅผ ์ ์ฅํ์ต๋๋ค.\n", i);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
[์คํ ๊ฒฐ๊ณผ]
์ฒซ ๋ฒ์งธ ํ์ผ๋ช
? a.dat
์ ์ 181๊ฐ๋ฅผ ์ฝ์์ต๋๋ค.
๋ ๋ฒ์งธ ํ์ผ๋ช
? b.dat
์ ์ 75๊ฐ๋ฅผ ์ฝ์์ต๋๋ค.
์ ์ฅํ ํ์ผ๋ช
? c.dat
์ ์ 256๊ฐ๋ฅผ ์ ์ฅํ์ต๋๋ค.