在我的课上,我必须阅读福尔摩斯的所有文本,并找到所有的对话.对话应该在两个引号内进行.
我试过的一切,我都不能始终如一地得到对话.在查看作业(job)中使用的文本和URL时,偶尔会缺少引号.这显然是由于语法,如果一个字符的讲话时间超过一个段落,那么每个新段落都会出现左引号,但右引号"标记只会出现在最后一段讲话的结尾.
由于这种情况的随机性,我发现这是不可能的,但我找到了一个可能的解决方案:如果我的引号状态变量等于1,并且出现第二个引号,我必须判断并查看是否有两个换行符,如果为真,则引号不计算并继续到结尾.
我一直在try 使用多个文件流指针来查找当前字符、前一个字符和前一个减go 一个字符,但我找不到这样做的方法.似乎不可能让多个指针指向同一个文件,但我不能100%确定.这真的是不可能的吗?
摘要:我遇到了一个运行时错误,我似乎永远找不到一致的对话.根据允许随机不放置结束引号的语法规则,似乎是不可能的.在我的脑海里,这让我觉得我充其量只能得到对话和叙述的混合.我try 创建多个文件流指针来查找当前、上一个和上一个字符减go 一个字符,但不起作用.
我的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 ;
}
我希望能够使用多个指针一次找到文件中的不同位置,并使用fSeek查找前一个和前一个减go 一个字符.它似乎不工作,我的日志(log)判断这些东西是否工作不打印到终端.