How did I read a given number of lines from the end of a file using streams in C ++?

For my implementation of the tail shell command on Linux, I need to read a certain number of lines / bytes from the end of the file using an input / output stream. Does anyone have any suggestions on how to do this? I suspect that I need to open the file and pass some parameter to the ifstream constructor, but I don’t know what exactly. Googling did not find anything.

+3
source share
5 answers

Since the tail has to work with pipes that you cannot rewind, you will have to store a rotating buffer of the last n lines that you read, which you will dump on EOF.

+4
source

n . n, .

+3
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
  ifstream is("file.txt", ios::binary);
  if (!is) {
    cout << "Failed to open file" << endl;
    return 1;
  }

  is.seekg(0, ios::end);
  int len = is.tellg();
  char c;
  int n = 0;
  ostringstream line;
  int lines = 0;

  for (int i = len - 1; i >= 0; --i) {
    is.seekg(i, ios::beg);
    is.get(c);
    if (c == '\n' || i == 0) {
      if (i < len - 1) {
        if (i == 0) {
          line << c;
        }
        string s = line.str();
        cout << lines << ": " << string(s.rend() - n, s.rend()) << endl;
        ++lines;
        n = 0;
        line.seekp(0, ios::beg);
      }
    } else {
      line << c;
      ++n;
    }
  }

  is.close();

  return 0;
}
+2

, , , , , ( , , , ) "" , , (, , , , , ), .

HTH

+2

this shows how you do it in C ++ ... read consecutive snippets from the end of the file, then scan chunks for new lines. if a new line is not found, part of the fragment should be saved and merged with the next fragment, which is read in ...

//
// USAGE: lastln COUNT [FILE]
//
// Print at most COUNT lines from the end of FILE or standard input.
// If COUNT is -1, all lines are printed.
//

#include <errno.h>
#include <libgen.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
  int ret = 0, maxLines = -1, len, count = 0, sz = 4096, lines = 0, rd;
  istream *is;
  ifstream ifs;
  stringstream ss;
  char *buf = NULL;
  const char *prog = (argc > 0 && argv[0] ? basename(argv[0]) : "");
  string line;

  if (argc > 1) {
    if ((maxLines = atoi(argv[1])) == 0) {
      goto end;
    }
  }

  if (argc > 2 && !(argv[2] && argv[2][0] == '-' && argv[2][1] == '\0')) {
    ifs.open(argv[2], ios::in | ios::binary);
    if (!ifs) {
      ret = 1;
      cerr << prog << ": " << argv[2] << ": " << strerror(errno) << endl;
      goto end;
    }
    is = &ifs;
  } else {
    ss << cin.rdbuf();
    if (!ss) {
      ret = 1;
      cerr << prog << ": failed to read input" << endl;
      goto end;
    }
    is = &ss;
  }

  is->seekg(0, ios::end);
  len = is->tellg();
  buf = new char[sz + 1];

  while (rd = min(len - count, sz)) {
    is->seekg(0 - count - rd, ios::end);
    is->read(buf, rd);
    count += rd;
    char *p = buf + rd, *q;
    *p = '\0';

    for (;;) {
      q = (char *)memrchr(buf, '\n', p - buf);
      if (q || count == len) {
        if (q) *q = '\0';
        if (lines || p - q - 1 > 0 || !q) {
          ++lines;
          cout << lines << ": " << (q ? q + 1 : buf) << line << endl;
          line.clear();
          if (lines >= maxLines && maxLines != -1) break;
        }
        if (q) p = q; else break;
      } else {
        line = string(buf, p - buf) + line;
        break;
      }
    }
  }

  end:

  if (buf) delete[] buf;
  return ret;
}
0
source

Source: https://habr.com/ru/post/1728842/


All Articles