본문 바로가기
Programming/C#

C - 구구단 파일 입출력

by NAMP 2009. 3. 27.
<문제의 발단>

다음과 같은 코드를 실행한다.

#include <stdio.h>

#define path "d:\\gugu.txt"
#define max 10

int main(){
int i,j;
FILE *f=fopen(path,"w");
for (i=1;i<max;i++)
{
for (j=1;j<max;j++)
{
fprintf(f,"%2d * %2d = %2d",i,j,i*j);
if (!(j%3))
{
fprintf(f,"\n");
}else{
fprintf(f," // ");
}
}
for (j=0;j<25;j++)
{
fprintf(f,"#");
}
fprintf(f,"\n");
}
fclose(f);
return 0;
}

그러면 다음과 같은 파일이 생성된다. 

 1 *  1 =  1 //  1 *  2 =  2 //  1 *  3 =  3
 1 *  4 =  4 //  1 *  5 =  5 //  1 *  6 =  6
 1 *  7 =  7 //  1 *  8 =  8 //  1 *  9 =  9
#########################
 2 *  1 =  2 //  2 *  2 =  4 //  2 *  3 =  6
 2 *  4 =  8 //  2 *  5 = 10 //  2 *  6 = 12
 2 *  7 = 14 //  2 *  8 = 16 //  2 *  9 = 18
#########################
 3 *  1 =  3 //  3 *  2 =  6 //  3 *  3 =  9
 3 *  4 = 12 //  3 *  5 = 15 //  3 *  6 = 18
 3 *  7 = 21 //  3 *  8 = 24 //  3 *  9 = 27
#########################
 4 *  1 =  4 //  4 *  2 =  8 //  4 *  3 = 12
 4 *  4 = 16 //  4 *  5 = 20 //  4 *  6 = 24
 4 *  7 = 28 //  4 *  8 = 32 //  4 *  9 = 36
#########################
 5 *  1 =  5 //  5 *  2 = 10 //  5 *  3 = 15
 5 *  4 = 20 //  5 *  5 = 25 //  5 *  6 = 30
 5 *  7 = 35 //  5 *  8 = 40 //  5 *  9 = 45
#########################
 6 *  1 =  6 //  6 *  2 = 12 //  6 *  3 = 18
 6 *  4 = 24 //  6 *  5 = 30 //  6 *  6 = 36
 6 *  7 = 42 //  6 *  8 = 48 //  6 *  9 = 54
#########################
 7 *  1 =  7 //  7 *  2 = 14 //  7 *  3 = 21
 7 *  4 = 28 //  7 *  5 = 35 //  7 *  6 = 42
 7 *  7 = 49 //  7 *  8 = 56 //  7 *  9 = 63
#########################
 8 *  1 =  8 //  8 *  2 = 16 //  8 *  3 = 24
 8 *  4 = 32 //  8 *  5 = 40 //  8 *  6 = 48
 8 *  7 = 56 //  8 *  8 = 64 //  8 *  9 = 72
#########################
 9 *  1 =  9 //  9 *  2 = 18 //  9 *  3 = 27
 9 *  4 = 36 //  9 *  5 = 45 //  9 *  6 = 54
 9 *  7 = 63 //  9 *  8 = 72 //  9 *  9 = 81
#########################

문제는 여기서 다시 파일의 내용을 읽어들일때 생긴다. 
다음과 같은 코드로 읽어서 화면에 출력한다. 

#include <stdio.h>
#include <string.h>

#define path "d:\\gugu.txt"
int main(){
char read[100]="\0";
char string[10000]="\0";
FILE*f=fopen(path,"r");
while (!feof(f))
{
fgets(read,sizeof(read),f);
strcat(string,read);
}
puts(string);
fclose(f);
return 0;
}

출력 결과는 다음과 같다. 

 
4 *  7 = 28 //  4 *  8 = 32 //  4 *  9 = 36
#########################
 5 *  1 =  5 //  5 *  2 = 10 //  5 *  3 = 15
 5 *  4 = 20 //  5 *  5 = 25 //  5 *  6 = 30
 5 *  7 = 35 //  5 *  8 = 40 //  5 *  9 = 45
#########################
 6 *  1 =  6 //  6 *  2 = 12 //  6 *  3 = 18
 6 *  4 = 24 //  6 *  5 = 30 //  6 *  6 = 36
 6 *  7 = 42 //  6 *  8 = 48 //  6 *  9 = 54
#########################
 7 *  1 =  7 //  7 *  2 = 14 //  7 *  3 = 21
 7 *  4 = 28 //  7 *  5 = 35 //  7 *  6 = 42
 7 *  7 = 49 //  7 *  8 = 56 //  7 *  9 = 63
#########################
 8 *  1 =  8 //  8 *  2 = 16 //  8 *  3 = 24
 8 *  4 = 32 //  8 *  5 = 40 //  8 *  6 = 48
 8 *  7 = 56 //  8 *  8 = 64 //  8 *  9 = 72
