如何使用POSIX函数计算C语言文件中的多个字符?
我正试图编写一个程序,通过使用标准POSIX函数获取一个文件和一个字符串,该程序计算该字符串包含的文件中的所有字符 例如,如果用户写入:如何使用POSIX函数计算C语言文件中的多个字符?,c,posix,counting,C,Posix,Counting,我正试图编写一个程序,通过使用标准POSIX函数获取一个文件和一个字符串,该程序计算该字符串包含的文件中的所有字符 例如,如果用户写入: count.exe x.txt abcd 程序计算每个字符的数量:x.txt文件中的a、b、c、d 示例消息: 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
count.exe x.txt abcd
程序计算每个字符的数量:x.txt文件中的a、b、c、d
示例消息:
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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义bufsize1024
无效退出系统(常量字符*消息)
{
佩罗尔(味精);
退出(退出失败);
}
无效退出失败(const char*msg)
{
fprintf(stderr,“%s\n”,msg);
退出(退出失败);
}
int get_count(字符*p,大小\u t大小,字符c)
{
整数计数=0;
尺寸i;
对于(i=0;i0)
计数+=获取计数(buf,n,c);
if(n<0)
退出系统(“读取”);
printf(“计数:%d\n”,计数);
关闭(fd);
}
int main(int argc,字符**argv)
{
运行字符计数应用程序(argc、argv);
返回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);
}
}
函数fgetc()
在文件或错误结束时,将读取为无符号字符转换的字符返回int或EOF
有UCHAR\u MAX+1
无符号字符可以存储的可能值(通常为0到255),因此我制作了一个数组,可以用这些值(计数器
)索引以存储我们的计数。可以将其视为其他语言的“映射”,将字符映射到其计数
最后,我循环输入字符串中的字符并打印它们的计数
如评论中所述,这仅适用于字符。因为您在评论中要求提供示例:
#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);
}
}
函数fgetc()
在文件或错误结束时,将读取为无符号字符转换的字符返回int或EOF
有UCHAR\u MAX+1
无符号字符可以存储的可能值(通常为0到255),因此我制作了一个数组,可以用这些值(计数器
)索引以存储我们的计数。可以将其视为其他语言的“映射”,将字符映射到其计数
最后,我循环输入字符串中的字符并打印它们的计数
正如评论中已经提到的,这仅适用于字符。您正在执行的
c=argv[2][0]
只传递c
的ASCII值,而不传递其余字符。您必须保留多个计数器,参数字符串中每个字符对应一个计数器。您可以将它们存储在无符号整数计数器[UCHAR_MAX+1]
中,并仅更新和打印您感兴趣的计数器(例如计数器['a']+
)。不过,这只适用于ascii字符…@Inian我该如何修复它?@MarcoLucidi你能举例说明吗,因为我对C语言非常熟悉,但我不完全了解如何操作。你正在做C=argv[2][0]
只传递c
的ASCII值,而不传递其余字符。您必须保留多个计数器,参数字符串中每个字符对应一个计数器。您可以将它们存储在无符号整数计数器[UCHAR_MAX+1]
中,并仅更新和打印您感兴趣的计数器(例如计数器['a']+
)。不过,这只适用于ascii字符…@Inian我该如何修复它?@MarcoLucidi你能举例说明吗,因为我对C语言非常熟悉,但我不完全了解如何使用open();而不是fopen();对于fclose();我可以用close()吗;?,因为我试图了解这些和其他像fread()这样的人有什么区别;>read();或fwrite();>write();,fgetc();>getc()@ArjanitKotorri长话短说:open()
,read()
,close()
。。。是unix系统调用,而fopen()
,fread()
,fclose()
。。。在这些系统调用上是c库包装器,它们为您提供缓冲I/O并允许您编写更多可移植代码。看:我们能说open():,read();,close();。。。是POSIX函数和fopen();,fread();,fclose();如果不是FILE*f=fopen(argv[1],“r”),标准C函数是否也可以工作代码>我没有文件*f=open(argv[1],仅限ordu)
?@ArjanitKotorrifopen()
和friends也在POSIX中定义,因此它们也是“POSIX函数”(请参阅:)。最后一条语句不起作用:open()
返回一个文件描述符(anint
),而fopen()
返回一个。如何使用open();而不是fopen();对于fclose();我可以用close()吗;?,因为我试图了解这些和其他像fread()这样的人有什么区别;>read();或fwrite();>write();,fgetc();>getc()@ArjanitKotorri长话短说:open()
,read()
,close()
。。。是unix系统调用,而fopen()
,fread()
,fclose()
。。。在这些系统调用上是c库包装器,它们为您提供缓冲I/O并允许您编写更多可移植代码。看:我们能说open():,read();,