2014-01-11 53 views
0

我想要讀取字符串以供C中的字符表進行處理,即直到遇到一個字符(0xFF)。基本上,我正在創建一個函數,用於處理文本中的靜態二進制文件,該文件使用ANSI中的自定義字符集。如何讀取字節,直到遇到某個字符爲止C

我的代碼看起來是這樣的:

short ReadText(char * Path, char * Offset) { 
    //Declare variables 
    size_t ReadResult; 
    FILE * OpenedFile; 
    short Position; 
    char CurrentChar; 
    char FormattedChar; 
    char * FileContents; 

    //Populate variables 
    OpenedFile = fopen(Path, "r"); 

    do { 
     // >>> Code to gather bytes <<< 
    } 
    //Initiate character conversion 
    do { 
     fseek(OpenedFile, Position, SEEK_SET); 
     CurrentChar = fread(

     //Character switch I/O 
     switch(CurrentChar) {enter code here 
      case 0x00: FormattedChar = ' '; 
      case 0x01: FormattedChar = 'À'; 
      case 0x02: FormattedChar = 'Á'; 
      case 0x03: FormattedChar = 'Â'; 
      // . . . 
     } 
    } 
} 

我需要知道如何做的是閱讀的偏移字節爲char*遇到原來的字符集的終止字節,直到(這是0xFF) 。我怎樣才能做到這一點?

+0

您似乎已經知道'fseek'和'fread'。你還需要什麼?閱讀手冊並查看示例。 –

+0

如何使fread識別0xFF並停止。 – AlexTheRose

+0

澄清:你想識別的是「0xFF」1)一個'char',其值可以是''\ xFF'(其中很多可能存在於一個文件中),或者2)文件結束指示_only_發生在文件末尾)? – chux

回答

1

我想你只需要添加一個調用feof(FILE *)像這樣,

fseek(OpenedFile, Position, SEEK_SET); 
if (feof(OpenedFile) != 0) { 
    /* end of the file. */ 
} else { 
    /* safe to read. */ 
} 
+0

feof知道如何查找0xFF?我如何告訴它不使用ASCII EOF? – AlexTheRose

+0

@AlexTheRose這是函數的用途,請閱讀我提供的文檔的鏈接。 –

0

嗨,你可能會想這樣,

short ReadText(char * Path, short Offset) 
{ 
    //Declare variables 
    size_t ReadResult; 
    FILE * OpenedFile; 
    short Position; 
    int byte; 
    unsigned char CurrentChar; 
    char FormattedChar; 
    char * FileContents; 

    //Populate variables 
    OpenedFile = fopen(Path, "r"); 

    if (OpenedFile == NULL) 
    { 
     printf("---File not exist----\n"); 
     return; 
    } 

    fseek(OpenedFile, Offset, SEEK_SET); 

    while(1) 
    { 
     unsigned int number; 
     byte = fscanf(OpenedFile,"%x",&number); 

     printf("read ::%x\n",number); 


     switch(number) 
     { 
      case 0x00: FormattedChar = ' ';break; 
      case 0x01: FormattedChar = 'À';break; 
      case 0x02: FormattedChar = 'Á';break; 
      case 0x03: FormattedChar = 'Â';break; 
      .................................... 
      .................................... 
     } 

     if(number == 0xFF) 
      break; 
    } 

    fclose(OpenedFile); 
} 
0

建議通過聲明的目標緩衝區處理緩衝區,其尺寸在ReadText()以外。創建一個簡單的循環,並重復呼叫fgetc(),並確保將其結果放入int以測試EOF並設置翻譯。除非只有幾個字節需要翻譯,而不是switch,否則簡單地添加一個256字節的翻譯數組。請注意,int CurrentChar將從fgetc()獲得0-255的值。

ssize_t ReadText(const char * Path, char *Dest, size_t DestSize, 
    long *Offset) { 
    //Declare variables 
    FILE * OpenedFile; 
    char FormattedChar; 
    size_t i; 

    //Populate variables 
    // OpenedFile = fopen(Path, "r"); 
    OpenedFile = fopen(Path, "rb"); // OP read a "binary file" 
    if (OpenedFile == NULL) 
    return -1; 
    if (fseek(OpenedFile, *Offset, SEEK_SET)) { 
    fclose(OpenedFile); 
    return -1; 
    } 
    DestSize--; // for a terminating '\0' 
    for (i = 0; i < DestSize; i++) { 
    int CurrentChar = fgetc(OpenedFile); 
    if (CurrentChar == EOF || CurrentChar == 0xFF) 
     break; 

    // perform translation via a switch if few 
    switch (CurrentChar) { 
     case 0x00: 
     FormattedChar = ' '; 
     break; 
     case 0x01: 
     FormattedChar = 'À'; 
     break; 
     // ... 
    } 
    // or use a lookup 
    static const char XLate[256] = 
      { ' ', 'À', 'Á', 'Â' /* and the 252 remaining */}; 
    FormattedChar = XLate[CurrentChar]; 
    Dest[i] = FormattedChar; 
    } 
    fclose(OpenedFile); 
    // If OP appears needs a terminating \0 
    Dest[i] = '\0'; 
    *Offset += i; // Update *Offset for another call if OP desires. 
    return i; 
} 
相關問題