gpt4 book ai didi

c - 使用 fgetc 进行词搜索

转载 作者:太空宇宙 更新时间:2023-11-04 08:57:53 25 4
gpt4 key购买 nike

我正在尝试使用 fgetc 进行单词搜索。我了解 fgetc 的作用,但我遇到了段错误。在运行 gdb 测试时,我返回以下内容。有没有更简单的方法来实现搜索功能??我是编程新手。感谢您的帮助。

#0  0x00007ffff7aa4c64 in getc () from /lib64/libc.so.6
#1 0x000000000040070c in main ()

我哪里错了?

#include <stdio.h>
#include <stdlib.h>
int isAlpha(char c)
{
if( c >= 'A' && c <='Z' || c >= 'a' && c <='z' || c >= '0' && c <= '9' )
{
return 1;
}
else
{
return 0;
}
}

int CheckFunctionn(int length, int message_counter, char ref_word[], char newmessage[])
{
int newCounter = 0;
int counterSuccess = 0;

while(newCounter < length)
{
if(ref_word[newCounter] == newmessage[newCounter + message_counter])
{
counterSuccess++;
}
newCounter++;
}

if(counterSuccess == length)
{
return 1;
}
else
{
return 0;
}
}

int main(int argc, char *argv[])
{
char message[300];
int counter = 0;
int ref_length = 0;
int alphaCounter = 0;
int alphaCounterTime = 0;
int messageCounter = 0;
int word_counter = 0;

FILE* input;
FILE* output;

//long fileLength;
//int bufferLength;
//char readFile;
//int forkValue;

input = fopen(argv[2],"r");
output = fopen(argv[3],"w");

int c;
c = fgetc(input);

while(c != EOF)
{
while((argv[1])[ref_length] !='\0')
{
// if string is "HEY", (argv[1]) is HEY, ref_counter is the length
// which in this case will be 3.
ref_length++; //<-- takes care of the length.
}

while(alphaCounter < ref_length)
{
// this will add to alphaCounter everyetime alphaCT is success.
alphaCounterTime += isAlpha((argv[1])[alphaCounter]);
alphaCounter++;
}

if(alphaCounterTime != ref_length)
{
return 0;
}

if((messageCounter == 0 ) && (message[messageCounter + ref_length] == ' ' || message[messageCounter] == '\n' || message[messageCounter]== '\t')) // counts the whole things and brings me to space
{
// compare the message with the word
word_counter += CheckFunctionn(ref_length, messageCounter, argv[1], message);
}

if((message[messageCounter] == ' ' || message[messageCounter] == '\n' || message[messageCounter]== '\t') && (message[messageCounter + ref_length + 1] == ' ' || message[messageCounter + ref_length + 1] == '\n' || message[messageCounter + ref_length + 1]== '\t'))
{
word_counter += CheckFunctionn(ref_length, messageCounter + 1, argv[1], message);
}

if((message[messageCounter]== ' '|| message[messageCounter] == '\n' || message[messageCounter]== '\t') && (messageCounter + ref_length+1)== counter) //<-- this means the length of the message is same
{
word_counter += CheckFunctionn(ref_length, messageCounter + 1, argv[1], message);
}

messageCounter++;
}
fclose(input);
fclose(output);
return 0;
}

最佳答案

您几乎肯定无法打开输入文件。如果 fopen 失败,它返回 NULL,并且调用 fgetc(NULL) 有未定义的行为,段错误是未定义行为的一种可能结果.

您需要检查错误并进行相应处理。您还需要检查是否为您的程序提供了足够的参数。这是处理它们的一种方法:

if (argc < 3)
{
fprintf(stderr, "Usage: %s input-file output-file\n", argv[0]);
exit(1);
}

input = fopen(argv[1],"r");
if (input == NULL)
{
fprintf(stderr, "Error opening input file %s: %s\n", argv[1], strerror(errno));
exit(1);
}

output = fopen(argv[2],"w");
if (output == NULL)
{
fprintf(stderr, "Error opening output file %s: %s\n", argv[2], strerror(errno));
exit(1);
}

关于c - 使用 fgetc 进行词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com