chapter 13 파일처리와 전처리기 496p 실전예제
내답: 어딘가 엉성한 그녀의 코드
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
void main()
{
FILE* Cf = fopen("C:\\Windows\\win.ini", "rb");
char str[1024];
if (Cf)
{
int start = ftell(Cf); //0
fseek(Cf, 0, SEEK_END);
int end = ftell(Cf);//124
fseek(Cf, 0, SEEK_SET);
fread(str, 1, (end - start), Cf);
FILE* Df = fopen("D:\\win.ini", "wb");
fwrite(str, 1, (end - start), Df);
}
}
해답: 사용메모리 최소화
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include <string.h>
void main()
{
FILE* fsrc = fopen("C:\\Windows\\win.ini", "rb");
if (fsrc)
{
FILE* fdest = fopen("D:\\win.ini", "wb");
if (fdest)
{
char buf[32];
while (1)
{
int r = fread(buf, 1, sizeof(buf), fsrc);
if (r)
{
fwrite(buf, 1, r, fdest);
}
else
break;
}
fclose(fdest);
}
else
{
printf("Write Error: %d, %s", errno, strerror(errno));
}
fclose(fsrc);
}
else
printf("Write Error: %d, %s", errno, strerror(errno));
}
502p chapter 13 파일처리와 전처리기 연습문제
1. #include
2. FILE* (파일포인터)
3. 3
4. 1
5. 2
6. 2
7. 파일크기가 2GB를 넘어가는 경우때문에
8. \n으로 구분하고 \n을 포함한다.
9. #include는 포함한 파일의 내용을 그대로 #include 지시문과 교체하기때문에 헤더파일에 함수나 객체를 정의하게 되면 재정의 오류가 난다.
10.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include <string.h>
int GetSize(const char* path)
{//경로를 인자로 받아 파일의 크기를 반환하는 함수
FILE* f = fopen(path, "rb");
int start = ftell(f);
fseek(f, 0, SEEK_END);
int end = ftell(f);
if (end - start > 2000000)
return -1;
return end - start;
}
void main()
{
printf("Byte: %d",GetSize("D:\\win.ini"));
}
11.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include <string.h>
int GetLines(const char* path)
{// 몇 줄로 되어있는지 반환하는 함수
FILE* f = fopen(path, "rb");
char str[1024];
int count = 0;
fread(str, 1, sizeof(str), f);
for(int i = 0; i < 1024; i++)
{
if (str[i] == '\n')
count++;
}
return count;
}
void main()
{
printf("줄 수: %d", GetLines("D:\\TestFile.txt")+1);
}
fread로 만들어봤는데 너무 구려서 다시 만들어봄.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include <string.h>
int GetLines(const char* path)
{// 몇 줄로 되어있는지 반환하는 함수
FILE* f = fopen(path, "rb");
int count = 0;
while (f)
{
int a = fgetc(f);
if (a == '\n')
count++;
else if (a == EOF)
break;
}
return count;
}
void main()
{
printf("줄 수: %d", GetLines("D:\\win.ini") + 1);
}
훨씬 낫네...
12. 이코드의 단점은 fputs로 직접 텍스트를 입력해야 정상적으로 출력된다는것. 안그럼 오류난다 ㅎ
그리고 나는 감으로 코딩을 하기 때문에 다시 풀수 있을것같지 않다.. 얻어걸린거 ㅅ같다....
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include <string.h>
int PrintLine(const char* path, int line)
{// 텍스트파일의 경로와 줄 번호를 인자로 받아서 해당 파일의 줄 번호에 해당하는 줄을 출력
FILE* f = fopen(path, "wb+");
// fgets는 파일 위치 표시자의 위치부터 한줄을 읽은후 문자배열에 저장...
// 파일 위치 표시자 = '\n'의 다음 부터 그 다음 '\n'까지 읽는다.
fputs("logically\r\nThinking\r\nYou can do it", f);
int seek = SEEK_SET;
char str[128];
for(int i =0; i<=line;i++)
{
fseek(f, 0, seek);
fgets(str, sizeof(str), f);
seek = SEEK_CUR;
if (i==line)
{
printf("%s", str);
}
}
fclose(f);
}
void main()
{
PrintLine("D:\\TestFile.txt",1);
}
13. 하 문제가 몬 소린지 아예 모르겠음..
<달력출력>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include <string.h>
#include <time.h>
int GetWDay(int year, int month, int day)
{
struct tm t;
memset(&t, 0, sizeof(t));
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
mktime(&t);
return t.tm_wday;
}
int GetLastDay(int year, int month)
{
int lastday[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int Leap = 0;
if (month == 2)
{
if (year % 400 == 0)
Leap = 1;
else if (year % 100 == 0)
Leap = 0;
else if (year % 4 == 0)
Leap = 1;
}
return lastday[month - 1] + Leap;
}
void PrintCalendar(int year, int month)
{
printf(" %d년 %d월\r\n", year, month);
printf(" --------------------------\r\n");
printf(" 일 월 화 수 목 금 토\r\n");
printf(" --------------------------");
int wday = GetWDay(year, month, 1);
int lastday = GetLastDay(year, month);
for (int i = 0; i < wday + lastday; i++)
{
if (i % 7 == 0)
printf("\r\n");
int day = i - wday + 1;
if (day > 0)
printf("%4d", day);
else
printf("%s", " ");
}
}
int main()
{
int year, month;
do
{
printf("연도를 입력하세요 > ");
scanf("%d", &year);
} while (year < 1);
do
{
printf("월을 입력하세요 > ");
scanf("%d", &month);
} while (month < 0);
printf("\r\n");
if (month == 0)
{
for (int i = 1; i <= 12; i++)
{
PrintCalendar(year, i);
printf("\r\n\r\n");
}
}
else
PrintCalendar(year, month);
}
<문자열 검색>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <string.h>
int main()
{
char path[256];
char find[64];
printf("파일 경로를 입력하세요 > ");
scanf("%s", path);
printf("찾을 문자열을 입력하세요 > ");
scanf("%s", find);
FILE* f = fopen(path, "rb");
if (f)
{
int rownum = 0;
int findnum = 0;
char str[1024];
while (1)
{
rownum++;
int r = fgets(str, sizeof(str), f);//파일위치표시자의 현재위치에서 파일을 읽으므로
// 이렇게 반복되면 자동으로 다음 줄을 읽게됨...
if (r)
{
if (strstr(str, find))
{
printf("%d. %s", rownum, str);
findnum++;
}
}
else
{
break;
}
}
fclose(f);
if (!findnum)
printf("Not Found!");
}
else
printf("Read Error: %d, %s", errno, strerror(errno));
}
<로또번호 생성 프로그램>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
int main()
{
int count = 0;
char lotto[45] = { 0 };
srand(time(NULL));
while (1)
{
int num = rand() % 45 + 1;
if (!lotto[num - 1])
{
lotto[num - 1] = 1;
count++;
if (count == 6)
{
break;
}
}
}
for (int i = 0; i < 45; i++)
{
if (lotto[i])
printf("%d ", i + 1);
}
}
난생처음 c 프로그래밍 끝.