我写的代码在C操作的文件,它解析的文本片段从文件input.txt,这里是
(
"The quick brown fox jumps over the lazy dog" is
an English-language pangram – a sentence that
contains all the letters of the alphabet.
)
(
The phrase is commonly used for touch-typing
practice, testing typewriters and computer
keyboards, displaying examples of fonts,
)
(
The earliest known appearance of the phrase
was in The Boston Journal. In an article
titled "Current Notes" in the February 9.
)
字符串
main.c:
#include <stdio.h>
#include <stdlib.h>
#include "include/functions.h"
int
main (void)
{
process_file("input.txt");
return EXIT_SUCCESS;
}
型
功能c:
#include "include/functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
The example of input.txt:
(
"The quick brown fox jumps over the lazy dog"
is an English-language pangram – a sentence that
contains all the letters of the alphabet.
)
(
"The quick brown fox jumps over the lazy dog"
is an English-language pangram – a sentence that
contains all the letters of the alphabet.
)
*/
static void
parse_parentheses(FILE *fp, void (*callback)(char *))
{
char line[560];
char text[560];
int inside_parentheses = 0;
while (fgets(line, sizeof(line), fp))
{
if (inside_parentheses)
{
if (strstr(line, ")") != NULL)
{
inside_parentheses = 0;
callback(text);
memset(text, 0, sizeof(text));
}
else
{
strcat(text, line);
}
}
else
{
if (strstr(line, "(") != NULL)
{
inside_parentheses = 1;
}
}
}
}
void
print_text(char *text)
{
printf("Extracted value: %s\n", text);
}
void
process_file (char *filename)
{
FILE *fp = fopen (filename, "r");
if (fp == NULL)
{
perror ("Can't open the file.");
exit (EXIT_FAILURE);
}
parse_parentheses(fp, print_text);
fclose (fp);
}
型
它编译和打印文本成功,但我用这个命令来检查是否有任何泄漏
$ valgrind --track-origins=yes --leak-check=yes -s ./file-parser
型
它抱怨无条件跳转和未初始化的字节。
我已经尝试启动vgdb服务器来跟踪到底是什么导致了这些问题,
$ valgrind --track-origins=yes --vgdb=yes --vgdb-error=0 ./file-parser
$ gdb ./file-parser
(gdb) target remote | vgdb
型
但是我不知道该怎么做,你能建议你怎么做跟踪这些错误吗?
1条答案
按热度按时间xsuvu9jc1#
字符串
具有自动存储持续时间,并且在未初始化时不归零。
你可能想要:
型