如何从文本文件中获取字符的 ASCII 码?
How to get ASCII code for characters from a text file?
更新,大家好谢谢大家的帮助,我最初的方法是错误的,我根本没有使用ASCII码。
抱歉重放晚了今天请了半天新的完整代码post
there is no errors but the prgram is not working proberly ( this is an update of old post )
我写了这个程序,它运行没有错误但是它没有给我想要的结果
我唯一的问题是当我读取一个字符时如何检查它的 ASCII 码并存储它。
#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);
}
}
在 C 中,字符 本质上是 ,它们的 ASCII 码(或者更确切地说,它们的 char
或 unsigned char
值)。因此,一旦您阅读了一个字符,您就已经 它的 ASCII 代码。
但是,fgetc()
并不总是 return 它为您读出的字符;它可能会失败,因此它 return 是 int
,而不是 unsigned char
,如果失败,它将是 -1
。
所以:
- 您需要定义一个
int
变量来获取 fgetc()
的结果。
- 如果不是
EOF
,您可以将结果转换回 unsigned char
。那是你的字符,同时也是 ASCII 值。
PS - 我忽略了非 ASCII 字符、非拉丁语言等(但 C 在其基本标准库函数中也大多忽略了它们。)
如果我们谈论的是纯 ASCII,值从 0 到 127,您的 table 应该如下所示:
int dictionary[128] = {0};
关于您的问题:
how to check its ASCII and store it
考虑 char
是一个很小的 int
,它们可以互换,您不需要任何转换。
fgetc
想要一个 int
来处理 EOF
,并且试图从包含少于 2000 字节的文件中读取 2000 个字符可能会产生非常糟糕的后果,读取整个文件:
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个词,使用动态记忆:
#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++] = '[=12=]';
}
}
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;
}
更新,大家好谢谢大家的帮助,我最初的方法是错误的,我根本没有使用ASCII码。 抱歉重放晚了今天请了半天新的完整代码post there is no errors but the prgram is not working proberly ( this is an update of old post ) 我写了这个程序,它运行没有错误但是它没有给我想要的结果
我唯一的问题是当我读取一个字符时如何检查它的 ASCII 码并存储它。
#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);
}
}
在 C 中,字符 本质上是 ,它们的 ASCII 码(或者更确切地说,它们的 char
或 unsigned char
值)。因此,一旦您阅读了一个字符,您就已经 它的 ASCII 代码。
但是,fgetc()
并不总是 return 它为您读出的字符;它可能会失败,因此它 return 是 int
,而不是 unsigned char
,如果失败,它将是 -1
。
所以:
- 您需要定义一个
int
变量来获取fgetc()
的结果。 - 如果不是
EOF
,您可以将结果转换回unsigned char
。那是你的字符,同时也是 ASCII 值。
PS - 我忽略了非 ASCII 字符、非拉丁语言等(但 C 在其基本标准库函数中也大多忽略了它们。)
如果我们谈论的是纯 ASCII,值从 0 到 127,您的 table 应该如下所示:
int dictionary[128] = {0};
关于您的问题:
how to check its ASCII and store it
考虑 char
是一个很小的 int
,它们可以互换,您不需要任何转换。
fgetc
想要一个 int
来处理 EOF
,并且试图从包含少于 2000 字节的文件中读取 2000 个字符可能会产生非常糟糕的后果,读取整个文件:
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个词,使用动态记忆:
#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++] = '[=12=]';
}
}
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;
}