# 12์ฅ Programming ํ์ด (1)
488p_1๋ฒ)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[100];
int c;
int i = 0;
printf("ํ์ผ๋ช
์ ์
๋ ฅํ์์ค: ");
gets_s(name, 100);
FILE* fp;
fopen_s(&fp, "good.txt", "r");
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์คํจ\n");
exit(1);
}
printf("%d ", i++);
while ((c = fgetc(fp)) != EOF)
{
if (ftell(fp) == 17)
{
printf("%d ", i);
}
putchar(c);
}
fclose(fp);
return 0;
}
488p_2๋ฒ)
#include <stdio.h>
int main()
{
int c;
int n = 0;
int i = 0;
int sum = 0;
int average = 0;
FILE* fp;
fopen_s(&fp, "numbers.txt", "r");
while (fscanf_s(fp, "%d", &n) != EOF)
{
sum += n;
i++;
}
average = sum / i;
printf("์ ์ ๊ฐ์: %d\n", i);
printf("์ ์ ํฉ: %d\n", sum);
printf("ํ๊ท : %d\n", average);
fclose(fp);
return 0;
}
488p_3๋ฒ)
#include <stdio.h>
int main()
{
int line_cnt = 0;
int c = 0;
char index[100];
FILE* fp;
fopen_s(&fp,"file.txt", "r");
while (fgets(index, 100, fp) != 0)
{
line_cnt++;
printf("%s", index);
}
printf("\n\n****** ๋ผ์ธ์ ๊ฐ์: %d ******\n", line_cnt);
fclose(fp);
return 0;
}
489p_4๋ฒ)
#include <stdio.h>
#include <string.h>
int main()
{
int exits = 0;
char x[20];
char y[20];
FILE* one_fp;
FILE* two_fp;
printf("์ฒซ ๋ฒ์งธ ํ์ผ๋ช
: ");
gets_s(x, 20);
printf("๋ ๋ฒ์งธ ํ์ผ๋ช
: ");
gets_s(y, 20);
fopen_s(&one_fp, x, "r");
fopen_s(&two_fp, y, "r");
while (!feof(one_fp))
{
if (fgetc(one_fp) != fgetc(two_fp)) //๋ ํ์ผ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ
{
exits = 1;
}
}
if (exits == 0)
{
printf("\n๋ ํ์ผ์ ์๋ก ๊ฐ์ต๋๋ค.\n");
}
else
{
printf("\n๋ ํ์ผ์ ์๋ก ๋ค๋ฆ
๋๋ค.\n");
}
fclose(one_fp);
fclose(two_fp);
return 0;
}
489p_5๋ฒ)
#include <stdio.h>
#include <string.h>
int main()
{
FILE* fp;
fopen_s(&fp, "emp.txt", "w");
char inputs[100];
while (1)
{
rewind(stdin);
gets_s(inputs, 100);
if (strcmp(inputs, " ") == 0)
{
break;
}
fputs(inputs, fp);
fputs("\n", fp);
}
fclose(fp);
return 0;
}
489p_6๋ฒ)
#include <stdio.h>
#include <string.h>
int main()
{
FILE* fp;
fopen_s(&fp, "testing.txt", "w");
char inputs[50];
char name[30];
printf("ํ์ผ๋ช
์ ์
๋ ฅํ์์ค: ");
gets_s(name, 30);
while (1)
{
rewind(stdin);
gets_s(inputs, 50);
if (strcmp(inputs, " ") == 0)
{
break;
}
fputs(inputs, fp);
fputs("\n", fp);
}
fclose(fp);
return 0;
}