2013-04-16 67 views
0

我正在尝试使用fgetc进行字词搜索。我了解fgetc的功能,但我陷入了seg故障。在运行gdb测试时,我返回以下内容。有没有一种更简单的方法来实现搜索功能?我是编程新手。 感谢您的帮助。使用fgetc搜索字词

#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; 
} 
+0

也许用调试信息('-g')编译并用调试程序遍历代码? – danfuzz

+0

我得到以下 (gdb) where #0 0x00007ffff7aa4c64 in getc() from /lib64/libc.so.6 #1 0x000000000040070c in main (argc=2, argv=0x7fffffffe4f8) at sum.c:67 Mani

回答

2

你几乎肯定无法打开输入文件。如果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); 
} 
+0

只是一个查询,当我执行程序时,我做了cat input.txt,它显示了输入文件,它是否仍然表示输入文件中有错误? – Mani

1

你只读一个字符c,然后循环while(c != EOF)几乎总是一个无限循环。在那个循环中,你增加messageCounter,你用它来走过数组的末端 - 繁荣!

+0

我该如何解决它?我明白你在说什么,但是如果你能提示我如何解决它。 – Mani

0

根据您的评论,argc是2,但你指的argv[2]这将是args来第三元素,并将于NULLFILE *也将最终变为NULL(因为将NULL传递给fopen无效)。

-1

这将是,如果你使用此功能strcmp很容易...

你所要做的就是先找到使用ftell乌尔文件的长度,之后分配这么大的内存,然后使用填补记忆fgetcfgets或任何其他文件功能...然后只需使用strcmp函数....宾果!!!!! :)