update README.md 2
This commit is contained in:
@@ -1,4 +1,28 @@
|
||||
1. Скомпилировать с помощью compile.sh
|
||||
2. Запустить с помощью run.sh
|
||||
3. Для теста менять значения в file.in, для проверки ошибок - вручную запускать с неправильными данными.
|
||||
Использование: ./12_first_symbols_to_spaces <input_file>
|
||||
## README.md
|
||||
### Кратко
|
||||
Работу выполнил студент группы АВТ-418 Куриленко Платон Семенович, вариант 12.
|
||||
|
||||
Программа построчно заменяет все символы, равные первому символу строки, на пробел; пишет результат в файл с суффиксом .out и выводит число замен в stdout.
|
||||
|
||||
### Сборка
|
||||
```bash
|
||||
./compile.sh
|
||||
```
|
||||
|
||||
### Использование
|
||||
```bash
|
||||
./12_first_symbols_to_spaces <input_file>
|
||||
```
|
||||
Имя вывода: исходное имя с заменой последнего расширения на .out, либо добавлением .out (пример: a.txt → a.out; data → data.out).
|
||||
|
||||
### Коды возврата
|
||||
- 0 при успехе; число замен также печатается в stdout.
|
||||
- -1 при ошибке: сообщение об ошибке выводится в stderr.
|
||||
|
||||
### Быстрый тест
|
||||
```bash
|
||||
./run.sh
|
||||
```
|
||||
|
||||
### Примечания
|
||||
- Замены считаются только при реальной замене; если первый символ — пробел, такие позиции не увеличивают счётчик.
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
Hi! This is a test.
|
||||
Wait: are commas, periods, and exclamations replaced?
|
||||
Let's check!
|
||||
@#$%&*)_+
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
Hi9 This is a test9
|
||||
Wait9 are commas9 periods9 and exclamations replaced9
|
||||
Let's check9
|
||||
@#$%&*)_+
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,76 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user