Your script is working fine. I have checked with some input file which containing more than 2 sprintf(buf,"%s", sourcestring) line also.