9 Replies - 1457 Views - Last Post: 27 May 2007 - 10:50 PM Rate Topic: -----

#1 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

what is piping and how does it effect c++?

Posted 27 May 2007 - 04:58 AM

can anyone explain me what is piping in C++ programming?

for example
invoice.exe < purchase.dat > statement.txt

do i replace that where all the cin statements? i created a text file called purchase.dat with values:

gameboy 149.99 1
ipod nano 220.99 2
DVD-player5 60.95 1
rubics cube 8.85 5

so i.e. below is some coding tat is in working progress. i able to make it tat it prints price, cost, quantity, and product id. i have to use piping. where do i put this statement: invoice.exe < purchase.dat > statement.txt
in the code?

 
		   for (int i = 0; i <= maxitems; i++)
		   {
			   cout << "product: ";
			   getline(cin, productid);
			   cout << "quantity; ";
			   cin >> quantity;
			   cout << "price: ";
			   cin >> price;
		   
			   cost = quantity * price;
			   totalcost = totalcost + cost;	 
		   cout << setw(34) << productid << setw(10) << quantity << setw(10) << price << setw(12) << cost << endl;		   
		   }   
cout << "Total: " << totalcost << endl;

This post has been edited by noob2007: 27 May 2007 - 05:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: what is piping and how does it effect c++?

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 06:29 AM

piping in this sense is ususally meant that the result of one operation is used as the input into another. ex:
invoice.exe creates purchase information and outputs purchase.dat, which is then read in directly to some other application which produces statement.txt from it.
If you have ever used linux, piping is a common command line operation with grep to filter it's results.

My guess is you are supposed to create the .dat file, then have another application or method use it, parse it and generate the .txt file.
Was This Post Helpful? 0
  • +
  • -

#3 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 03:56 PM

View PostWilliam_Wilson, on 27 May, 2007 - 06:29 AM, said:

piping in this sense is ususally meant that the result of one operation is used as the input into another. ex:
invoice.exe creates purchase information and outputs purchase.dat, which is then read in directly to some other application which produces statement.txt from it.
If you have ever used linux, piping is a common command line operation with grep to filter it's results.

My guess is you are supposed to create the .dat file, then have another application or method use it, parse it and generate the .txt file.



so wat do i do in the code? or how do i make it work?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 06:21 PM

The syntax (in DOS/Windows)

command < InputFile > OutputFile

will set the standard input to InputFile and the standard output to OutputFile

This mean that cin and cout would be used ALMOST as if input and output were from/to the console. Everything that you send to cout will go to OutputFile, and every time you use cin it will be reading from InputFile.

Most of the time there are few changes that need to be made to a standard program to enable it to work this way. Generally prompts for input should be avoided (unless the input is echoed to the output file) since they clutter the output file.

Technically this is called redirection. The pipe operator | takes the standard output of one command/program and directs it to the standard input of another program. So for example

dir > dir.txt

will generate a text file with a directory listing in it.

dir | sort > dir.txt

will generate a directory listing and then sort the listing and save it in a file named dir.txt.

This "pipe" affect allows us to chain several programs together (very commonly done in UNIX utilities) to process information.
Was This Post Helpful? 0
  • +
  • -

#5 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 06:40 PM

View PostNickDMax, on 27 May, 2007 - 06:21 PM, said:

The syntax (in DOS/Windows)

command < InputFile > OutputFile

will set the standard input to InputFile and the standard output to OutputFile

This mean that cin and cout would be used ALMOST as if input and output were from/to the console. Everything that you send to cout will go to OutputFile, and every time you use cin it will be reading from InputFile.

Most of the time there are few changes that need to be made to a standard program to enable it to work this way. Generally prompts for input should be avoided (unless the input is echoed to the output file) since they clutter the output file.

Technically this is called redirection. The pipe operator | takes the standard output of one command/program and directs it to the standard input of another program. So for example

dir > dir.txt

will generate a text file with a directory listing in it.

dir | sort > dir.txt

will generate a directory listing and then sort the listing and save it in a file named dir.txt.

This "pipe" affect allows us to chain several programs together (very commonly done in UNIX utilities) to process information.


so i do go to start> run > type invoice.exe < purchase.dat > statement.txt? or do i put it in the program whre the cin statements are?
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 06:51 PM

well... I think yes.

Yes you would go to the command line and type the command:
invoice.exe < purchase.dat > statement.txt

if invoice.exe uses cin to get input, and cout to write output.
Was This Post Helpful? 0
  • +
  • -

#7 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 09:26 PM

View PostNickDMax, on 27 May, 2007 - 06:51 PM, said:

well... I think yes.

Yes you would go to the command line and type the command:
invoice.exe < purchase.dat > statement.txt

if invoice.exe uses cin to get input, and cout to write output.


cool it works but wen i enter invoice.exe < purchase.dat > statement.txt then press enter the statement write to a text file and tat normal but on the cmd screen it prints out [see below]:

C:\Documents and Settings\user > invoice.exe < purchase.dat > statement.txt
product: quantity: price: product: quantity: price: product: quantity: price: product: quantity: price:

is tat normal or wat? can u display it in the cmd

Posted Image

This post has been edited by noob2007: 27 May 2007 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 09:46 PM

Well it looks like you have some output going to the console anyway. Do you use anything other than cout to do your output? For example do you use cerr in your program? Or any of the C routines like puts() etc.

The output you see is not normal, and there does not need to be any output at all to the console.

If all of your input and output routines use cin and cout then this should not happen.
Was This Post Helpful? 0
  • +
  • -

#9 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 10:31 PM

View PostNickDMax, on 27 May, 2007 - 09:46 PM, said:

Well it looks like you have some output going to the console anyway. Do you use anything other than cout to do your output? For example do you use cerr in your program? Or any of the C routines like puts() etc.

The output you see is not normal, and there does not need to be any output at all to the console.

If all of your input and output routines use cin and cout then this should not happen.


alrite ill hav a go at the cerr statement. ok i was wrong there still a problem wen printing it to a txt file.
it printed:
ABC COMPANY INVOICE

							PRODUCT  Quantity  Price($)   Cost($)

						   gameboy	   149	  0.99	  147.51
								 1	   149	  0.99	  147.51
								 1	   149	  0.99	  147.51
								 1	   149	  0.99	  147.51
				 Total: $590.04
Press any key to continue . . . 



it suppose to print
gameboy 149.99 1
ipod nano 220.99 2
DVD-player5 60.95 1
rubics cube 8.85 5
jus for quick reference..headings is inlcuded in teh statement.txt but im jus sayin tat those items were not printed in the statement.txt 

heres my code:

		   for (int i = 0; i <= maxitems; i++)
		   {
			   cerr << "product: ";
			   cin >> productid;
			   cerr << "quantity; ";
			   cin >> quantity;
			   cerr << "price: ";
			   cin >> price;
		   
			   cost = quantity * price;
			   totalcost = totalcost + cost;	 
		   cout << setw(34) << productid << setw(10) << quantity << setw(10) << price << setw(12) << cost << endl;		   
		   } 
		   
		   cout << setw(25) << "Total: $" << totalcost << endl;	

This post has been edited by noob2007: 27 May 2007 - 10:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: what is piping and how does it effect c++?

Posted 27 May 2007 - 10:50 PM

OK nvm i figured out the problem. thanks nick.

but one question

wen i go to run, type in cmd, the default location is :\Documents and Settings\user. how do i open a directory lik on teh desktop becuase i dont kno how to

for example
how do i make it so tat it opens lik C:\Documents and Settings\user\Desktop\New Folder

This post has been edited by noob2007: 28 May 2007 - 01:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1