변수에 한글이 깨져서 들어갑니다.

niceboildbeef의 이미지

	//전역 포인터 변수
	char *korChar[WORD_COUNT];
	char *currentWord[6];
 
	//파일읅 읽어 변수에 넣는 함수의 내용
 
	FILE fr*;
	char *c;
	char line[18728];
	fr = fopen("wordkor.txt","rt");
	if (fr) {
		if(fgets(line,18728,fr) != NULL)
		{
			c = strtok(line," ");
		}
	}
	fclose(fr);
	while (c) {
		korChar[count] = c;
		printf("%s ",korChar[count]); //정상 작동
		c = strtok(NULL,  " ");
		count++;
	}
 
	//다른 함수에서 랜덤으로 문자 출력
 
	for(i = 0; i < 6; i++)
	{
		int random;
		srand((time(NULL)+i)*100);
		random = rand() % WORD_COUNT;
		currentWord[i] = korChar[random];
		printf("%s ", currentWord[i]);
	}

코드를 위와 같이 작성했습니다. wordkor.txt 파일에는 띄어쓰기를 구분으로 하여 2808개의 한글 단어가 들어가있습니다.

printf("%s ",korChar[count]); << 해당 라인에서는 모든 단어가 제대로 출력이 되는데 다른 함수에서 랜덤으로 문자를 출력하려고 하면 잘못된 문자가 있다고 blank를 출력하거나 아예 깨진 문자를 출력하기도 합니다.

왜 이러는 걸까요??

익명 사용자의 이미지

한글은 저렇게 영어 자르듯이 자르시면 안됩니다.

사용하시는 환경에서 한글의 인코딩은 무엇인가요? CP949나 EUC-KR? UTF-8? UTF16LE? UTF-32 또는 UCS4?
한글은 멀티바이트 캐릭터, 즉 한 글자가 여러 바이트로 구성되어 있습니다.
따라서 글자를 자를 때에는 인코딩에 따라 맞게 잘라야 합니다.
UTF-32는 한 글자에 4바이트이므로 기계적으로 막 잘라도 되지만
CP949는 영어는 1바이트, 한글 및 특수문자는 2바이트이고
UTF-8은 가변,
UTF16LE는 2바이트 또는 4바이트,
여러가지가 있습니다.

WinAPI등의 기존 문자열 관련 라이브러리 쓰시는게 좋고,
현재 환경에서 사용하는 인코딩이 뭔지 확인을 하시고,
컴퓨터에서 한글(그리고 문자열)의 표현 방법에 대해 검색하면서 공부를 하셔야 할것 같네요.

HDNua의 이미지

답변 잘 봤습니다. 덧글 달아서 남겨놔야겠어요.

저는 이렇게 생각했습니다.

niceboildbeef의 이미지

CP949 인코딩이라 2바이트씩 자르도록 했는데 같은 문제가 발생하더라구요 -..-;;

저 코드를 따로 함수로 만들어 호출하지 않고 그냥 코드 블럭 사이에 끠워넣었더니 정상작동하는..

예상치 못한 방법으로 해결했지만 좋은 답변 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.