Create a program that processes text file according to task variant. Additional
ID: 3873927 • Letter: C
Question
Create a program that processes text file according to task variant. Additional requirements 1. A program user inputs name of file. 2. Program outputs content of file before and after modification of file. 3. Program should output text message in case of errors. 4. Program has to be written in C or C++ Deadline: December 11 Task variants Last digit of student card 1 or 6 number of Task Given an integer K and a text file, remove K leading characters from each line of the file. If the length of some line is less than K then remove all characters from the line.Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const char inFileName[] = "inputfile.txt";
const char outFileName[] = "output.txt";
ifstream inFile;
ofstream outFile;
int main() {
string line;
string temp;
inFile.open(inFileName); //open file
if (!inFile) { //check if u opened file properly or not
cout << "Error opening " << inFileName << " " << endl;
return 1;
}
outFile.open(outFileName); //open file
if (!outFile) { //check if u opened file properly or not
cout << "Error opening " << outFileName << " " << endl;
return 1;
}
int check = 0;
cin >> check;
while (getline(inFile, line)) {
outFile << line << endl;
}
outFile << endl;
inFile.close();
inFile.open(inFileName); //open file
if (!inFile) { //check if u opened file properly or not
cout << "Error opening " << inFileName << " " << endl;
return 1;
}
while (getline(inFile, line)) {
if (check < line.length()) {
outFile << line.substr(check, line.length()) << endl;
} else {
outFile << endl;
}
}
outFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.