40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
int main(int argc, char* argv[]) {
|
|
// check if correct argument count
|
|
if (argc < 2) {
|
|
printf("Dateiname erwartet\n");
|
|
return 1;
|
|
}
|
|
|
|
// get filename from argument list
|
|
string filename = argv[1];
|
|
// open input file
|
|
ifstream fileIn;
|
|
fileIn.open(filename + ".txt");
|
|
// open output file
|
|
ofstream fileOut;
|
|
fileOut.open(filename + "-a.txt");
|
|
// check if we could open the files
|
|
if (!fileIn.good() || !fileOut.good()) {
|
|
// could not open files
|
|
cout << "Konnte Datei " + filename + ".txt nicht lesen" << endl;
|
|
return 1;
|
|
}
|
|
string input; // variable containing line
|
|
int lineNo = 1; // count lines
|
|
while(getline(fileIn, input)) { // while there is still a line left
|
|
// write out line with prepended line number to output file
|
|
fileOut << to_string(lineNo) + ": " + input << endl;
|
|
// increment line counter
|
|
lineNo += 1;
|
|
}
|
|
// close files
|
|
fileIn.close();
|
|
fileOut.close();
|
|
}
|