C ++读取文件直到找到char

时间:2014-12-23 16:22:10

标签: c++ file

我想创建一个函数,可以通过char连续读取文件char,直到遇到某个特定的char。

这是我在FileHandler类中的方法。

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("%s\n", text);

    return text;
}

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

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

但是,我确信代码中某处存在不正确的东西,因为如果我在循环块中使用这些方法,应用程序就会挂起。我猜'不对'的事情是关于指针,但我不确切知道在哪里。你能指点一下吗?

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

        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 - '\0'
   char* text = new char[2];

   // Make it a null terminated string.
   text[1] = '\0';

   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] = '\0';
            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 - '\0'
   char* text = new char[2];
   text[1] = '\0';

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

答案 2 :(得分:0)

        this->inputFileStream->read(buffer, 1);

没有错误检查。

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

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