0

I have very new to whole C++ programming and learning it on the fly.

I want to build a DLL that would read text output from program A and insert this text at various locations in a template file to produce a file for program B.

Output from program A looks like this: FileA

1    A  23
2    B  4
3    C  5
4    D  4
5    E  2
6    A  6
7    B  7
8    C  55
9    D  66
10   E  8
11   A  2
12   B  34
13   C  55
14   D  2
15   E  1
16   1  0.45
17   2  0.45
18   3  0.10

I want to read the lines 1-5, 6-10, 11-15 and 16-18 and replace the text between markers in this Template file:

-Start1   #marker
A   1
B   1
C   1
D   1
E   1
-End1     #marker
//some text
-Start2   #marker
A   2
B   2
C   2
D   2
E   2
-End2     #marker
//some text
-Start3   #marker
A   3
B   3
C   3
D   3
E   3
-End3     #marker
//some text
-Start4    #marker
1   1
2   2
3   3
-End4      #marker
//some text

This will produce FileB for program B.

A   23
B   4
C   5
D   4
E   2
//some text
A   6
B   7
C   55
D   66
E   8
//some text
A   2
B   34
C   55
D   2
E   1
//some text
1   0.45
2   0.45
3   0.10
//some text

I have a code that can deal with just one block of text replacement but I can't figure out how to handle four text replacements at different locations using one C++ source file.

void ReadTemplate(const double inputs[], const int numArgs)
{
    char buffer[500];
    int i;
    std::ifstream infile("input_template.txt");
    std::ofstream outfile("output.txt");
    while (!infile.eof())
    {
        infile.getline((char*)buffer, sizeof(buffer));
        if (strcmp(buffer, "-Start1") == 0)
        {
            break;
        }//Stop reading when start marker reached. 
        outfile << buffer << '\n'; //dump lines to outfile
    }

    //replace the existing text in infile with the text produced by the program:
    for (i = 0; i < numArgs; i++)
    {
        infile.getline((char*)buffer, sizeof(buffer));
        std::istrstream insert1(buffer);
    
    infile.getline((char*)buffer, sizeof(buffer));

    while (!infile.eof())
    {
        infile.getline((char*)buffer, sizeof(buffer));
        if (strcmp(buffer, "-End1") == 0)
        {
            break;
        }//Stop reading when start marker reached. 
        outfile << buffer << '\n';
    }

    //  
    // Add code for other three text replacements????
    // 
    
    infile.close();
    outfile.close();

    return;
}

I would like to provide actual files but not sure how to. Any help would be greatly appreciated. Thanks

BRat
  • 61
  • 3
  • Unrelated: Give [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) a read. – user4581301 Oct 05 '20 at 05:18

1 Answers1

0

You need to create a state machine so you know how to process each line. This state machine has two states (inside a section or not). The first two cases are transitions between the two, the last two cases handle non-transition lines.

std::string line;
std::string current_section;
while (std::getline(infile, line)) {
    if (line.find("-Start") == 0) {
        // extract first word
        std::istringstream(line) >> current_section;
    } else if (line.find("-End") == 0) {
        current_section = {};
    } else if (current_section.empty()) {
        outfile << line << "\n";
    } else {
      std::string token;
      std::istringstream(line) >> token;
      // TODO for you: implement lookup based on current_section and token.
      outfile << token << " " << lookup(current_section, token) << "\n";
    }
}
Botje
  • 15,729
  • 2
  • 22
  • 32