c++ - 设置获取指针

标签 c++

我正在处理这个类(class)项目,但我不确定我是否完全理解这个问题。说明如下:

When you are reading from the stream, each time you read a character the pointer is incremented by one. That way you always get the next character. What if you want to read the same file twice? Well you need to reset the 'get' point back to 0. In this example, pretend that the file numbers.txt exists and that it has inside it the number 1, 2, 3, 4, and 5 in order with a space in-between. You notice that the count resets back to 1 when the get pointer is put back at the 0 location of the file.

我的问题是他究竟要求什么?

此外,我对如何处理 int location 感到困惑。不要求解决,至少只是在正确方向上的提示。

#include <iostream>
#include <ostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>

bool GetInputFileStream(std::ifstream * fin, std::string filename);
void SetGetPointer(std::istream & fin, int location);

int main()
{
std::ifstream fin;
    std::string filename = "numbers.txt";
    bool isOpen = GetInputFileStream(&fin, filename);
    std::cout << filename << " open: ";
    std::cout << std::boolalpha << isOpen << std::endl;
    int number = 0;
    fin >> number;
    std::cout << "Read: " << number << std::endl;
    fin >> number;
    std::cout << "Read: " << number << std::endl;
    SetGetPointer(fin, 0);
    while (fin >> number)
    {
        std::cout << "Read: " << number << std::endl;
    }
    fin.close();

  std::cout << "Press ENTER to continue";
  std::cin.get();

}//END MAIN

bool GetInputFileStream(std::ifstream * fin, std::string filename)
{

    std::ifstream Infield(filename);
    return Infield.good();

}

void SetGetPointer(std::istream & fin, int location)
{

     &fin.seekg(std::ios::beg);

    //What am I missing with location??

}

最佳答案

教授给的描述是SetGetPointer()中需要实现的内容

根据描述:您需要将“获取”点重置回 0。 你可以看到这个函数是如何被使用的: SetGetPointer(fin, 0);

因此您可以说 SetGetPointer() 在某个位置 设置fin。在前面的示例中,它将 fin 设置回开头。

关于c++ - 设置获取指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704701/

相关文章:

c++ - 如何引入标准库的意外构建依赖项

c++ - 线性插值更线性

c++ - 对(函数)c++ 的 undefined reference

c++ - AVX2:计算 512 个 float 组的点积

c++ - 在C++程序中运行linux命令

C++ 映射 vector 引用错误

c++ - 在定义模板后声明函数

c++ - Windows Linux C++编译问题: "no matching function for call"

c++ - 如有必要,从字符串中删除尾随的 0 和小数

c++ - HttpAddUrl 权限