Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <stdio.h>
- #define DEFAULT_BUFFER_SIZE 64
- using namespace std;
- string getFilePathFromTheConsole() {
- char filePath[DEFAULT_BUFFER_SIZE];
- printf("\nPlease, specify the path to the text file of your desire. \nPath: ");
- cin.getline(filePath, DEFAULT_BUFFER_SIZE);
- return string(filePath);
- }
- template<typename Container>
- bool in_quote(const Container& cont, const string& s)
- {
- return search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end();
- }
- string readFromTheFile(string &filePath) {
- FILE* file;
- if ((file = fopen(filePath.c_str(), "r+")) == NULL) {
- cerr << "Unable to open the file for reading." << endl;
- return NULL;
- }
- string result;
- char readChar;
- while ((readChar = fgetc(file)) != EOF) {
- result += readChar;
- }
- fclose(file);
- return result;
- }
- string cutfromcode(string &originalString) {
- string changedString;
- changedString = originalString;
- int stcount = changedString.find('0');
- while (stcount!=-1) {
- changedString.erase(stcount , 1);
- changedString.insert(stcount, "[zero]");
- stcount = changedString.find('0');
- }
- stcount = changedString.find('1');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[one]");
- stcount = changedString.find('1');
- }
- stcount = changedString.find('2');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[two]");
- stcount = changedString.find('2');
- }
- stcount = changedString.find('3');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[tree]");
- stcount = changedString.find('3');
- }
- stcount = changedString.find('4');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[four]");
- stcount = changedString.find('4');
- }
- stcount = changedString.find('5');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[five]");
- stcount = changedString.find('5');
- }
- stcount = changedString.find('6');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[six]");
- stcount = changedString.find('6');
- }
- stcount = changedString.find('7');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[seven]");
- stcount = changedString.find('7');
- }
- stcount = changedString.find('8');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[eight]");
- stcount = changedString.find('8');
- }
- stcount = changedString.find('9');
- while (stcount != -1) {
- changedString.erase(stcount, 1);
- changedString.insert(stcount, "[nine]");
- stcount = changedString.find('9');
- }
- return changedString;
- }
- bool writeIntoTheFile(string &filePath, string content, bool append) {
- FILE* file;
- if ((file = fopen(filePath.c_str(), (append ? "a+" : "w+"))) == NULL) {
- cerr << "Unable to open the file for writing." << endl;
- return false;
- }
- int charCount = strlen(content.c_str());
- for (int i = 0; i < charCount; i++) {
- fputc(content.c_str()[i], file);
- }
- fclose(file);
- return true;
- }
- void main(){
- string filePath = getFilePathFromTheConsole();
- string readData = readFromTheFile(filePath);
- string uppercasedData = cutfromcode(readData);
- writeIntoTheFile(filePath, uppercasedData, false);
- printf("\n - Read Data -> %s \n - Edited Data -> %s", readData.c_str(), uppercasedData.c_str());
- printf("\n - Editing was done successfully.\n");
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement