Help - C++ Read File and substi

Asked

Viewed 248 times

-1

Good evening, everyone I have a question in c++ I need to read a file and replace some characters with another one and generate a new file. Could someone help me

  • https://www.devmedia.com.br/forum/substr-uma-sequence-of-characters-em-c/505368 This would help you?

  • 1

    Yes. To do this, go to [Dit] and rephrase your question by placing the code you have done so far, describe the difficulties encountered, the errors they have given, list what still remains to be done, describe the results obtained so far, the desired results and make a comparison between both. Also seek to elaborate a [mcve], read the guide of [Ask] and do the [tour] to better learn the operation of the site. If you have any questions, go to [help] and [meta].

1 answer

1

See if this helps.. (tested on onlinegdb)

#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <sstream> //std::stringstream
#include <boost/algorithm/string/replace.hpp>

using namespace std;

int main()
{

// CRIO O ARQUIVO 
std::ofstream outfile ("test.txt");

outfile << "conteudo do arquivo!" << std::endl;

outfile.close();
//FIM DA CRIACAO DO ARQUIVO

//ABRE ARQUIVO
std::ifstream inFile;
inFile.open("test.txt"); //open the input file

std::stringstream strStream;
strStream << inFile.rdbuf(); //read the file
std::string str = strStream.str(); //str holds the content of the file

std::cout << str; //you can do anything with the string!!!

std::string s = str;
boost::replace_all(s, "arquivo", "ficha"); // REPLACE -> SUBSTITUI
//std::replace( s.begin(), s.end(), 'arquivo', 'ficha'); // replace all 'x' to 'y'

std::cout << s;
return 0;
}

code testing https://onlinegdb.com/B1vCwLOOV

Browser other questions tagged

You are not signed in. Login or sign up in order to post.