C++读取文件,直到找到字符
C++ read file until char found
我想创建一个函数,该函数可以连续读取文件字符,直到遇到某些特定的字符。
这是我在类文件处理程序中的方法。
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
是未定义行为的原因。 strcmp
和strcat
需要以空结尾的字符串。
这是一个更新的版本,带有适当的注释。
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
函数用于比较字符串。在这里,您只想比较两个字符。
相关文章:
- 尝试一次将单个字符读取到大小不确定的数组中
- C++将 txt 数据和字符读取到 2D 数组中
- C++ UTF-8 瑞典语字符读取为 ASCII
- C++逐个字符读取二进制文件字符
- 逐个字符读取文件并使用<cctype>
- C 如何通过特定n字符读取TXT文件的值
- 如何使用4个字符读取C 的文本文件
- 使用C++中的字符读取具有文件 I/O 的文件
- 是否可以从C 中的文件中的线中的特定字符读取
- 将字符读取到数组
- 如何在 c++ 中逐个字符读取文件并通过 array_name[index] 访问它
- 如何从文本文件中读取,C++中逐个字符读取
- 如何将多个字符读取合并为std::向量
- 在 c++ 中逐个从文件字符读取
- C++ 中有多少个方法逐个字符读取字符串,以对每个字符执行所需的操作
- scanf 只能将 3 个字符读取到 5 位字符数组中
- 将行的前 X 个字符读取为字符串
- 逐个字符读取以查看它是否是数字
- 如何从字符**读取字符串
- 如何逐个字符读取文件的全部内容