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๊ฐœ๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.
โš ๏ธ **GitHub.com Fallback** โš ๏ธ