如何使用 POSIX 函数计算 C 语言文件中的多个字符?
How to count multiple characters from a file in C language using POSIX functions?
我正在尝试编写一个程序,使用标准 POSIX 函数获取一个文件和一个字符串,程序计算字符串包含的文件中的所有字符。
例如,如果用户写:
count.exe x.txt abcd
程序计算文件中a,b,c,d每个字符的个数x.txt
示例消息:
Number of 'a' characters in 'x.txt' file is: 4
Number of 'b' characters in 'x.txt' file is: 9
Number of 'c' characters in 'x.txt' file is: 7
Number of 'd' characters in 'x.txt' file is: 0
目前得到的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define BUFSIZE 1024
void exit_sys(const char* msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
void exit_fail(const char* msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int get_count(char* p, size_t size, char c)
{
int count = 0;
size_t i;
for (i = 0; i < size; ++i)
if (p[i] == c)
++count;
return count;
}
void run_count_characters_application(int argc, char** argv)
{
int fd;
char c;
char buf[BUFSIZE];
int n;
int count;
if (argc != 3)
exit_fail("usage: ./mycounter file character");
if (strlen(argv[2]) < 0)
exit_fail("You have to give at least one character");
c = argv[2][0];
if ((fd = open(argv[1], O_RDONLY)) < 0)
exit_sys("open");
count = 0;
while ((n = read(fd, buf, BUFSIZE)) > 0)
count += get_count(buf, n, c);
if (n < 0)
exit_sys("read");
printf("Count:%d\n", count);
close(fd);
}
int main(int argc, char** argv)
{
run_count_characters_application(argc, argv);
return 0;
}
到目前为止我在这段代码中得到的问题是它只计算一个字符(只计算第一个字符),我想知道如何让它读取和计算我在命令中写的其他字符, 提前谢谢你:)
由于您在评论中要求示例:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void die(const char *reason)
{
fprintf(stderr, "%s\n", reason);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
if (argc != 3)
die("usage: ./count file characters");
FILE *f = fopen(argv[1], "r");
if (f == NULL)
die("unable to open file");
unsigned int counter[UCHAR_MAX + 1] = { 0 };
int c;
while ((c = fgetc(f)) != EOF)
counter[c]++;
fclose(f);
size_t len = strlen(argv[2]);
for (unsigned int i = 0; i < len; i++) {
char c = argv[2][i];
unsigned int count = counter[c];
printf("Number of '%c' characters in '%s' file is: %u\n", c, argv[1], count);
}
}
$ cc count.c -o count
$ echo "bbaaafff" > test.txt
$ ./count test.txt afm
Number of 'a' characters in 'test.txt' file is: 3
Number of 'f' characters in 'test.txt' file is: 3
Number of 'm' characters in 'test.txt' file is: 0
$
函数fgetc()
return the character read as an unsigned char cast to an int or EOF on end of file or error.
unsigned char
可以存储 UCHAR_MAX + 1
个可能的值(通常为 0 到 255)所以我制作了一个数组,可以通过这些值(counter
)进行索引来存储我们的很重要。将其视为来自其他语言的 "map",将字符映射到它们的计数。
然后在最后,我遍历输入字符串中的字符并打印它们的计数。
如评论中所述,这仅适用于 ASCII 个字符。
我正在尝试编写一个程序,使用标准 POSIX 函数获取一个文件和一个字符串,程序计算字符串包含的文件中的所有字符。
例如,如果用户写:
count.exe x.txt abcd
程序计算文件中a,b,c,d每个字符的个数x.txt
示例消息:
Number of 'a' characters in 'x.txt' file is: 4
Number of 'b' characters in 'x.txt' file is: 9
Number of 'c' characters in 'x.txt' file is: 7
Number of 'd' characters in 'x.txt' file is: 0
目前得到的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define BUFSIZE 1024
void exit_sys(const char* msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
void exit_fail(const char* msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int get_count(char* p, size_t size, char c)
{
int count = 0;
size_t i;
for (i = 0; i < size; ++i)
if (p[i] == c)
++count;
return count;
}
void run_count_characters_application(int argc, char** argv)
{
int fd;
char c;
char buf[BUFSIZE];
int n;
int count;
if (argc != 3)
exit_fail("usage: ./mycounter file character");
if (strlen(argv[2]) < 0)
exit_fail("You have to give at least one character");
c = argv[2][0];
if ((fd = open(argv[1], O_RDONLY)) < 0)
exit_sys("open");
count = 0;
while ((n = read(fd, buf, BUFSIZE)) > 0)
count += get_count(buf, n, c);
if (n < 0)
exit_sys("read");
printf("Count:%d\n", count);
close(fd);
}
int main(int argc, char** argv)
{
run_count_characters_application(argc, argv);
return 0;
}
到目前为止我在这段代码中得到的问题是它只计算一个字符(只计算第一个字符),我想知道如何让它读取和计算我在命令中写的其他字符, 提前谢谢你:)
由于您在评论中要求示例:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void die(const char *reason)
{
fprintf(stderr, "%s\n", reason);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
if (argc != 3)
die("usage: ./count file characters");
FILE *f = fopen(argv[1], "r");
if (f == NULL)
die("unable to open file");
unsigned int counter[UCHAR_MAX + 1] = { 0 };
int c;
while ((c = fgetc(f)) != EOF)
counter[c]++;
fclose(f);
size_t len = strlen(argv[2]);
for (unsigned int i = 0; i < len; i++) {
char c = argv[2][i];
unsigned int count = counter[c];
printf("Number of '%c' characters in '%s' file is: %u\n", c, argv[1], count);
}
}
$ cc count.c -o count
$ echo "bbaaafff" > test.txt
$ ./count test.txt afm
Number of 'a' characters in 'test.txt' file is: 3
Number of 'f' characters in 'test.txt' file is: 3
Number of 'm' characters in 'test.txt' file is: 0
$
函数fgetc()
return the character read as an unsigned char cast to an int or EOF on end of file or error.
unsigned char
可以存储 UCHAR_MAX + 1
个可能的值(通常为 0 到 255)所以我制作了一个数组,可以通过这些值(counter
)进行索引来存储我们的很重要。将其视为来自其他语言的 "map",将字符映射到它们的计数。
然后在最后,我遍历输入字符串中的字符并打印它们的计数。
如评论中所述,这仅适用于 ASCII 个字符。