首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从文本文件中获取字符的ASCII代码?

如何从文本文件中获取字符的ASCII代码?
EN

Stack Overflow用户
提问于 2020-12-26 08:49:11
回答 1查看 1.1K关注 0票数 1

更新,大家好,谢谢大家的帮助,我的最初方法是错误的,我根本没有使用ASCII代码。抱歉,我今天放了半天假,为我编写的程序的完整代码there is no errors but the prgram is not working proberly ( this is an update of old post )写了一篇新文章,没有错误,但没有给出我想要的结果。

我唯一的问题是当我读到一个字符时,如何检查它的ASCII并存储它。

代码语言:javascript
运行
复制
#include <stdio.h>
#include <string.h>
int main()
{
 char dictionary[300];
 char ch, temp1, temp2;
 FILE *test;
 test=fopen("HW2.txt","r");
 for(int i=0;i<2000;i+=1)
  { ch=fgetc(test);
    printf("%c",ch);
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-26 09:04:10

如果我们讨论的是普通的ASCII,值从0到127,那么您的表应该如下所示:

代码语言:javascript
运行
复制
int dictionary[128] = {0};

关于你的问题:

如何检查其ASCII并将其存储

假设char是一个很小的int,它们是可互换的,您不需要任何转换。

fgetc需要一个int来处理EOF,并且尝试从包含少于2000个字节的文件中读取2000个字符会产生非常糟糕的后果,从而读取整个文件:

代码语言:javascript
运行
复制
int c;

while ((c = fgetc(test)) != EOF)
{
    if ((c > 0) && (c < 128))
    {
        dictionary[c]++;
    }
}
for (int i = 1; i < 128; i++)
{
    if (dictionary[i] > 0)
    {
        printf("%c appeared %d times\n", i, dictionary[i]);
    }
}

编辑:

再读一遍,我看到你想存储单词,而不是字符,好吧,那就有点难了,但没什么可怕的,不要把自己限制在300个字以内,使用动态记忆:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// A struct to hold the words and the
// number of times it appears
struct words
{
    size_t count;
    char *word;
};

int main(void)
{
    FILE *file;

    file = fopen("HW2.txt", "r");
    // Always check the result of fopen 
    if (file == NULL)
    {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    
    struct words *words = NULL;
    size_t nwords = 0;
    char *word = NULL;
    size_t nchars = 1;
    size_t i;
    int c;

    // while there is text to scan
    while ((c = fgetc(file)) != EOF)
    {
        if (isspace(c))
        {
            if (word != NULL)
            {
                // Search the word in the table
                for (i = 0; i < nwords; i++)
                {
                    // Found, increment the counter
                    if (strcmp(word, words[i].word) == 0)
                    {
                        words[i].count++;
                        free(word);
                        break;
                    }
                }
                // Not found, add the word to the table
                if (i == nwords)
                {
                    struct words *temp;

                    temp = realloc(words, sizeof(*temp) * (nwords + 1));
                    if (temp == NULL)
                    {
                        perror("realloc");
                        exit(EXIT_FAILURE);
                    }
                    words = temp;
                    words[nwords].word = word;
                    words[nwords].count = 1;
                    nwords++;
                }
                // Prepare the next word
                word = NULL;
                nchars = 1;
            }
        }
        else
        {
            char *temp;

            temp = realloc(word, nchars + 1);
            if (temp == NULL)
            {
                perror("realloc");
                exit(EXIT_FAILURE);
            }
            word = temp;
            word[nchars - 1] = (char)c;
            word[nchars++] = '\0';
        }
    }
    for (i = 0; i < nwords; i++)
    {
        printf("%s appeared %zu times\n", words[i].word, words[i].count);
        free(words[i].word);
    }
    free(words);
    fclose(file);
    return 0;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65454826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档