update README.md 2

This commit is contained in:
2025-10-01 18:40:39 +07:00
parent b630a89426
commit adf8edd882
5 changed files with 26 additions and 4 deletions

View File

@@ -1,4 +1,26 @@
1. Скомпилировать с помощью compile.sh
2. Запустить с помощью run.sh
3. Для теста менять значения в file.in, для проверки ошибок - вручную запускать с неправильными данными.
Использование: ./12_first_symbols_to_spaces <input_file>
## README.md
### Кратко
Программа построчно заменяет все символы, равные первому символу строки, на пробел; пишет результат в файл с суффиксом .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
```
### Примечания
- Замены считаются только при реальной замене; если первый символ — пробел, такие позиции не увеличивают счётчик.

View File

@@ -1,5 +0,0 @@
Hi! This is a test.
Wait: are commas, periods, and exclamations replaced?
Let's check!
@#$%&*)_+

View File

@@ -1,6 +0,0 @@
Hi9 This is a test9
Wait9 are commas9 periods9 and exclamations replaced9
Let's check9
@#$%&*)_+

Binary file not shown.

View File

@@ -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;
}