2013-11-10 138 views
-1

我需要打开一个文件,然后计算某个序列在文件中出现的次数,并忽略空格。文件名和序列通过使用命令行输入。这是我的方法:我打开文件,然后将内容存储到数组,然后从该数组中删除所有空间并将其存储到另一个数组。然后,我搜索序列并计算它出现的次数。这是我的代码:文件输入/输出和搜索

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

void main (int argc, char *argv[]) 
{ 
char *tempRaw; 
char *temp; 
int size; 
//Input check 
if(argc != 3) 
{ 
fprintf(stderr, "Usage: %s Input Search\n", argv[0]); 
exit(1); 
} 
//Open files 
FILE *input = fopen(argv[1],"r"); 
//Check for file 
if(input == NULL) 
{ 
    fprintf(stderr, "Unable to open file: %s\n", argv[1]); 
    exit(1); 
} 
//Get the file size 
fseek (input,0,SEEK_END); 
size = ftell(input); 
rewind(input); 
//Allocate memory for the strings 
tempRaw = (char*) malloc(sizeof(char)*size); 
temp = (char*) malloc(sizeof(char)*size); 

//Copy the file's content to the string 
int result =0; 
int i; 
fread(tempRaw,sizeof(char),size,input); 
//Remove the blanks 
removeBlanks(temp,tempRaw); 
fclose(input); 

char *pointer; 
//Search for the sequence 
pointer = strchr(pointer,argv[2]); 
// If the sequence is not found 
if (pointer == NULL) 
{ 
    printf("%s appears 0 time",argv[2]); 
    return; 
} 
else if (pointer != NULL) 
{ 
    //Increment result if found 
    result ++; 
} 
while (pointer != NULL) 
{ 
    //Search the next character 
    pointer = strchr(pointer+1,argv[2]); 
    //Increment result if the sequence is found 
    if (pointer != NULL) 
    { 
     result ++; 
    } 
    //If the result is not found, pointer turn to NULL the the loop is break 
} 

printf(" Sequence : %s\n",temp); 
printf("%s appears %d time(s)\n",argv[2],result); 
} 

void removeBlanks(char *dest, const char *src) 
{ 
//Copy source to destination 
strcpy(dest,src); 
char *old = dest; 
char *new = old; 
//Remove all the space from destination 
while (*old != '\0') 
{ 
    // If it's not a space, transfer and increment new. 

    if (*old != ' ') 
    { 
     *new++ = *old; 
    } 
    // Increment old no matter what. 

    old++; 
} 

// Terminate the new string. 

*new = '\0'; 

} 

我测试了它,并且遇到了从文件中获取内容的问题。有时它是有效的,但大部分时间,我所得到的只是一个空字符串。

+0

这压痕不利于readabil ity ...也,[不要强制'malloc()']的返回值(http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858# 605858),'sizeof(char)'总是1,所以它是多余的。 – 2013-11-10 07:53:38

回答

1

有几个问题与您的代码和编译器应该给你的警告对他们(不要忽略编译器):

第一功能应被宣布,而不是定义,以便补充:

void removeBlanks(char *dest, const char *src); 

之前主要。根据C99标准(5.1.2.2.1程序启动main应声明一个返回值,如int main(int argc, char *argv[]),您应该添加适当的return陈述。

和上面指出的casting malloc is not needed一样。

上述问题不是为什么它不工作但...这是因为你使用strchr功能上的错误变量,以错误的方式:

pointer = strchr(pointer,argv[2]); 

应该

pointer = strchr(temp, *argv[2]); 

因为temp是指向您从文件中读取的内容的指针,而strchr需要char作为第二个参数,而不是char *。如果你要搜索的字符串,你将不得不使用strstr而这需要一个char *,如:

pointer = strstr(temp, argv[2]); 

而且,因为你从tempRaw删除空白,并存储新字符串中temp第二个字符串将缩短并会在最后得到的垃圾,所以你应该像初始化内存:

tempRaw = calloc(1, size); 

可能有其他错误太多,但这些变化使得它为我工作...

+0

'calloc'不是必需的,'removeBlanks()'为null - 终止输出数组。 –

+0

@EdwardClements嗯,它应该,但我在'printf(“序列:\ n%s \ n \ n”,temp)结束时得到垃圾;'如果我不使用calloc。 – jpw

+0

尽管如此,仍然没有在某些情况下工作。例如:如果我在文件中搜索GT包含“GAGAGAGAGAGAAAAAAGGGGGTTAATATATTTTGATAC”。我得到了12而不是1.尝试通过我的代码,看看有什么不对。任何建议? –