c - 打开文本文件并在 C 中搜索字符串后无输出

标签 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;
}

最佳答案

您忘记打开文件来检查大小,因此 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() 语句上移

关于c - 打开文本文件并在 C 中搜索字符串后无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951952/

相关文章:

c - 我希望循环复制空字符或其他内容,但它再次从头开始复制 char。这是为什么?这个循环是如何工作的?

java - 文件读取时执行速度差异较大

java - 如何在1个案例陈述中使用两个要求

c++ - 可以支持动画 GIF 的快速图形库

linux - 替换文件 Unix 中的字符串

C++ 字符串赋值给 at(int) 函数

file - V在Coq文件扩展名中代表什么?

java - 试图制作一个模仿进程表的程序

循环链表

c - 在 clang 中启用优化时是否定义了内置宏?