remove some shi
This commit is contained in:
@@ -1,107 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <climits>
|
||||
|
||||
static bool file_exists(const std::string &path) {
|
||||
struct stat st{};
|
||||
return ::stat(path.c_str(), &st) == 0; // stat success => exists [web:59]
|
||||
}
|
||||
|
||||
static std::string make_out_name(const std::string &in) {
|
||||
std::size_t pos = in.find_last_of('.');
|
||||
if (pos == std::string::npos) return in + ".out";
|
||||
return in.substr(0, pos) + ".out";
|
||||
}
|
||||
|
||||
static void print_usage(const char* prog) {
|
||||
std::cerr << "Usage: " << prog << " <input_file> <replace_count>\n"; // positional args [web:59]
|
||||
}
|
||||
|
||||
static bool parse_ll(const char* s, long long& out) {
|
||||
if (!s || !*s) return false;
|
||||
errno = 0;
|
||||
char* end = nullptr;
|
||||
long long v = std::strtoll(s, &end, 10); // use endptr and errno [web:59]
|
||||
if (errno == ERANGE) return false;
|
||||
if (end == s || *end != '\0') return false;
|
||||
out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
print_usage(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string in_path = argv[1];
|
||||
long long replace_limit = 0;
|
||||
if (!parse_ll(argv[2], replace_limit) || replace_limit < 0) {
|
||||
std::cerr << "Error: replace_count must be a non-negative integer.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!file_exists(in_path)) {
|
||||
std::cerr << "Error: input file does not exist.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ifstream in(in_path);
|
||||
if (!in) {
|
||||
std::cerr << "Error: cannot open input file.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string out_path = make_out_name(in_path);
|
||||
std::ofstream out(out_path);
|
||||
if (!out) {
|
||||
std::cerr << "Error: cannot create output file.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
long long performed = 0;
|
||||
std::string line;
|
||||
|
||||
while (performed < replace_limit && std::getline(in, line)) { // stop early when limit hit [web:59]
|
||||
if (!line.empty()) {
|
||||
char first = line.front();
|
||||
for (char &c: line) {
|
||||
if (c == first && c != ' ') {
|
||||
c = ' ';
|
||||
++performed;
|
||||
if (performed >= replace_limit) break; // cap reached [web:44]
|
||||
}
|
||||
}
|
||||
}
|
||||
out << line;
|
||||
if (!in.eof()) out << '\n';
|
||||
if (!out) {
|
||||
std::cerr << "Error: write error.\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// If limit reached mid-file, copy the rest unchanged
|
||||
if (performed >= replace_limit) { // stream may be mid-line or between lines [web:59]
|
||||
// Write remaining lines as-is
|
||||
if (!in.eof()) {
|
||||
// Flush any remaining part of current line already written; then dump rest
|
||||
std::string rest;
|
||||
while (std::getline(in, rest)) {
|
||||
out << rest;
|
||||
if (!in.eof()) out << '\n';
|
||||
if (!out) {
|
||||
std::cerr << "Error: write error.\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << performed << std::endl; // print actual replacements up to limit [web:59]
|
||||
return 0; // success [web:59]
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
g++ -std=c++17 -O2 -Wall -Wextra 12_first_symbols_to_spaces.cpp -o 12_first_symbols_to_spaces
|
||||
@@ -1,21 +0,0 @@
|
||||
Здравствуйте, Андрей Васильевич!
|
||||
|
||||
TLDR:
|
||||
ssh pajjilykk@cs.webmaple.ru
|
||||
cd /home/pajjilykk/CS_LABS/lab_1
|
||||
cat README.txt
|
||||
|
||||
Сообщаю, что лабораторная работа №1 по программированию выполнена и размещена на сервере. Данные для доступа и проверки:
|
||||
|
||||
Адрес сервера: cs.webmaple.ru
|
||||
|
||||
Логин: pajjilykk
|
||||
|
||||
Пароль: NSTU.CS
|
||||
|
||||
Путь к коду: /home/pajjilykk/CS_LABS/lab_1
|
||||
|
||||
Готов ответить на вопросы по коду и/или защите.
|
||||
|
||||
С уважением,
|
||||
Куриленко Платон, группа АВТ-418
|
||||
@@ -1,8 +0,0 @@
|
||||
abcabcABC
|
||||
Aaaaaa
|
||||
1112233111
|
||||
,,,,.,,
|
||||
leading spaces here
|
||||
tab leading here
|
||||
first-letter f repeats: f fff f-f_f
|
||||
ZzzZzZ
|
||||
@@ -1 +0,0 @@
|
||||
./12_first_symbols_to_spaces file.in 3
|
||||
@@ -1,53 +0,0 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#define BUF_SIZE 1024
|
||||
// пример программы обработки текстового файла средствами системых вызовов Linux
|
||||
// учебник "Системное программирование в среде Linux", Гунько А.В., стр. 22
|
||||
int main (int argc, char * argv [ ])
|
||||
{
|
||||
int inputFd, outputFd, openFlags;
|
||||
mode_t filePerms ;
|
||||
ssize_t numRead;
|
||||
char buf[BUF_SIZE];
|
||||
if (argc != 3)
|
||||
{
|
||||
printf("Usage: %s old-file new-file \n", argv[0]); exit(-1);
|
||||
}
|
||||
/* Открытие файлов ввода и вывода */
|
||||
inputFd = open (argv[1], O_RDONLY);
|
||||
if (inputFd == -1)
|
||||
{
|
||||
printf ("Error opening file %s\n", argv[1]) ; exit(-2);
|
||||
}
|
||||
openFlags = O_CREAT | O_WRONLY | O_TRUNC;
|
||||
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw - rw - rw - */
|
||||
outputFd = open (argv [2], openFlags, filePerms);
|
||||
if (outputFd == -1)
|
||||
{
|
||||
printf ("Error opening file %s\n ", argv[2]) ; exit(-3);
|
||||
}
|
||||
/* Перемещение данных до достижения конца файла ввода или возникновения ошибки */
|
||||
while ((numRead = read (inputFd, buf, BUF_SIZE)) > 0)
|
||||
{
|
||||
if (write (outputFd, buf, numRead) != numRead)
|
||||
{
|
||||
printf ("couldn't write whole buffer\n "); exit(-4);
|
||||
}
|
||||
if (numRead == -1)
|
||||
{
|
||||
printf ("read error\n "); exit(-5);
|
||||
}
|
||||
if (close (inputFd ) == -1 )
|
||||
{
|
||||
printf ("close input error\n"); exit(-6);
|
||||
}
|
||||
if (close (outputFd ) == -1 )
|
||||
{
|
||||
printf ("close output error\n"); exit(-7);
|
||||
}
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
// пример программы обработки текстового файла средствами системых вызовов Linux
|
||||
// учебник "Системное программирование в среде Linux", Гунько А.В.
|
||||
int main(int argc, char *argv[]) {
|
||||
int handle, numRead, total= 0;
|
||||
char buf;
|
||||
if (argc<2) {
|
||||
printf("Usage: file textfile\n");
|
||||
exit(-1);
|
||||
}
|
||||
// низкоуровневое открытие файла на чтение
|
||||
handle = open( argv[1], O_RDONLY);
|
||||
if (handle<0) {
|
||||
printf("Error %d (%s) while open file: %s!\n",errno, strerror(errno),argv[1]);
|
||||
exit(-2);
|
||||
}
|
||||
// цикл до конца файла
|
||||
do {
|
||||
// посимвольное чтение из файла
|
||||
numRead = read( handle, &buf, 1);
|
||||
if (buf == 0x20) total++; }
|
||||
while (numRead > 0);
|
||||
// Закрытие файла
|
||||
close( handle);
|
||||
printf("(PID: %d), File %s, spaces = %d\n", getpid(), argv[1], total);
|
||||
// возвращаемое программой значение
|
||||
return( total); }
|
||||
@@ -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