2014-06-05 4 views
0

그리스어로 쓰여진 약 540000 단어를 포함하는 grwords.txt이라는 파일을 읽는 함수를 만들어야합니다.그리스어 단어를 대문자로 변환

이 단어를 대문자로 변환하고 char **words이라는 배열을 채워야합니다.

이것은 내가 지금까지 가지고있는 것입니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <time.h> 
#include <windows.h> 
#include <ctype.h> 


void fp(); 

int main(int argc, char *argv[]) { 

    SetConsoleOutputCP(1253); 

    fp(); 
    return 0; 
} 

void fp(){ 
    char **words; 
    words = malloc(546490 * sizeof(int *)); 
    for (i = 0; i < 546490; i++) 
      words[i] = malloc(24 * sizeof(int)); 
    FILE *file; 
    char *word; 
    size_t cnt; 

    file = fopen("grwords.txt", "rt"); 
    if (file == NULL){ 
     printf("File cannot be opened.\n"); 
     exit(1); 
    } 
    cnt = 0; 
    while (1==fscanf(file, "%24s",word)){ 
     if (cnt == 546490) 
      break; 
     strcpy(words[cnt++], word); 
    } 
    fclose(file); 
} 

저는 여전히 포인터를 파악하려고합니다. 나는 &이 값에서 포인터를 만들고 포인터에서 값을 * 생성한다는 것을 알고 있습니다. 프로그램을 업데이트하면 파일의 단어로 배열이 성공적으로 채워집니다! 나는 그리스어 소문자를 대문자로 변환하는 방법을 아직 모른다.

+0

그리스어 파일은 어떻게 인코딩됩니까? UTF8, UTF16, Windows CP 1253? 나는'toupper'이 "즉시"작동한다는 것을 확신하지 못한다. (또는 이것을 해결하기위한 과제의 일부입니까?) – usr2564301

+0

'words'는 ​​메모리의 임의의 위치를 ​​가리키는 포인터입니다. 사용하기 전에 메모리를 할당하고 할당해야합니다. 'malloc' 함수를 조사하십시오. –

+0

그것은 CP 1253입니다. 정확하게 모르겠습니다. 과제는 전환 만 언급하고 다른 것은 언급하지 않습니다. @ 캐리 이것은 동적 메모리 할당으로 만 수행 할 수 있습니까? – user3601507

답변

1

그리스어 단어는 플랫폼에 따라 다를 수 있습니다.

먼저 파일 처리가 어떻게 작동하는지 이해해야합니다.

Georgios 
Samaras 
Γιώργος 
Σαμαράς 

코드로 실행됩니다 :

./exe test.txt 
Georgios 
GEORGIOS 
done with this word 
Samaras 
SAMARAS 
done with this word 
Γιώργος 
Γιώργος 
done with this word 
Σαμαράς 
Σαμαράς 
done with this word 

당신이 볼 수 있듯이, 나는 그리스를 읽을 수이있는 test.txt 파일의 경우

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

#define bufSize 1024 // max lenght of word 
// we are going to receive the .txt from cmd line 
int main(int argc, char *argv[]) 
{ 
    FILE *fp; 

    // Assume file has max 10 words 
    const size_t N = 10; 

    // Allocate a 2D array of N rows 
    // and bufSize columns. 
    // You can think of it like an array 
    // of N strings, where every string 
    // has, at most, bufSize length. 
    char buf[N][bufSize]; 

    // make sure we got the .txt 
    if (argc != 2) 
    { 
    fprintf(stderr, 
      "Usage: %s <soure-file>\n", argv[0]); 
    return 1; 
    } 

    // open the file 
    if ((fp = fopen(argv[1], "r")) == NULL) 
    { /* Open source file. */ 
    perror("fopen source-file"); 
    return 1; 
    } 

    // we will use that for toupper() 
    char c; 

    // counters 
    int i = 0, j; 


    while (fscanf(fp, "%1024s", buf[i]) == 1) 
    { /* While we don't reach the end of source. */ 
    /* Read characters from source file to fill buffer. */ 

    // print what we read 
    printf("%s\n", buf[i]); 

    j = 0; 
    // while we are on a letter of word placed 
    // in buf[i] 
    while (buf[i][j]) 
    { 
     // make the letter capital and print it 
     c = buf[i][j]; 
     putchar (toupper(c)); 
     j++; 
    } 
    i++; 
    printf("\ndone with this word\n"); 
    } 
    // close the file 
    fclose(fp); 

    return 0; 
} 

: 여기에 내가 쓴 것입니다 단어는 대문자로 변환하지 못했습니다.

