无法理解如何使用 seekg tellg 来实现以下结果

Cant understand how to use seekg tellg to achieve the following result

作为大型程序的一部分,我的任务是读取输入文件的每一行并将索引偏移量存储到每一行。稍后给出所有索引偏移量,我希望能够直接转到文件中的那个位置并打印与该偏移量对应的行。谁能帮我找出我在以下代码中做错了什么。

    #include <iostream>
    #include <sstream> 
    #include <vector>
    #include <iterator>
    #include <stdio.h>
    #include <string.h>    

    void create_index_info(ifstream& myFile, std::unordered_map<size_t, std::pair<std::streampos, size_t>>& map_index_start_end_pos)
    {
        size_t uiLength;
        size_t uiCount = 0;
        std::string line;

        while (getline(myFile, line))
        {        
            start_pos = myFile.tellg();
            uiLength = strlen(line.c_str());

            map_index_start_end_pos.emplace( uiCount, std::make_pair(start_pos, uiLength) );
            uiCount++;                        
        }
    }

    void print_index_info(ifstream& myFile, const std::unordered_map<size_t, std::pair<std::streampos, size_t>>& map_index_start_end_pos)
    {
        size_t uiLength;
        for(auto it = map_index_start_end_pos.begin(); it != map_index_start_end_pos.end(); it++)
        {
            auto res = it->second;

            myFile.clear();
            myFile.seekg(res.first, ios::beg);
            uiLength = res.second;

            char* buffer = (char*)malloc(uiLength * sizeof(char));
            myFile.read(buffer, uiLength);

            for(size_t uiCount = 0; uiCount < uiLength; uiCount++)
            {
                std::cout<< buffer[uiCount];
            }
            std::cout<<"\n";

            free(buffer);
            buffer = NULL;    
        }
    }

    int main()
    {
        std::unordered_map<size_t, std::pair<std::streampos, size_t>> map_index_start_end_pos;
        ifstream myFile("Filename.txt");
        create_index_info(myFile, map_index_start_end_pos);
        myFile.close();

        ifstream myFile1("Filename.txt");
        print_index_info(myFile1, map_index_start_end_pos);
        myFile1.close();
        return 0;
    }

输入文本文件中的数据包含以下条目:

9 10 11

8 7 5

67 34 12 45 9

20

代码的理想输出应该与输入模数相同,直到打印行的排序(我使用的是无序映射,所以输出不需要按照输入的顺序打印)。但是我的代码输出了一些垃圾,主要是 \x00 形式的字符。谁能帮我弄清楚我的代码有什么问题。

  • 您的程序无法编译。
  • 为什么要使用地图?为什么 vector 不够?
  • 为什么是 getline 而不是 ignoreignore 速度更快且不分配内存。

您可能正在寻找这个:

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

std::istream& get_line_idx( std::istream& is, std::vector<std::streampos>& result )
{
  for( std::streampos pos = is.tellg(); is.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); pos = is.tellg() )
    result.push_back( pos );
  return is;
}

void print( std::istream& is, std::vector<std::streampos>& pos )
{
  for( auto curpos : pos )
  {
    std::string line;
    is.seekg( curpos );
    std::getline( is, line );
    std::cout << line << std::endl;
  }
}

int main()
{
  std::vector<std::streampos> result;
  {
    std::ifstream is { "c:\temp\test.txt" };
    if( !is )
      return -1;
    get_line_idx( is, result );
  }

  {
    std::ifstream is { "c:\temp\test.txt" };
    if( !is )
      return -2;
    print( is, result );
  }

  return 0;
}