#########################
 9 *  1 =  9 //  9 *  2 = 18 //  9 *  3 = 27
 9 *  4 = 36 //  9 *  5 = 45 //  9 *  6 = 54
 9 *  7 = 63 //  9 *  8 = 72 //  9 *  9 = 81
#########################
#########################

Press any key to continue


마지막에 
###########################
이 한줄이 더 추가된다는 점이 문제다. 



< 관찰 > 

만들어진 파일의 내용을 메모장으로 보면 다음과 같다. 
 8 *  1 =  8 //  8 *  2 = 16 //  8 *  3 = 24
 8 *  4 = 32 //  8 *  5 = 40 //  8 *  6 = 48
 8 *  7 = 56 //  8 *  8 = 64 //  8 *  9 = 72
#########################
 9 *  1 =  9 //  9 *  2 = 18 //  9 *  3 = 27
 9 *  4 = 36 //  9 *  5 = 45 //  9 *  6 = 54
 9 *  7 = 63 //  9 *  8 = 72 //  9 *  9 = 81
#########################

메모장플러스로 보면 다음과 같이 CR과 LF를 볼수 있다. 



CR : Carriage return
LF : NL line Feed, new line

Emeditor 를 통해서, CRLF의 값을 읽어보면 다음과 같다. 


printf("%d\n",feof(f));   이렇게 찍어보면, 
feof(f)의 값은 = 16 이다. 
그러나, 그 어디에서도 0x10 을 찾을 수 없었다.IDA로 열어보았지만, 실패. 원래 없는것일까....

seg000:00000AE0  20 00 34 00 35 00 20 00  2F 00 2F 00 20 00 20 00   .4.5. ././. . .
seg000:00000AF0  39 00 20 00 2A 00 20 00  20 00 36 00 20 00 3D 00  9. .*. . .6. .=.
seg000:00000B00  20 00 35 00 34 00 0D 00  0A 00 20 00 39 00 20 00   .5.4.
.
. .9. .
seg000:00000B10  2A 00 20 00 20 00 37 00  20 00 3D 00 20 00 36 00  *. . .7. .=. .6.
seg000:00000B20  33 00 20 00 2F 00 2F 00  20 00 20 00 39 00 20 00  3. ././. . .9. .
seg000:00000B30  2A 00 20 00 20 00 38 00  20 00 3D 00 20 00 37 00  *. . .8. .=. .7.
seg000:00000B40  32 00 20 00 2F 00 2F 00  20 00 20 00 39 00 20 00  2. ././. . .9. .
seg000:00000B50  2A 00 20 00 20 00 39 00  20 00 3D 00 20 00 38 00  *. . .9. .=. .8.
seg000:00000B60  31 00 0D 00 0A 00 23 00  23 00 23 00 23 00 23 00  1.
.
.#.#.#.#.#.
seg000:00000B70  23 00 23 00 23 00 23 00  23 00 23 00 23 00 23 00  #.#.#.#.#.#.#.#.
seg000:00000B80  23 00 23 00 23 00 23 00  23 00 23 00 23 00 23 00  #.#.#.#.#.#.#.#.
seg000:00000B90  23 00 23 00 23 00 23 00  0D 00 0A 00                 #.#.#.#.


아니면 0x10이 아닐 수 있다. 

In computingend-of-file, commonly abbreviated EOF, is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or stream.

In the C Standard Library, file access and other I/O functions may return a value equal to the symbolic value (macro) EOF to indicate that an end-of-file condition has occurred. The actual value of EOF is a system-dependent negative number, commonly -1, which is guaranteed to be unequal to any valid character code.  (위키)


보통 -1이라고 하는군. 


한줄에 # 가 25개가 있다. 

다라서, 출력 소스에서,  (char read[x]="\0";)  x의 값을 변경하면서 실행해 본다. 


1 - 무한으로 입력이 계속된다. EOF를 만날수 없다는 의미이다.

2 - 한줄은 더 띄어지나, 출력되는 값은 없다.

3 - 하나의 #이 더 출력된다. 

4 - 위와 같음

5 - 위와 같음

6 - 2의 경우와 같음

7 - 하나의 #

8 - ####

9 - #

10 - #######

11 - #####

12 - ###

13 - #

14 - # 12개

15 - 11개

16 - 10개

17 - 9개

24 - 2개

25 - 1개

26 - 한 줄 띄어짐. 


............. 결론은 나중에 해야겠다.....








































'Programming > C#' 카테고리의 다른 글

배열 포인터  (0) 2009.06.05
C - 지렁이 & 지뢰 찾기 게임 만들기  (0) 2009.03.29
Maximum value of each Radix ... ?  (0) 2009.03.25
c - Students' score management program.  (0) 2009.03.21
c - Baseball Game.  (0) 2009.03.20

댓글