winapi - c - merging two text files -
say have 2 text files:
a.txt:
param1: value1 param2: value2 . . . . paramm: valuem
b.txt
param1: value1 param2: value2 . . . . paramn: valuen
number of parameters in a.txt, i.e. m can greater than, lesser or equal number of parameters in b.txt, i.e. n. values same parameter need not same in a.txt , b.txt. m , n can reach maximum value of 200. the parameter names arbitrary. have no numbers. above illustration. my objective merge a.txt , b.txt. if conflicts occur, have file/in-memory storage dictates 1 takes precedence.
for example,
a.txt like:
path: c:\program\files data: importantinfo.dat version: 1.2.3 useless: uselessparameter.txt
and
b.txt like:
path: c:\programfiles data: notsoimportant.dat version: 1.0.0 useful: usefulparameter.txt
the final text file should like:
path: c:\programfiles data: importantinfo.dat version: 1.2.3 useful: usefulparameter.txt
now approach thinking is:
get line a.txt line b.txt tokenize both ":" compare param names if same write a.txt's value result.txt else if different write a.txt's line result.txt write b.txt's line result.txt /* order doesn't matter */ repeat above steps until end of both text files
this approach doesn't take care of when a.txt has parameter , b.txt doesn't. extremely helpful if lightweight library/apis exist this. not looking command-line tools (thanks overhead of doing system()
calls).
thanks!
this much easier in language has map datatyp. this:
read of b , store key-value strings in memory
read , each key either overwrite value (if exists) or add together (if doesn't)
output key-values file
c winapi file-io
No comments:
Post a Comment