ahmet
ahmet

Reputation: 5005

Replacing a string inside a txt line by line

How would i check for the strings below line by line in a txt and replace each one?

"FLD_CUSTORDREF_FLD"                 
"FLD_CUSTOMERNAME_FLD"            
"FLD_ADDRESSLINE1_FLD"             
"FLD_ADDRESSLINE2_FLD"             
"FLD_CITY_FLD"           

e.g.

If file line has "FLD_CITY_FLD"
  change to Header->City
else if file line has "FLD_CUSTOMERNAME_FLD" 
  change to Header->Name

I can read each line by using

while ( fgets ( line, sizeof line, file ) != NULL ) 

But how would i check for a matching string?

Upvotes: 0

Views: 84

Answers (1)

cnicutar
cnicutar

Reputation: 182649

You're looking for the strstr function.

char *strstr(const char *haystack, const char *needle);

The strstr() function finds the first occurrence of the substring needle in the string haystack.

However the replacing part might be trickier. I would just fprintf the result instead of replacing in the string.

Upvotes: 4

Related Questions