5

我有一个 C++ 程序,它需要返回出现特定单词的行。例如,如果我的文件如下所示:

the cow jumped over
the moon with the
green cheese in his mouth

我需要打印带有“with”的行。程序得到的只是文件开头的偏移量(在本例中为 24,因为“with”是文件开头的 24 个字符)。

如何打印整行“the moon with the”,仅使用偏移量?

非常感谢!

4

3 回答 3

3

您可以通过单独读取每一行并记录读取前后的文件位置来做到这一点。然后只是简单的检查一下单词的偏移量是否落在该行的范围内。

#include <iostream>
#include <fstream>
#include <string>

std::string LineFromOffset(
    const std::string &filename,
    std::istream::pos_type targetIndex)
{
    std::ifstream input(filename);

    //  Save the start position of the first line. Should be zero of course.
    std::istream::pos_type  lineStartIndex = input.tellg();

    while(false == input.eof())
    {
        std::string   line;

        std::getline(input, line);

        //  Get the end position of the line
        std::istream::pos_type  lineEndIndex = input.tellg();

        //  If the index of the word we're looking for in the bounds of the
        //  line, return it
        if(targetIndex >= lineStartIndex && targetIndex < lineEndIndex)
        {
            return line;
        }

        // The end of this line is the start of the next one. Set it
        lineStartIndex = lineEndIndex;
    }

    //  Need a better way to indicate failure
    return "";
}

void PrintLineTest()
{
    std::string str = LineFromOffset("test.txt", 24);

    std::cout << str;
}
于 2012-06-10T15:21:21.737 回答
2

一个好的解决方案是从头开始读取文件直到所需位置(@Chet Simpson 回答)。如果您想要优化(例如非常大的文件,位于中间某处,典型的行相当短),您可以向后读取文件。但是,这仅适用于以二进制模式打开的文件(类 unix 平台上的任何文件;ios_base::binary在 Windows 上使用参数打开文件)。

算法如下:

  • 返回文件中的几个字节
  • 读取几个字节
  • 如果那里有行尾,剩下的就很容易了
  • 否则,重复

代码(在 Windows 上测试):

std::string GetSurroundingLine(std::istream& f, std::istream::pos_type start_pos)
{
    std::istream::pos_type prev_pos = start_pos;
    std::istream::pos_type pos;
    char buffer[40]; // typical line length, so typical iteration count is 1
    std::istream::pos_type size = sizeof(buffer);

    // Look for the beginning of the line that includes the given position
    while (true)
    {
        // Move back 40 bytes from prev_pos
        if (prev_pos < size)
            pos = 0;
        else
            pos = prev_pos - size;
        f.seekg(pos);

        // Read 40 bytes
        f.read(buffer, prev_pos - pos);
        if (!f)
            throw;

        // Look for a newline byte, which terminates previous line
        int eol_pos;
        for (eol_pos = sizeof(buffer) - 1; eol_pos >= 0; --eol_pos)
            if (buffer[eol_pos] == '\n')
                break;

        // If found newline or got to beginning of file - done looking
        if (eol_pos >= 0 || pos == (std::istream::pos_type)0)
        {
            pos += eol_pos + 1;
            break;
        }
    }

    // Position the read pointer
    f.seekg(pos);

    // Read the line
    std::string s;
    std::getline(f, s, '\n');

    return s;
}

编辑:在类似 Windows 的平台上,行尾由 标记\r\n,因为您必须使用二进制模式,所以输出字符串将包含额外字符\r(除非文件末尾没有行尾),你可以扔掉。

于 2012-06-10T17:21:31.307 回答
1

每个操作都有功能

fopen- 打开文件

fseek- 将文件搜索到所需的偏移量

fread- 读取你想要的字节数

fclose- 关闭文件

于 2012-06-10T12:12:51.667 回答