打开文本文件并在C中搜索字符串后没有输出

时间:2013-09-23 04:51:25

标签: c string file command-line-arguments

我正在开发一个程序,用户输入程序名称,文件名,使用“-w”或“-i”或两者后跟多个单词的选项(3 in我的情况)在他们指定的文件中。我的代码应该显示找到单词的次数,但我不知道。

我认为我的问题在于这一行:while ((options = getopt (argc, argv, "You entered:")) != -1)行。

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

//main function    
int main (int argc, char * argv[])
{
    //initialize variable string so user can type in a string with up to 256 characters
    char string[256], temp[512];
    int options, index, lineNumber=1, findResult=0;
    FILE *file;

    //Waiting for user to enter their string
    scanf(string);

    //retrieves user-entered string
    gets(string);

    // Obtain file size
    fseek (file, 0, SEEK_END);
    long size = ftell (file);
    rewind (file);

    // Allocate memory for the whole file
    char* buffer = (char*) malloc (sizeof(char) * size);
    if (buffer == NULL)
        return 2;

    //open file
    file = fopen("test.txt", "rb");

    // Copy the file into the buffer:
    size_t result = fread (buffer, 1, size, file);
    if (result != size)
        return 3;

    // File is now contained within buffer


    //method for other stuff
    while ((options = getopt (argc, argv, "You entered:")) != -1)
        switch (options)
        {
            case 'w':
                //search for whole matched words only
                while(fgets(temp, 512, file) != NULL) 
                {
                    if((strstr(temp, string)) != NULL) 
                    {
                        printf("A match found on line: %d\n", lineNumber);
                        printf("\n%s\n", temp);
                    }
                    lineNumber++;
                    if(findResult == 0) 
                    {
                    printf("\nSorry, couldn't find a match.\n");
                    }
                }
                break;
            case 'i':
                // ignore case when looking for words
                while(fgets(temp, 512, file) != NULL) 
                {
                    if((strcasecmp(temp, string)) == 0) 
                    {
                        printf("A match found on line: %d\n", lineNumber);
                        printf("\n%s\n", temp);
                    }
                    lineNumber++;
                    if(findResult == 0) 
                    {
                        printf("\nSorry, couldn't find a match.\n");
                    }
                }
                break;

            //case '?':
            default:
                if (optopt == '?')
                    fprintf (stderr, "Option -%c requires an argument.\n", optopt);
                else if (isprint (optopt))
                    fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                else
                    printf("USAGE: wordsearch [-?] [-w] [-i] <text file> <word 1> <word 2> ... <word n>");
                    printf("-w: Search for whole matched words only. Any non-alpha, non-numerical characters can be considered breaking points for a word.");
                    printf("Ignore case when looking for words.");
                    fprintf (stderr,
                    "Unknown option character `\\x%x'.\n",
                    optopt);
                    return (-1);
                abort ();
        }

    //close file
    fclose(file);

    //free buffer
    free(buffer);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您忘记打开文件以检查尺寸,因此file未初始化。

FILE *file;

//Waiting for user to enter their string
scanf(string);

//retrieves user-entered string
gets(string);

// Obtain file size
fseek (file, 0, SEEK_END);
long size = ftell (file);
rewind (file);

只需提升您的fopen()声明

即可