Skip to content

Instantly share code, notes, and snippets.

@KaiserKatze
Created February 5, 2020 09:51
Show Gist options
  • Save KaiserKatze/f17698769b7355cf2cbde156f8536d13 to your computer and use it in GitHub Desktop.
Save KaiserKatze/f17698769b7355cf2cbde156f8536d13 to your computer and use it in GitHub Desktop.
Read a file interactively.
#include <fstream>
#include <iostream>
int interactive(const std::string& path)
{
int count{ 0 };
char buffer[1024]{ 0 };
std::ifstream ifs{ path };
if (!ifs)
return -1;
while (!ifs.eof())
{
std::cout << "> ";
std::cin >> count;
if (count == 0) break;
if (count < 0)
{
auto pos{ ifs.tellg() };
pos += count;
if (pos < 0) pos = 0;
ifs.seekg(pos);
continue;
}
ifs.read(buffer, count);
long long* number{ reinterpret_cast<long long*>(buffer) };
std::cout
<< buffer // string
<< std::endl
<< *number
<< std::endl;
std::memset(buffer, 0, count);
}
ifs.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment