C program for file handling

June 27, 2009 at 05:37:32
Specs: Windows XP
Hi,
I want to write a C code, which should read a input text file and after doing some operation needs to write out a text file. Need to read lines that starts with letter A from some part of the inputfile and get 3 values from each line in the order. Also, needs to read the lines that starts with word DD and get 1 value from each line in the output file. Below is the input & output file format.

Input file is below
*caxxxxxx/
*ghhhgahh
BSRP,2
BSRP,45
BSRP,67
A, velo, 12.4567, 13.8745, 45.6789
A, velo, 45.2222, 1.3453, 0.0897
A, velo, 0.7867, 1567.4534, 122.5676
DD, ASD, 2345.0099
DD, ASD,342.8970
DD, ASD,5542.8945

And ouput file should look like,
12.4567 13.8745 45.6789 2345.0099
45.2222 1.3453 0.0897 342.8970
0.7867 567.4534 122.5676 5542.8945

.ie, 3 values from lines that starts with letter A and 1 value from the lines that starts with DD. The Number of lines that starts with A and DD are same.

GREATLY APPRECIATE IF SOME BODY CAN SHARE A SAMPLE PROGRAM or give a start on how I can proceed.
Thanks


See More: C program for file handling

Report •


#1
June 27, 2009 at 05:57:24
i will show you an alternative that is easier, if you have gawk for windows(see my sig)
BEGIN{	OFS=FS="," }
$1=="A"{
	$1=$2=""
	gsub(/^,,/,"")
	s[++d]=sprintf ("%s," ,$0	)
}
$1=="DD"{	print s[++e]" "$NF	}

save the above as myscript.awk and on command line

C:\test>gawk -f myscript.awk file.txt
 12.4567, 13.8745, 45.6789,  2345.0099
 45.2222, 1.3453, 0.0897, 342.8970
 0.7867, 1567.4534, 122.5676, 5542.8945

GNU win32 packages | Gawk


Report •
Related Solutions


Ask Question