C++读取文件,直到找到字符

C++ read file until char found

本文关键字:字符 读取 文件 C++      更新时间:2023-10-16

我想创建一个函数,该函数可以连续读取文件字符,直到遇到某些特定的字符。

这是我在类文件处理程序中的方法。

char* tysort::FileHandler::readUntilCharFound(size_t start, char seek)
{
    char* text = new char;
    if(this->inputFileStream != nullptr)
    {
        bool goOn = true;
        size_t seekPos = start;
        while (goOn)
        {
            this->inputFileStream->seekg(seekPos);
            char* buffer = new char;
            this->inputFileStream->read(buffer, 1);
            if(strcmp(buffer, &seek) != 0)
            {
                strcat(text, buffer); // Execution stops here
                seekPos++;
            }
            else
            {
                goOn = false;
            }
        }
    }
    //printf("%sn", text);
    return text;
}

我测试了这个函数,它确实有效。这是一个读取文件内容的示例,直到找到换行符'n'

size_t startPosition = 0;
char* text = this->fileHandler->readUntilCharFound(startPosition, 'n');

但是,我确信代码中的某处存在不正确的内容,因为如果我在循环块中使用这些方法,应用程序只会挂起。我想"不对"的事情是关于指针的,但我不知道确切的位置。你能为我指出吗?

C++提供了一些易于使用的解决方案。例如:

istream& getline (istream& is, string& str, char delim);

在您的情况下,该参数将等效于您的text变量,而 delim 将等效于您的seek参数。此外,getline 的返回值在某种程度上等同于您的 goOn 标志(关于使用 getline 的返回值检查 EOF 和 IO 错误的正确模式,有很好的常见问题解答)

        if(strcmp(buffer, &seek) != 0)

            strcat(text, buffer); // Execution stops here

是未定义行为的原因。 strcmpstrcat需要以空结尾的字符串。

这是一个更新的版本,带有适当的注释。

char* tysort::FileHandler::readUntilCharFound(size_t start, char seek)
{
   // If you want to return a string containing 
   // one character, you have to allocate at least two characters.
   // The first one contains the character you want to return.
   // The second one contains the null character - ''
   char* text = new char[2];
   // Make it a null terminated string.
   text[1] = '';
   if(this->inputFileStream != nullptr)
   {
      bool goOn = true;
      size_t seekPos = start;
      while (goOn)
      {
         this->inputFileStream->seekg(seekPos);
         // No need to allocate memory form the heap.
         char buffer[2];
         this->inputFileStream->read(buffer, 1);
         if( buffer[0] == seek )
         {
            buffer[1] = '';
            strcat(text, buffer);
            seekPos++;
         }
         else
         {
            goOn = false;
         }
      }
   }
   return text;
}

您可以进一步简化该功能,以便:

char* tysort::FileHandler::readUntilCharFound(size_t start, char seek)
{
   // If you want to return a string containing 
   // one character, you have to allocate at least two characters.
   // The first one contains the character you want to return.
   // The second one contains the null character - ''
   char* text = new char[2];
   text[1] = '';
   if(this->inputFileStream != nullptr)
   {
      this->inputFileStream->seekg(start);
      // Keep reading from the stream until we find the character
      // we are looking for or EOF is reached.
      int c;
      while ( (c = this->inputFileStream->get()) != EOF && c != seek )
      {
      }
      if ( c != EOF )
      {
         text[0] = c;
      }
   }
   return text;
}
        this->inputFileStream->read(buffer, 1);

没有错误检查。

        if(strcmp(buffer, &seek) != 0)

strcmp函数用于比较字符串。在这里,您只想比较两个字符。