C doc assembly

ledeni
ledeni used Ask the Experts™
on
I am a big time newb using C and I would appreciate any help that you may provide.

I need to build C program that will take 3 files....
one which is going to be a template letter, one input persons info and one that is going to be a an output file.

The input file will have something like (first 5 char may not be hard coded and they may change) and than multiple people info.
*
$$
@
##
!
persons name
last name
address
town
zip

than program will read the template file and output lines to output file. If encounters the characters in the template file it will replace it with person info on fly and send it to the output file. That will also be repeated for number of persons within input file.

If any of you was lucky enough to solve this problem before or if you know where I can get a free working code I would highly appreciate. This problem means a lot to me so I am placing all my points to it.

By the way ... I have some code written if you would like to take a look at but  it just does not want to work for me. :)

THANKS !!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Could you post the code that you have?
CEO/Programmer
Commented:
Well I though this is a somewhat interesting problem. But I'm  quite sure that it might be a homework. Therfor I just implemented some IMHO interesting stuff.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void replace_pattern(char * read_line,
          char* pattern, char* replacement,
          char *output_line,
             size_t len_output_line){
  size_t pos = 0;
  size_t old_pos = 0;
  size_t len_replacement = strlen(replacement);
  size_t len_pattern = strlen(pattern);
  size_t len_read_line = strlen(read_line);
  size_t len_out = 0;
  output_line[0] = '\0';

  pos = strcspn(read_line, pattern);
  while (pos < len_read_line){
     len_out += pos - old_pos + len_replacement;
     if (len_out < len_output_line){
          strncat (output_line, (read_line +  old_pos), pos - old_pos);
               strncat (output_line, replacement, len_replacement);
               output_line[len_out] = '\0';
          } else {
               output_line[len_output_line] = '\0';
               return;
          }
          pos += len_pattern;
          old_pos = pos;
          if (pos < len_read_line){
               pos = pos + strcspn((read_line + pos), pattern);
          }
     }
     output_line[len_out] = '\0';
}



int main (int argc, char * argv[]){
     char read_line[120];
     char output_line[240];

     strcpy(read_line, argv[1]);
     replace_pattern(read_line, "*", "someone", output_line, 240);
     printf("output_line = %s\n", output_line);
     return 0;
}

It seems to do the replacement
./a.out "* this is a * template line *"
output_line = someone this is a someone template line someone

I did not care very much about efficiency, (the strcat is not very efficient, and could be easily replace with a strcpy construct. But I think the algorithm is clear and understandable.

The stuff around is not difficult. The only thing which probably needs some consideration is no how to associate the pattern with the replacement. I would think an arry of a structure would not be the worst idea. But I guess it can't be my task to figure that out too.

On the other hand there is AWK in which a solution would be a twenty liner ;-). I don't feel like C is the proper tool for that task here...

Regards
Friedrich

Author

Commented:
Sorry Friedrich but this code does not work. When I compile it and run ti with a pattern and input file .. it produces nothing but

output_line =
and exits the program ...

However its probably my mistake not specifying it correctly.

I need something like .. lets say I have an input text  file..

Dear #, $$

Line @@ dasdalsd $$ asdlkasdl kA #

and Data text file that contains the

#
$$
@@
Person 1 Name
Person 1 Surname
person 1 Address
Person 2 Name
Person 2 Surname
person 2 Address
etc.

so # represents the name $$ surname and @@ address

so the code will read the pattern file and input file, replace the characters and create a new file that contains the persons info ... and it will repeat that for the number of persons in the input file.

Thanks,
L
fridomCEO/Programmer

Commented:
It works as intended, it's you term to change the read-in from  the command line to read the lines from a file. As told I do think it's homework. And therefor I do not post a "fully" fledged solution. Try to understand what the code is doing and on which data, than change it to suit your data.

Regards
Friedrich

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial