Merge two text files with tags

I have to develop a code that merges two text files but i have little clue on how am i supposed to attack this problem.

This is what's it:

Suppose you have a .txt file named template
it contains lines with multiple tags which are limited by the characters "<" and ">".

For example, inside template.txt:
-----------------------------------
Hello <Name> today is <Holiday> !

Have this complementary <Gift> to celebrate.

------------------------------------

You also have another text file named data.txt, which contains all the data to be contained in the tags in the following manner.
For example inside data.txt:
----------------------------
<Name> John Doe
<Holiday> Christmas
<Gift> Deluxe beer
----------------------------

I have to merge both input files into one output file which should look something like this:
-----------------------------------------------
Hello John Doe today is Christmas !

Have this complementary Deluxe beer to celebrate.
----------------------------------------

The tricky parts are the following:

The template lines can contain 1 or more tags.

The output file lines cannot exceed more than 75 characters.

If they do exceed 75 chars per line, then the rest of the line after position 75 must be placed in the next line, caring not to cut words in half.

Blank lines from original template file must be respected.

The code must apply for any variation of template and data...so the files i showed here are just an example.

thats pretty much it

thanks in advanced
sorry for bad grammar
Last edited on
I have no idea how much C++ you know, so it's hard to give you specific suggestions. All I can do is give you a general idea.

I'd open the data.txt file first and build an array (or vector or map) of tag+text pairs.
Then open the text file. Read it line by line. Break each line into words. As you scan each word, check if it starts and ends with <>. If it doesn't, write the word to the output stream. If it does start and end with <>, search your table of tags of a match and write the text corresponding to the tag to the output file. Move on to the next work in the input stream.

That should be enough to get you started.
How exactly can i break a whole line into several words?
properly after getting the line using:

getline(inputTemplate,line1);

besides if i have a : symbol immediatly after a tag

for example:

Hello <Name>:

what should i do in this particular situation?


String has a find function, so something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void scan_line (const string & line)
{   size_t curr_pos, pos1, pos2;
     string tag; 
	
    curr_pos = 0;
    do
    {   pos1 = line.find ("<", curr_pos);
         if (pos1 == string::npos)
        { //  no tags in this line
           //  write out curr_pos thru end of line
          return;  // Done with line
        }
        pos2 = line.find (">", pos1);
        if (pos2 == string::npos)  
        {   // No closing >, we have just a <
             // write out curr_pos thru end of line
             return;
        }
        // We have a tag
        tag = string(line[pos1], pos2-pos1);
        //  search tag table for matching tag
        //  write out curr_pos thru pos1-1
        //  if found write out replacement text
        //  what to do if not found?
        //  set curr_pos after >
        curr_pos = pos2+1;
     }  while (curr_pos < line.size());
}


I found another way too fix my problem.
Still thank you.
Topic archived. No new replies allowed.