RAHUL
RAHUL

Reputation: 1

How to replace a data in a file with another data from another file?

I'm trying to open this file (final.txt) and read the contents:

c0001
f260
L
D11
H30
R0000
C0040
1X1100000100010B300300003003
181100202900027Part No
181100202900097[PRTNUM]
1e5504002400030B
1X1100002300010L300003
191100202000030Quantity
191100202000080[QUANTY]
1e5504001500040B
1X1100001400010L300003
1X1100001400150L003090
191100202000170P.O.No
191100202000220[PONUMB]
1e5504001500180B
191100201200030Supplier
1e3304000700030B
1X1100000600010L300003
181100200300030Serial
181100200300090[SERIAL]
171100300900190Rev
171100300300190[REV]
171100300900240Units
171100300300240[UNITS]
1X1100000100180L003130
Q0001
E

from which I am reading only [PRTNUM], [QUANTY], [PONUMB], [SERIAL], [UNITS].

I've written the following C program:

char* cStart = strchr(cString, '[');

if (cStart)
{
    // open bracket found
    *cStart++ = '\0'; // split the string at [
    char* cEnd = strchr(cStart, ']');
    // you could check here for the close bracket being found
    // and throw an exception if not
    *cEnd = '\0'; // terminate the keyword
    printf("Key: %s, Value: %s",cString, cStart);
}
// continue the loop

but now I want to replace these placeholders with data from the 2nd file:

132424235
004342
L1000
DZ12
234235
234235

I want to replace [PRTNUM] (from the 1st file) with 132424235 and so on... In the end my file should be updated with all this data. Can you tell me what function I should use in the above program?

Upvotes: 0

Views: 78

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

If you don't mind having an alternate approach, here's an algorithm to do the work in an elegant way

  1. Create one (large enough) temporary buffer. Also, create (open) one output file which will be the modified version.
  2. Read a line from the input file into the buffer using fgets()
  3. Search for the particular "keyword" using strstr()
  4. If a match is found --

    4.1. Open the other input file.

    4.2. Read the corresponding data (line), using fgets()

    4.3. Replace the actual data in the temporary buffer with the newly read value.

    4.4. write the modified data to the output file.

  5. If match is not found, write the original data in the output file. Then, go to step 2.

  6. Continue until fgets() returns NULL (indicates the file content has been exhausted).

Finally, the output file will have the data from the first file with those particular "placeholders" substituted with the value read from the second file.

Obviously, you need to polish the algorithm a little bit to make it work with multiple "placeholder" string.

Upvotes: 1

cruxion effux
cruxion effux

Reputation: 1054

Keep an extra string(name it copy) large enough to hold file 1 + some extra to manage replacement of [PRTNUM] with 132424235.

Start reading first string that has file1 and keep copying into second string (copy) as soon as you encounter [PRTNUM] , in string 2 instead of copying [PRTNUM] you append it with 132424235 and so on for all others.

And finally replace file1.txt with this second (copy) string.

Upvotes: 0

Related Questions