Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a program on Windows in regular C using Visual Studio 2017.

I'm reading a file one line at a time into a char[1024] variable and then writing it back out to another file.

The line can contain none, one, or several occurrences of: @{varname}

I need to replace @{varname} in the output file with a value from a database that I lookup using the value between the brackets.

The value from the database could be larger than 1024 so I need to write the portion of the line prior to the @ to the output file, then write the value from the database, then write the portion of the line after the }.

Conceptually it is like a mail merge. I just need some help figuring out the best way to parse out the parts of the line. I control the format of the input file so could change the characters around the variable names if needed.

What I have tried:

I've tried several things including strtok and various str**** runtime functions.
Posted
Updated 10-Jan-20 21:20pm

1 solution

Start by setting up an output line to copy into.
Then process the input line:
1) Create a variable called foundAt, set it to false.
2) Loop through input
2.1) If  foundAt is true, check if this is a '{'.
2.1.1) If it is, loop though the input to find the matching '}'. This give you the name, so you can search the db, and copy the replacement string to the output. Set foundAt to false and use <code>continue</code> to skip the rest of your loop body.
2.1.2) If it isn't, copy an '@' to the output and clear foundAt
2.2 Is the input character an '@'?
2.2.1) If it is, set foundAt to true;
2.2.2) Otherwise, copy the character from the input to the output
3) Move on to the next input character and loop round again until all inputs are processed.


Make sense?
 
Share this answer
 
Comments
CPallini 11-Jan-20 5:39am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900