Created
February 5, 2020 09:51
-
-
Save KaiserKatze/f17698769b7355cf2cbde156f8536d13 to your computer and use it in GitHub Desktop.
Read a file interactively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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