파일 처리 방법을 알게되면이라는 파일을 읽을 때 와이드 문자를 사용해야합니다.

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
#include <wchar.h> 
#include <wctype.h> 
#include <locale.h> 

#define bufSize 1024 

int main(int argc, char *argv[]) 
{ 
    setlocale(LC_CTYPE, "en_GB.UTF-8"); 
    FILE *fp; 
    const size_t N = 15; 
    wchar_t buf[N][bufSize]; 
    if (argc != 2) 
    { 
    fprintf(stderr, 
      "Usage: %s <soure-file>\n", argv[0]); 
    return 1; 
    } 
    if ((fp = fopen(argv[1], "r")) == NULL) 
    { 
    perror("fopen source-file"); 
    return 1; 
    } 
    wchar_t c; 
    int i = 0, j; 
    while (fwscanf(fp, L"%ls", buf[i]) == 1) 
    { 
    wprintf(L"%ls\n\n", buf[i]); 
    j = 0; 
    while (buf[i][j]) 
    { 
     c = buf[i][j]; 
     putwchar (towupper(c)); 
     j++; 
    } 
    i++; 
    wprintf(L"\ndone with this word\n"); 
    } 
    fclose(fp); 
    return 0; 
} 

이제 출력은 이것이다 :

그래서, 바로 위의 코드를 수정하여, 우리가 얻을

난 당신이 읽는 기능을 만들 수 있습니다 볼
Georgios 

GEORGIOS 
done with this word 
Samaras 

SAMARAS 
done with this word 
Γιώργος 

ΓΙΏΡΓΟΣ 
done with this word 
Σαμαράς 

ΣΑΜΑΡΆΣ 
done with this word 

말. C에서 간단한 함수 예제가 필요하면 내 의사 사이트 here을 방문하십시오. I는 상술 한 2 차원 어레이로서

,이 사진은 도움이 될 :

N (4 같음) 행의 수이고, M은 열 번호

enter image description here

(동일 내지 5). 위의 코드에서 N은 N이고 M은 bufSize입니다. 더 자세한 내용은 here, 2D 배열의 동적 할당 코드도 있습니다.

나는 당신이 에 있음을 알고 있습니다. 나는 코드를 우분투에 테스트했다.

의 경우이 question을 잘 살펴볼 수 있습니다.

위의 내용을 모두 읽고 이해하면 동적 메모리 관리로 무엇을 요구했는지 확인할 수 있습니다.이 코드 리눅스 (우분투 12.04에서 테스트)에서 작동 할 것으로 예상된다

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <wchar.h> 
#include <wctype.h> 
#include <locale.h> 

#define bufSize 1024 

wchar_t **get(int N, int M); 
void free2Darray(wchar_t** p, int N); 

int main(int argc, char *argv[]) 
{ 
    setlocale(LC_CTYPE, "en_GB.UTF-8"); 
    FILE *fp; 
    const size_t N = 15; 
    wchar_t** buf = get(N, bufSize); 
    if (argc != 2) 
    { 
    fprintf(stderr, 
      "Usage: %s <soure-file>\n", argv[0]); 
    return 1; 
    } 
    if ((fp = fopen(argv[1], "r")) == NULL) 
    { 
    perror("fopen source-file"); 
    return 1; 
    } 
    wchar_t c; 
    int i = 0, j; 
    while (fwscanf(fp, L"%ls", buf[i]) == 1) 
    { 
    wprintf(L"%ls\n", buf[i]); 
    j = 0; 
    while (buf[i][j]) 
    { 
     c = buf[i][j]; 
     putwchar (towupper(c)); 
     j++; 
    } 
    i++; 
    wprintf(L"\ndone with this word\n"); 
    } 
    fclose(fp); 
    // NEVER FORGET, FREE THE DYNAMIC MEMORY 
    free2Darray(buf, N); 
    return 0; 
} 

// We return the pointer 
wchar_t **get(int N, int M) /* Allocate the array */ 
{ 
    /* Check if allocation succeeded. (check for NULL pointer) */ 
    int i; 
    wchar_t **table; 
    table = malloc(N*sizeof(wchar_t *)); 
    for(i = 0 ; i < N ; i++) 
     table[i] = malloc(M*sizeof(wchar_t)); 
    return table; 
} 

void free2Darray(wchar_t** p, int N) 
{ 
    int i; 
    for(i = 0 ; i < N ; i++) 
     free(p[i]); 
    free(p); 
} 

참고하지 Windows에서 (승 7에서 테스트).

관련 문제