Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 line
1.1KB

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int main(int argc, char* argv[]) {
  6. // check if correct argument count
  7. if (argc < 2) {
  8. printf("Dateiname erwartet\n");
  9. return 1;
  10. }
  11. // get filename from argument list
  12. string filename = argv[1];
  13. // open input file
  14. ifstream fileIn;
  15. fileIn.open(filename + ".txt");
  16. // open output file
  17. ofstream fileOut;
  18. fileOut.open(filename + "-a.txt");
  19. // check if we could open the files
  20. if (!fileIn.good() || !fileOut.good()) {
  21. // could not open files
  22. cout << "Konnte Datei " + filename + ".txt nicht lesen" << endl;
  23. return 1;
  24. }
  25. string input; // variable containing line
  26. int lineNo = 1; // count lines
  27. while(getline(fileIn, input)) { // while there is still a line left
  28. // write out line with prepended line number to output file
  29. fileOut << to_string(lineNo) + ": " + input << endl;
  30. // increment line counter
  31. lineNo += 1;
  32. }
  33. // close files
  34. fileIn.close();
  35. fileOut.close();
  36. }