Good c++ Program


 
Thread Tools Search this Thread
Top Forums Programming Good c++ Program
# 1  
Old 08-24-2007
Good c++ Program

Hi Guru's
I wrote a shell script some days back.
i want to translate this script to a c++ program.I need help from you guys.I have never done any programming in c++ til now.

Code:
#!/usr/bin/ksh

inpFile=$1
outFile="$HOME/Output.txt"
errFile="$HOME/Error.txt"
dupFile="$HOME/Duplicate.txt"
tmpFile1=sample1.txt
tmpFile2=sample2.txt
sortFile=sortfile.txt

TOTAL_FILEDS=9
COUNT=1
PRINT_FLAG=0
LINE_NO=1
check_lines=0
rm -f $outFile $errFile $dupFile
sort $inpFile | uniq -d > $dupFile
#sort $inpFile | uniq -d | sed "s/$/& : Duplicate/g" >> $errFile
for line in $(cat $inpFile)
do
  	count_fields=$(echo $line | sed "s/00,*//g;s/,$//g" | wc -c)
	echo "count_fields is: $count_fields"
	cnt_flds1=$(echo $line | cut -d',' -f1-9 | sed "s/00,*//g;s/,$//g" | wc -c )
	echo "cnt_flds1 is : $cnt_flds1"
	cnt_flds2=$(echo $line | cut -d',' -f10-18 | sed "s/00,*//g;s/,$//g" | wc -c )
	echo "cnt_flds2 is : $cnt_flds2"
	cnt_flds3=$(echo $line | cut -d',' -f19-27 | sed "s/00,*//g;s/,$//g" | wc -c )
	echo "cnt_flds3 is : $cnt_flds3"
	if [[ $count_fields -eq 45 && $cnt_flds1 -eq 15 && $cnt_flds2 -eq 15 && $cnt_flds3 -eq 15 ]]
	then
            	PRINT_FLAG=0

		echo $line | cut -d',' -f1-9 > $sortFile
		echo $line | cut -d',' -f10-18 >> $sortFile
		echo $line | cut -d',' -f19-27 >> $sortFile

		while [[ $COUNT -le $TOTAL_FILEDS ]]
		do
                  	cut -d',' -f$COUNT $sortFile | grep -v 00 > $tmpFile1
			cut -d',' -f$COUNT $sortFile | grep -v 00 | sort > $tmpFile2

			diff $tmpFile1 $tmpFile2 > /dev/null
			if [[ $? -eq 1 || ! -s $tmpFile1 ]]
			then
                            	PRINT_FLAG=1
				break
fi
		COUNT=$((COUNT+1))
		done
		echo "Value of print flag is : $PRINT_FLAG"
		if [[ $PRINT_FLAG -eq 0 ]]
		then
			echo "$line : SUCCESS" >> $outFile
		else
			echo "$line : ERROR in $COUNT" >> $errFile
		fi
	else
		echo "$line : Out of Scope"  >> $errFile
	fi
	COUNT=1
	check_lines=$((check_lines+count_fields))
	if [[ $LINE_NO -eq 6 ]]
	then
		LINE_NO=0
		if [[ $check_lines -eq 270 ]]
		then
			echo " **** Success A Strip has 90 digits"
		else
			echo "----- Error,Check the above -----"
		fi
		check_lines=0
	fi
	LINE_NO=$((LINE_NO+1))
done
rm -f $tmpFile1 $tmpFile2 $sortFile
exit 0

The input file is----
Code:
00,11,21,00,00,55,00,73,83,07,00,22,39,43,00,66,00,00,00,15,00,00,44,58,67,00,87
02,14,24,30,00,00,00,70,00,00,00,00,32,47,50,60,74,00,04,16,27,38,00,00,00,00,89
00,00,00,00,42,52,61,71,86,01,00,29,33,00,59,00,75,00,03,19,00,00,45,00,68,00,88
00,13,20,31,00,00,64,76,00,05,00,00,35,41,56,00,00,81,00,00,28,00,00,57,65,79,90
06,10,00,36,40,00,00,00,80,00,12,25,00,00,51,62,72,00,09,00,00,37,49,00,69,00,84
00,17,23,00,46,53,00,77,00,08,00,00,34,00,54,00,78,82,00,18,26,00,48,00,63,00,85
00,14,23,39,00,00,60,00,87,02,00,28,00,41,50,00,75,00,03,15,00,00,44,00,68,00,90
00,16,25,35,00,53,00,71,00,08,00,00,36,40,00,62,00,88,00,00,27,37,00,58,64,78,00
05,10,00,00,48,52,00,73,00,00,00,24,31,00,57,66,00,81,07,17,00,00,49,00,00,79,85
00,00,20,33,00,00,63,70,80,06,12,00,34,42,56,00,00,00,00,00,29,00,00,59,69,74,86
04,13,00,32,43,00,65,00,00,00,18,22,00,00,55,00,72,84,09,00,00,38,47,00,67,00,89
01,11,21,00,00,51,00,76,00,00,00,26,30,45,54,00,00,82,00,19,00,00,46,00,61,77,83

Thanks

Last edited by vino; 08-24-2007 at 09:48 AM.. Reason: Added code tags.
# 2  
Old 08-24-2007
You might have to explain the logic again to the group, the giving the code wouldn't help. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Shell Programming and Scripting

Good Example on $|

I am trying to learn how to use the predefined var. I did read perlvar - perldoc.perl.org but like much of the perl docs, I just dont "see" the explanation. Can someone explain its usage to me. Id like to buffer printing on occasion. Thanks in advance. (1 Reply)
Discussion started by: popeye
1 Replies

3. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

4. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

5. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

6. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

7. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

8. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies
Login or Register to Ask a Question