77 lines
1.9 KiB
C++
77 lines
1.9 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <sys/stat.h>
|
|
|
|
using namespace std;
|
|
|
|
// ???????? ????????????? ?????
|
|
bool fileExists(const string& filename) {
|
|
struct stat buffer;
|
|
return (stat(filename.c_str(), &buffer) == 0);
|
|
}
|
|
|
|
// ????????? ????? ????????? ????? ? ??????? ?????????? ?? ".out"
|
|
string getOutputFileName(const string& inputFileName) {
|
|
size_t pos = inputFileName.find_last_of('.');
|
|
if (pos == string::npos) {
|
|
// ???? ?????????? ???, ????????? .out
|
|
return inputFileName + ".out";
|
|
}
|
|
return inputFileName.substr(0, pos) + ".out"; // ???????? ??????????
|
|
}
|
|
|
|
// ????????, ???????? ?? ?????? ?????? ??????????, ?????????? ??????
|
|
bool isSign(char c) {
|
|
return (c == '!' || c == '?' || c == '.' || c == ',' || c == ';' || c == ':');
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if (argc != 3) {
|
|
cerr << "Error: Missing arguments.\nUsage: " << argv[0] << " <inputfile> <symbol>\n";
|
|
return -1;
|
|
}
|
|
|
|
string inputFileName = argv[1];
|
|
char replacementSymbol = argv[2][0];
|
|
|
|
if (!fileExists(inputFileName)) {
|
|
cerr << "Error: Input file \"" << inputFileName << "\" does not exist.\n";
|
|
return -1;
|
|
}
|
|
|
|
string outputFileName = getOutputFileName(inputFileName);
|
|
|
|
ifstream inFile(inputFileName);
|
|
if (!inFile) {
|
|
cerr << "Error: Cannot open input file.\n";
|
|
return -1;
|
|
}
|
|
|
|
ofstream outFile(outputFileName);
|
|
if (!outFile) {
|
|
cerr << "Error: Cannot create output file.\n";
|
|
return -1;
|
|
}
|
|
|
|
int replaceCount = 0;
|
|
char ch;
|
|
while (inFile.get(ch)) {
|
|
if (isSign(ch)) {
|
|
ch = replacementSymbol;
|
|
replaceCount++;
|
|
}
|
|
outFile.put(ch);
|
|
if (!outFile) {
|
|
cerr << "Error: Write error occurred.\n";
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
inFile.close();
|
|
outFile.close();
|
|
|
|
cout << replaceCount << endl; // ??????? ????? ?????
|
|
return replaceCount;
|
|
}
|