How to read a file twice (e.g. like an old two-pass assembler do) using std::ifstream
?
I tried the obvious
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char**argv)
{
std::string path = argc>1?std::string{argv[1]}:std::string(__FILE__);
std::ifstream inp{path};
int num=0;
std::cout << "first pass" << std::endl;
do {
std::string lin;
std::getline(inp,lin);
if (inp.eof())
break;
num++;
std::cout << "#" << num << ":" << lin << std::endl;
} while (!inp.eof());
inp.seekg(0, inp.beg);
inp.sync();
std::cout << "second pass" << std::endl;
num=0;
do {
std::string lin;
std::getline(inp,lin);
if (inp.eof())
break;
num++;
std::cout << "##" << num << ":" << lin << std::endl;
} while (!inp.eof());
inp.close();
return 0;
}
and it does not work (the second loop is looping indefinitely).
FWIW, compiling with GCC 4.9.2 on Linux/x86-64/Debian
precisions
Actually I am trying to parse a file made of lines like ** somename
followed (in the next lines) by a JSON object, followed by some empty newlines (and repeatedly again perhaps a ** someothername
followed by another JSON object etc...). I need a two-pass algorithm. The first pass is extracting all the names (like somename
) and building some "empty" named things. The second pass is filling the named things from the JSON object following them. JSON parsing is done using a recent 1.5 jsoncpp
library.