Reputation: 21
my task is to replace a string with another string in a file line.
I am still in the beginning of my studies in C programming
language and I am facing some problems. I should find the lines beginning with #define
, get their values and delete them. Then wherever the constant name appears, it should be replaced with its value. I have already saved the define values from the file but I have some difficulties in replacing the constant names with their values. So far I have two arrays where I have saved the constant names and the values:
char constant_names[256];
char constant_values[256];
I will be so thankful if you can give me some ideas about the solution of the task.
Upvotes: 0
Views: 241
Reputation: 153507
I will be so thankful if you can give me some ideas about the solution of the task
As OP is looking for ideas:
A key issue of that the pattern string and its replacement string may be of different lengths.
Read and process each line and save to another file.
Pseudo code:
Open file for reading
Open a temp file for writing
For each line
if "define" found
add to internal substitution list
else
if a substitutable token found
perform substitutions
write line to temp file
close files
rename original file to tmp2
rename tmp file to original
if no errors anywhere
delete tmp2
I'd expect about 100-200 lines of C code.
Upvotes: 1