2015-02-07 142 views
-1

你好傢伙請幫我理解一些東西!我有一個txt文件,我讀不同的值。我成功地做到了,但我也有一個ASCII碼,即。 KS98B2從txt文件讀取char是錯的

我想讀取它並將其存儲在一個值中。你能看看我的代碼嗎?單詞「KS98B2」應該存儲在變量「名稱」中。所以我把它作爲char來聲明。你同意嗎? 在「asc」函數裏面有一個putchar,並且打印正確,我檢查了一下,我收到了KS98B2。

但是,ASC的printf函數裏面給出的值:84122658 及主要的printf裏面給出的值:24

是的,我把%d printf和名稱是一個字符,但它是如何可能變量是不一樣的?我怎樣才能使它工作?請幫幫我 !

#include <stdio.h> 
 
#include <stdlib.h> 
 
#include <stdbool.h> 
 

 
FILE *file; 
 
char ch; 
 

 

 
int asc(char eow, bool *eof) { 
 
\t int var = 0; 
 
\t 
 
\t while((ch=fgetc(file))!=EOF) { 
 
\t \t putchar(ch); 
 
\t \t 
 
\t \t if ((ch >= 'A') && (ch <= 'Z')) { 
 
\t \t \t var <<= 4; 
 
\t \t \t var += (ch - 'A' + 65); 
 
\t \t } 
 
\t \t else if ((ch >= '0') && (ch <= '9')) { 
 
\t \t \t var <<= 4; 
 
\t \t \t var += (ch - '0'); 
 
\t \t } else if (ch == eow) { 
 
\t \t \t 
 
\t \t \t return var; 
 
\t \t } else { 
 
\t \t \t puts("Incorrect syntax.\n"); 
 
\t \t } 
 
\t } 
 
\t putchar('\n'); 
 
\t printf("Var inside asc %d\n", var); 
 

 
} 
 

 

 

 
int main() { 
 
\t char name; 
 
\t bool eof = false; 
 
\t \t 
 
\t if ((file = fopen("messages.txt", "r")) == NULL) { 
 
\t \t puts("WRONG FILE\n"); 
 
\t \t return 1; 
 
\t } 
 
\t while(!feof(file)) { 
 
\t \t 
 
\t \t name= asc('\n', &eof); 
 
\t 
 
\t \t printf("Var main: %d\n", name); 
 
\t } 
 
\t fclose(file); 
 
\t return 0; 
 
}

+3

我想你會發現fgetc獲得一個int類型而不是char。 – 2015-02-07 21:58:21

+0

顯示「Var inside asc ...」時,返回值無效。 – BLUEPIXY 2015-02-07 22:17:30

+0

謝謝大家的意見。所以大衛,你如何建議我將這個單詞的價值存儲在一個變量中並返回? – 2015-02-07 22:51:01

回答

1
#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <ctype.h> 

FILE *file; 
//char ch;//There is no need to be a global variable 

int asc(char eow, bool *eof) { 
    int var = 0; 
    int ch;//Type in order to compare the EOF and value must be int 

    while((ch=fgetc(file))!=EOF) { 
     if(isupper(ch)) 
      var = var * 36 + (ch - 'A' + 10); 
     else if(isdigit(ch)) 
      var = var * 36 + (ch - '0'); 
     else if (ch == eow) 
      return var; 
     else { 
      fprintf(stderr, "\nIncorrect syntax.\n"); 
     } 
    } 
    *eof = true; 
    return var; 
} 

int main(void) { 
    int name;//It must be int to receive the value of int 
    bool eof = false; 

    if ((file = fopen("messages.txt", "r")) == NULL) { 
     puts("WRONG FILE\n"); 
     return 1; 
    } 
    while(!feof(file)) { 
     name= asc('\n', &eof); 
     printf("Var main: %d\n", name); 
    } 
    fclose(file); 
    return 0; 
} 

void putdecimal(int name) { 
    int i=0; 
    int var = name; 
    int array[30]; 
    int cnt = 0; 

    while(var){ 
     array[cnt++] = var % 36; 
     var /= 36; 
    } 

    for(i = cnt-1; i>=0; i--){ 
     if(array[i]<10) 
      putchar(array[i] + '0'); 
     else 
      putchar(array[i] - 10 + 'A'); 
    } 
} 
+0

'strtol(「KS98B2」,NULL,36)' – BLUEPIXY 2015-02-08 00:50:42

+0

首先我不明白你爲什麼用36,你能解釋一下這個表達嗎? 另外,在ASCII中,字符A是十進制65,爲什麼要用10? 最後,在你的代碼中,我打印的整數是1256783438. 但是,我在ASCII中的單詞是KS98B2,它是十進制的75 83 57 56 66 50.因此,我們打印的是錯的,對嗎? – 2015-02-08 01:28:08

+0

@aredhel_vlsi 1)36:36十進制。 2)+10:'ABC ...'''A的位置意味着'10'。 3)'75 83 57 56 66 50'是6個字節。但Int是一個32位系統,預計它是4byte。你想'var << = 8'?你想把角色存儲到你爲什麼int?我認爲如果你一次只收到一個字符,問題就會解決。 – BLUEPIXY 2015-02-08 01:44:24

0

實施例所讀取的字符存儲到一個數組。

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <ctype.h> 

FILE *file; 

char *gasc(int size, char buff[size], char eow){ 
    int i = 0, ch = 0; 

    while(i < size - 1 && (ch=fgetc(file))!=EOF && ch != eow){ 
     if (isupper(ch) || isdigit(ch)){ 
      buff[i++] = ch; 
     } else { 
      fprintf(stderr, "\nIncorrect syntax.\n"); 
     } 
    } 
    buff[i] = '\0'; 
    if(i == 0 && ch == EOF) 
     return NULL; 

    return buff; 
} 

int main(void) { 
    char name[20]; 

    if ((file = fopen("messages.txt", "r")) == NULL) { 
     puts("WRONG FILE\n"); 
     return 1; 
    } 
    //is_eof is no longer necessary to represent NULL return value of gasc instead of EOF. 
    while(gasc(sizeof(name), name, '\n') != NULL) { 
     printf("'%s'\n", name); 
    } 
    fclose(file); 
    return 0; 
}