You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C语言fseek与文件流指针提取文本对话并定位前序字符?

《福尔摩斯》文本对话提取的引号处理问题

我在课程作业中需要提取《福尔摩斯》文本中的所有对话,对话本应处于双引号内。但因语法规则,角色长段发言时仅每段开头有开引号、最后一段结尾有关引号,导致文本存在引号缺失,无法稳定提取对话。

我提出一种解决方案:当引号状态变量为1时,若遇到第二个引号,需检查前两个字符是否为换行符,若是则该引号不计入。我尝试使用多个文件流指针结合fseek来获取当前字符、前一个字符及前两个字符,但该方案无法实现,且运行时日志未输出,代码无法正常工作。请问这种多文件指针方案是否可行?

以下是我的C语言函数代码:

void findDialogueInFile(char* filename)
{
    FILE *newlyWrittenFile = fopen(filename, "r");

    if (newlyWrittenFile == NULL)
    {
        printf("\nFile could not be opened");
    }
    else
    {
        printf("\nNewlyWrittenFile is readable");
    }

    char charIterator;
    
    int doubleQuoteCounter = 0;

    FILE *quoteCheckerFile = fopen("quoteChecker.txt", "w");

    if (quoteCheckerFile == NULL)
    {
        printf("\nFile could not be opened");
    }
    else
    {
        printf("\nquoteChecker is writeable");
    }

   int singleQuoteCounter = 0;

   FILE *previousElementOfStreamPointer = fopen("quoteChecker.txt", "r");

   FILE *ElementMinus2OfStreamPointer = fopen("quoteChecker.txt", "r");

   char previousCharElement;

   char previousCharElementMinus2;

   int lengthOfStringArrayCounter = 0;

   if (previousElementOfStreamPointer == NULL)
    {
        printf("\nFile could not be opened");
    }
    else
    {
        printf("\nquoteChecker is readable with previousElementOfStreamPointer");
    }

   if (ElementMinus2OfStreamPointer == NULL)
    {
        printf("\nFile could not be opened");
    }
    else
    {
        printf("\nquoteChecker is readable with elementMinus2OfStreamPointer");
    }

    /// vvvvv where the magic happens vvvvv

    while( (charIterator = fgetc(newlyWrittenFile)) != EOF )
    {
        
        fseek(previousElementOfStreamPointer, -1L, SEEK_CUR);

        fseek(ElementMinus2OfStreamPointer, -2L, SEEK_CUR);


        previousCharElement = fgetc(previousElementOfStreamPointer);

        previousCharElementMinus2 = fgetc(ElementMinus2OfStreamPointer);

        if (charIterator == '"')
        {
            if(previousCharElement == '\n' && previousCharElementMinus2 == '\n')
            {
                printf("\nFOUND DIALOGUE LONGER THAN A PARAGRAPH\n");
                continue;
            }
            fprintf(quoteCheckerFile, "%c", charIterator);
            doubleQuoteCounter++;
        }
        else if (singleQuoteCounter >= 2)
        {
            fprintf(quoteCheckerFile, "\n");
            singleQuoteCounter = 0;
            //doubleQuoteCounter = 0;
            continue;
        }
        else if (doubleQuoteCounter == 1)
        {

            if (charIterator == '\'')
            {
                singleQuoteCounter++;
            }
            
            fprintf(quoteCheckerFile, "%c", charIterator);
        }
        else if (doubleQuoteCounter >= 2)
        {
            fprintf(quoteCheckerFile, "\n\n");
            doubleQuoteCounter = 0;
        }

   }

    fclose(newlyWrittenFile);
    fclose(quoteCheckerFile);

    return ;

}

内容的提问来源于stack exchange,提问作者Riviera Sperduto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 18:02:45