Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to replace specific data in one file with the data in another file using c
following is my barcode label...

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



& the content in another file is as below

132424235
004342
L1000
DZ12
234235
234235



i want to replace field [PRTNUM] from first file with data 004342 in another file in this way i want all data inside [] is replaced by data from other file.
Posted
Updated 10-Jun-15 1:58am
v5
Comments
[no name] 10-Jun-15 7:30am    
Read line from original file
check to see if it the data you need to replace
if not write to temp file
if yes replace and write to temp file
delete original file
rename temp file.
done

1 solution

You can't necessarily just "replace data" in a file: if the data is the same length as it's replacement, you can open the file, seek to the position, write the new data, save the file, and close it.

But...if it isn't then you have to copy the input file into a new output file up to the point of the first replacement, write the new infor to the output file, skip the old information in the old file, copy the rest of the input file to the output, save the output and close them both, then delete the old file, and rename the new one.
 
Share this answer
 
Comments
CPallini 10-Jun-15 7:56am    
5.
Superdude Rahul 11-Jun-15 4:19am    
thanks but i'm using following program to find character between [,] for first file but then i want to replace it with the data from other the file
fPOut = fopen("final.txt", "r");

if(fPOut)

{

printf("The file is\n\n");

do


{

scanf("%c", &a[1]);
fgets(cString ,2000 ,fPOut);


char* cStart = strchr(cString, '[');
cStart++;
char* cEnd = strtok(cString, "]");
cEnd = '\0';
// fprintf(fpOut," %s\n",cStart);
printf("%s",cStart);
i++;


}

while(i
Superdude Rahul 11-Jun-15 4:23am    
is there any other good approach to parse the file please tell...
OriginalGriff 11-Jun-15 4:46am    
No, if you want to insert variable length data then you have to create a new file and copy up to the insertion point. There is no other "good solution" that isn't a heck of a lot more complicated!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900