2014-06-05 103 views
0

我必須創建一個函數,讀取一個名爲grwords.txt的文件,其中包含大約540000個字,用希臘字母書寫。將希臘字轉換爲大寫

我必須將這些單詞轉換爲大寫,然後填充名爲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.我不完全清楚。該作業只提到轉換,沒有別的。 @Carey這隻能用動態內存分配來完成嗎? – user3601507

回答

1

處理希臘字可能取決於您的平臺。

首先,您需要了解文件處理的工作原理。下面是我寫的:

#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; 
} 

對於這個test.txt文件:

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 

正如你所看到的,我可以讀希臘的話,但沒有把它們轉換成大寫字母。

一旦你得到了文件處理的方式,你需要用寬字符來讀取一個帶有希臘詞的文件。

所以,僅通過修改上面的代碼,我們得到:

#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

至於我上面提到的2D陣列,該圖像可能有幫助:

enter image description here

其中,N是行數(等於4)的數量,M是列數(等於5 )。在上面的代碼中,N是N,M是bufSize。我解釋更多here,你是否也可以找到動態分配二維數組的代碼。

我知道看到你在Windows。我測試了Ubuntu中的代碼。

對於Windows你可能想看看這個question

因此,在閱讀完上述內容並理解它們之後,您可以看到您對動態內存管理所要求的內容。

#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); 
} 

注意,此代碼預計將在的Linux(在Ubuntu 12.04測試)工作,而不是在Windows(在Win 7測試)。