Created
March 16, 2019 22:31
-
-
Save bathtime/a55f3afcdc829630254c78bf31284b69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
g++ -Wall -O2 editfile.cpp -o editfile | |
*/ | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/sendfile.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <fstream> | |
#include <iostream> | |
#include <string> | |
#include <iostream> | |
#include <cstdlib> | |
#include <cstdio> | |
#include <array> | |
using namespace std; | |
int main(int argc, char **argv) | |
{ | |
ifstream in(argv[1]); | |
ofstream out(argv[2]); | |
string wordToReplace(argv[3]); | |
string wordToReplaceWith(argv[4]); | |
if (!in) | |
{ | |
cerr << "Could not open " << argv[1] << "\n"; | |
return 1; | |
} | |
if (!out) | |
{ | |
cerr << "Could not open " << argv[2] << "\n"; | |
return 1; | |
} | |
string line; | |
size_t len = wordToReplace.length(); | |
while (getline(in, line)) | |
{ | |
while (true) | |
{ | |
size_t pos = line.find(wordToReplace); | |
if (pos != string::npos) | |
line.replace(pos, len, wordToReplaceWith); | |
else | |
break; | |
} | |
out << line << '\n'; | |
} | |
std::string command; | |
std::string inputFile(argv[1]); | |
std::string outputFile(argv[2]); | |
in.close(); | |
out.close(); | |
int read_fd; | |
int write_fd; | |
struct stat stat_buf; | |
off_t offset = 0; | |
/* Open the input file. */ | |
read_fd = open (argv[2], O_RDONLY); | |
/* Stat the input file to obtain its size. */ | |
fstat (read_fd, &stat_buf); | |
/* Open the output file for writing, with the same permissions as the | |
source file. */ | |
write_fd = open (argv[1], O_WRONLY | O_CREAT, stat_buf.st_mode); | |
/* Blast the bytes from one file to the other. */ | |
sendfile (write_fd, read_fd, &offset, stat_buf.st_size); | |
close (read_fd); | |
close (write_fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment