give me coding for search and edit a file in c++

Dani AI

Generated

A concise, practical complement to the thread: asked for code to search and edit a file; rightly pointed to fstream and gave a Boost.Serialization option for object persistence. The gap here is a small, copy-paste example that shows a safe, common pattern for text-file search-and-replace: read (or stream) the file, write changes to a temporary file, then atomically replace the original. The snippet below uses C++17 std::filesystem and performs a byte-accurate substring replace (suitable for many text files).

#include <fstream>
#include <string>
#include <filesystem>

bool replace_in_file(const std::string& path,
                     const std::string& target,
                     const std::string& replacement,
                     bool replaceAll = true)
{
    std::ifstream in(path, std::ios::binary);
    if (!in) return false;
    std::string content((std::istreambuf_iterator<char>(in)),
                        std::istreambuf_iterator<char>());
    in.close();

    std::size_t pos = 0;
    bool changed = false;
    while ((pos = content.find(target, pos)) != std::string::npos) {
        content.replace(pos, target.size(), replacement);
        changed = true;
        pos += replacement.size();
        if (!replaceAll) break;
    }
    if (!changed) return false;

    auto tmp = std::filesystem::temp_directory_path() /
               (std::filesystem::path(path).filename().string() + ".tmp");
    std::ofstream out(tmp, std::ios::binary | std::ios::trunc);
    out << content;
    out.close();

    std::filesystem::rename(tmp, path); // atomic on many systems
    return true;
}

Notes and cautions: this approach reads the whole file into memory (good for small/medium files). For very large files, stream and transform line-by-line into a temp file to avoid excessive memory use. In-place editing without rewriting is only safe when replacements have identical byte length. Beware of encoding and line-ending issues (CRLF vs LF) and concurrent access—keep backups or lock the file if necessary. For structured data (CSV/JSON) parse fields and update them, or use Boost.Serialization for object storage as suggested by . Finally, providing a short sample input line and the desired result—as requested—makes targeted help possible.

Recommended Answers

All 8 Replies

Give me money and you have a deal

use fstream


its a forum to help not make money !

I know that. He wanted the code so I told him to give me money. It was an unresonable request like him just asking for code.

I've sometimes said that too -- it's a toung-in-cheek way of saying we aren't going to write the program. But ... if someone really puts a million dollars usd in my paypal I'll gladly see to it that he/she gets the program.

Yeah I wouldn't argue at that point.

give me coding for search and edit a file in c++

Can you supply an example of the types of files you wish to search ... and the types of editing that you wish to do?

Also, please show the code that you have coded so far, to do the desired job.

Its true you have to make an effort on your own behalf. But here is the sugar you are after. Read http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html
(All of it!) and here is how to write and read - you can choose whether you want binary or text. Let me know if you need more help.

#include <iostream>
#include <fstream>
#include <boost\archive\xml_iarchive.hpp>    // XML In
#include <boost\archive\xml_oarchive.hpp>    // XML Out
#include <boost\archive\text_iarchive.hpp>   // Text In
#include <boost\archive\text_oarchive.hpp>   // Text Out
#include <boost\archive\binary_iarchive.hpp> // Binary In
#include <boost\archive\binary_oarchive.hpp> // Binary Out


int main()
{
    unsigned int iFlags = boost::archive::no_header;

    {
        // ofstream ofs("filename.txt", std::ios::trunc);
        ofstream ofs("filename.bin", std::ios::binary);

        //boost::archive::xml_oarchive oa(ofs, iFlags);
        //boost::archive::text_oarchive oa(ofs, iFlags);
        boost::archive::text_oarchive oa(ofs, iFlags);
        int i = 42;
        string s = "hello";
        oa << i << s;
    }
    {
        ifstream ifs("filename.bin", std::ios::binary);

        //boost::archive::xml_iarchive ia(ifs, iFlags);
        //boost::archive::text_iarchive ia(ifs, iFlags);
        boost::archive::text_iarchive ia(ifs, iFlags);
        int i = 9;
        string s;
        ia >> i;
        ia >> s;
        cout << i << " " << s.c_str();
    }

    cin.get();
    return 0;
}

It writes an integer and a string then reads it from a binary file. Read in the same way you have written out. I hope it starts you out on the right path.

Its true you have to make an effort on your own behalf. But here is the sugar you are after. Read boost.Serialization
(All of it!) and here is how to write and read - you can choose whether you want binary or text. Let me know if you need more help.

#include <iostream>
#include <fstream>
#include <boost\archive\xml_iarchive.hpp>    // XML In
#include <boost\archive\xml_oarchive.hpp>    // XML Out
#include <boost\archive\text_iarchive.hpp>   // Text In
#include <boost\archive\text_oarchive.hpp>   // Text Out
#include <boost\archive\binary_iarchive.hpp> // Binary In
#include <boost\archive\binary_oarchive.hpp> // Binary Out


int main()
{
    unsigned int iFlags = boost::archive::no_header;

    {
        // ofstream ofs("filename.txt", std::ios::trunc);
        ofstream ofs("filename.bin", std::ios::binary);

        //boost::archive::xml_oarchive oa(ofs, iFlags);
        //boost::archive::text_oarchive oa(ofs, iFlags);
        boost::archive::text_oarchive oa(ofs, iFlags);
        int i = 42;
        string s = "hello";
        oa << i << s;
    }
    {
        ifstream ifs("filename.bin", std::ios::binary);

        //boost::archive::xml_iarchive ia(ifs, iFlags);
        //boost::archive::text_iarchive ia(ifs, iFlags);
        boost::archive::text_iarchive ia(ifs, iFlags);
        int i = 9;
        string s;
        ia >> i;
        ia >> s;
        cout << i << " " << s.c_str();
    }

    cin.get();
    return 0;
}

It writes an integer and a string then reads it from a binary file. Read in the same way you have written out. I hope it starts you out on the right path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.