2015-02-07 110 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; 
}