我試圖打開這個文件(final.txt
),並讀取其中的內容:如何用另一個文件中的數據替換文件中的數據?
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
從中我只[PRTNUM]
,[QUANTY]
,[PONUMB]
,[SERIAL]
,[UNITS]
閱讀。
我已經寫了下面的C程序:
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
,但現在我想從第二個文件替換這些佔位符的數據:
132424235 004342 L1000 DZ12 234235 234235
我想替換[PRTNUM]
(從第一個文件)與132424235
等......最後,我的文件應該更新所有這些數據。你能告訴我在上面的程序中我應該使用什麼函數嗎?