我正在尝试从文件中提取数字。我有限制,我只需要使用open()
,read()
和close()
.
我已成功读取数据并保存在缓冲区中。不,我需要将其与 RE 匹配。我正在使用 RE = ^[0-9]*
这是我的代码
char buffer[1024] = { NULL };
int count = 0;
int fRead;
fRead = open("file.in", O_RDONLY);
read(fRead, buffer, sizeof(buffer));
printf("\nFile Opened %s, %lu", buffer, sizeof(buffer));
/* Compile regular expression */
regex_t re;
int reti = regcomp(&re, "^[1-9]*", 0);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
/* Execute regular expression */
reti = regexec(&re, buffer, 0, NULL, 0);
if (!reti) {
printf("\nMatch: %d", reti);
} else if (reti == REG_NOMATCH) {
puts("No match");
} else {
char msgbuf[100];
regerror(reti, &re, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
close(fRead);
现在的问题是我想计算并显示我在文件中找到的数字。例如我的文件可能有 text some thing 2 to 3 makes 5
,在这种情况下我的输出必须是 OUTPUT:
2,3,5
count = 3