c++ Modify Text File

There are those instances where unattended editions may take place on a text file.

Basic approach to modifying text files is to:

    • Place the contents into a collection of strings.
    • Modigy the string collection to reflect the desired end effect of the update.
    • Produce a new text file which includes the modificatinons from the previous step.

A c++ example of how this is accomplished is listed below.

[codesyntax lang=”cpp”]

void grailsGenFrame::updateFile()
{
    loadBuild("/home/archman/workspace/grails/grailsSkel/build.gradle");
    manipulateCollection();
    saveBuild("/home/archman/workspace/grails/grailsSkel/build.gradle2");

}

[/codesyntax]

The code to load the text file into the collection of string is as follows.

[codesyntax lang=”cpp”]

void grailsGenFrame::loadBuild(wxString inpFile)
{
    str.Clear();
    inf.Open(inpFile);
    for ( wxString fbuf = inf.GetFirstLine(); !inf.Eof(); fbuf = inf.GetNextLine() )
    {
        str.Add(fbuf);
    }
    inf.Close();

}

[/codesyntax]
The following code manipulates the collection of string by editing, inserting or deleting the text in the collection.

[codesyntax lang=”cpp”]

void grailsGenFrame::manipulateCollection()
{
    wxString insStr = "    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client:2.7.4'";
    str.Insert(insStr, 67, 1);

}

[/codesyntax]

After modifications are performed the results should be stored in a text file.

[codesyntax lang=”cpp”]

void grailsGenFrame::saveBuild(wxString outFile)
{
    out.Open(outFile);
    out.Clear();
    for (size_t i=0; i < str.Count(); i++)
    {
        out.AddLine(str[i]);
    }
    out.Write();
    out.Close();

}

[/codesyntax]

Here is the header file which will reveal pertenent globals.

[codesyntax lang=”cpp”]

/***************************************************************
 * Name:      grailsGenMain.h
 * Purpose:   Defines Application Frame
 * Author:    Mr. Arch Brooks (arch@archbrooks.com)
 * Created:   2023-02-27
 * Copyright: Mr. Arch Brooks (https://archbrooks.us/b4)
 * License:
 **************************************************************/

#ifndef GRAILSGENMAIN_H
#define GRAILSGENMAIN_H

//(*Headers(grailsGenFrame)
#include <wx/frame.h>
#include <wx/menu.h>
#include <wx/statusbr.h>
//*)

#include <wx/textfile.h>

class grailsGenFrame: public wxFrame
{
public:

    grailsGenFrame(wxWindow* parent,wxWindowID id = -1);
    virtual ~grailsGenFrame();
    wxArrayString str;
    wxTextFile inf, out;
    void loadBuild(wxString inpFile);
    void saveBuild(wxString outFile);
    void updateFile();
    void manipulateCollection();

private:

    //(*Handlers(grailsGenFrame)
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    void OnmenOpt1Selected(wxCommandEvent& event);
    //*)

    //(*Identifiers(grailsGenFrame)
    static const long ID_MENUITEM1;
    static const long idMenuQuit;
    static const long idMenuAbout;
    static const long ID_STATUSBAR1;
    //*)

    //(*Declarations(grailsGenFrame)
    wxMenuItem* menOpt1;
    wxStatusBar* StatusBar1;
    //*)

    DECLARE_EVENT_TABLE()
};

#endif // GRAILSGENMAIN_H

[/codesyntax]

This level of software allows for unattended updates of text files.  This approach will work for any number of text files.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *