// Textdatei zentriert ausgeben
//
// Aufruf: center file1
//
// Klaus Kusche, 2011

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

const int LINE_WIDTH = 80;

int main(int argc, const char *argv[])
{
  if (argc != 2) {
    cerr << "Aufruf: " << argv[0] << " infilename" << endl;
    exit(EXIT_FAILURE);
  }

  ifstream inf(argv[1]);
  if (!inf) {
    cerr << argv[0] << ": Cannot open " << argv[1] << " for reading" << endl;
    exit(EXIT_FAILURE);
  }

  char line[LINE_WIDTH + 1]; // +1 für \0
  int spaces;

  while (inf.getline(line, sizeof(line))) {
    // zuerst die richtige Anzahl einzelner Zwischenräume ausgeben ...
    for (spaces = (LINE_WIDTH - strlen(line)) / 2; spaces > 0; --spaces) {
      cout << ' ';
    }
    // ... und dann den Zeilentext
    cout << line << endl;
  }

  // wenn fail() true ist aber eof() nicht, ist ein Fehler aufgetreten
  if (!inf.eof()) {
    cerr << argv[0] << ": Cannot read " << argv[1] <<
            " (line too long?)" << endl;
    exit(EXIT_FAILURE);
  }

  inf.close();
  
  exit(EXIT_SUCCESS);
}
