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