1

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

我认为我的问题在于这条线:while ((options = getopt (argc, argv, "You entered:")) != -1)line.

#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;
}
4

1 回答 1

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()陈述

于 2013-12-12T06:20:49.897 回答