loadfile.c
loadfile.exe
/*
입력 파일 전체를 memory로 load
*/
unsigned char *loadFile(char *filename)
{
long n;
unsigned char *p;
FILE *fp;
fp = fopen(filename, "rb"); // [주의] 'fp'를 "rb"로 open
if (!fp) {
p = (unsigned char *) malloc(strlen(filename)+1);
strcpy(p, filename); // file명 자체를 return
return p;
}
fseek(fp, 0L, 2);
n = ftell(fp); // n: byte size of file 'fp'
if (n < 3) return NULL;
p = (unsigned char *) malloc(n+1); // memory allocation
if (p == NULL) return NULL;
// memset(p, 0, n+1);
fseek(fp, 0L, 0);
fread(p, sizeof(unsigned char), n, fp); // read 'fp' to 'p'
*(p+n) = '\0';
fclose(fp);
return p;
}
//============================================
//==== test.txt 파일 내용
ABCDabcd0123가각간갇갈
//==== $ od -x test.txt // 16진수로 출력
0000000 4241 4443 6261 6463 3130 3332 a1b0 a2b0
0000020 a3b0 a4b0 a5b0 0a0d 0a0d
0000032
//==== $ od -c test.txt
0000000 A B C D a b c d 0 1 2 3 260 241 260 242
0000020 260 243 260 244 260 245 \r \n \r \n
0000032
//==== $ od -o test.txt
0000000 041101 042103 061141 062143 030460 031462 120660 121260
0000020 121660 122260 122660 005015 005015
0000032
<참고> 아래 파일들은 test.txt로부터 "윈도 메모장"을 이용하여 유니코드로 저장하였음.
리눅스에서는 'iconv' 명령으로 코드변환 --> 차이점 BOM 표시
test.txt
test-BE.txt
test-LE.txt
test-UTF8.txt
<실습> test.txt를 iconv를 이용하여 코드변환 했을 때 위 윈도에서 메모장으로 변환 결과와 차이점은 무엇인가?
<참고> DOS(윈도)와 리눅스에서 작성한 텍스트 파일은 '줄바꿈 문자' 부분에 차이가 있음.
DOS(윈도)의 경우 CR-LF, 리눅스(unix계열)는 LF
첫댓글 [실습 1] dump한 파일 내용 전체를 8진수 또는 16진수로 출력하는 프로그램 작성 --> linux의 od 명령
[실습 2] dump한 파일의 word count --> linux의 wc 명령
iconv 사용 예
$ iconv --help
$ iconv --list 또는 iconv -l
$ iconv -f utf8 t- euckr test.txt euckr.txt
$ iconv -f utf8 t- cp949 test.txt euckr.txt
<참고> iconv 라이브러리(libiconv.a)를 이용하여 C언어 main() 함수에서 iconv() 함수를 직접 호출하는 방법은?
iconv_open(), iconv(), iconv_close()
"$ od -o" 8진수 출력은... 역워드 형식의 16비트(HDD에 기록된 순서)를 3비트씩 끊어서 8진수로 출력하였음!
예) "AB"는 역워드 형식에서 0x4241 이고, 2진수 0100 0010 0100 0001 --> 0 100 001 001 000 001 (ox041101)