See more articles about "C Language Programming "

Redirecting Ascribe and Achievement :: Ascribe & Achievement



 31 December 18:00   

    

A program that uses stdin and stdout can advance an operating-system affection alleged redirection. Redirection allows you to do the following:



    



        

  1. Output beatific to stdout can be beatific to a deejay book or the printer rather than

        

    to the screen.



  2.     

  3. Program ascribe from stdin can appear from a deejay book rather than from the

        

    keyboard.



  4.     



    

We don t cipher redirection into our programs; we specify it on the command band

    

if we run the program. In DOS, as in UNIX, the symbols for redirection are

    

< and >.



    

Bethink the first C program, HELLO.C? It acclimated the printf() library action

    

to affectation the bulletin Hello, apple on-screen. As we now know, printf() sends

    

achievement to stdout, so it can be redirected. If we access the program name at

    

the command-line prompt, chase it with the > attribute and the name of the

    

new destination:



    

hello > destination



    

Thus, if we access accost >prn, the program achievement goes to the printer instead of to the awning (prn is the DOS name for the printer absorbed to anchorage LPT1:). If we access accost >hello.txt, the achievement is placed in a deejay book with the name HELLO.TXT.
When we alter achievement to a deejay file, be careful. If the book already exists, the old archetype is deleted and replaced with the new file. If the book doesn t exist, it is created. If redirecting achievement to a file, we can aswell use the >> symbol. If the defined destination book already exists, the program achievement is added to the end of the file.



    

The redirection of ascribe and output.

    

1: /* Can be acclimated to authenticate redirection of stdin and stdout. */

    

2:

    

3: #include <stdio.h>

    

4:

    

5: main()

    

6: {

    

7: burn buf[80];

    

8:

    

9: gets(buf);

    

10: printf("The ascribe was: %s
", buf);

    

11: acknowledgment 0;

    

12: }



    

This program accepts a band of ascribe from stdin and then sends the band to

    

stdout, above-mentioned it with The ascribe was:. Afterwards accumulation and bond the program,

    

run it after redirection (assuming that the program is called TRIAL) by entering

    

Balloon at the command-line prompt. If we then access I am teaching myself C, the

    

program displays the afterward on-screen:



    

The ascribe was: I am teaching myself C


    

If we run the program by entering Balloon >test.txt and create the aforementioned entry, annihilation is displayed on-screen. Instead, a book called TEST.TXT is created on the disk. If we use the DOS Blazon (or an equivalent) command to affectation the capacity of the file:



    

type test.txt


    

We ll see that the book contains alone the band The ascribe was: I am teaching myself C. Similarly, if we had run the program by entering Balloon >prn, the achievement band would accept been printed on the printer (prn is a DOS command name for the printer).
Just for the account of it run the program again, this time redirecting achievement to TEST.TXT with the >> symbol. Instead of the book s accepting replaced, the new achievement is added to the end of TEST.TXT. Accord it a try ?



    

Redirecting Input



    

First we charge a antecedent file. Use the editor to make a book called INPUT.TXT that contains the individual band COMSATS Abbottabad. Now run the antecedent program by entering the afterward at the DOS prompt:



    

trial < INPUT.TXT


    

The program doesn t delay for you to create an access at the keyboard. Instead, it anon displays the afterward bulletin on-screen:



    

The ascribe was: COMSATS Abbottabad


    

The beck stdin was redirected to the deejay book INPUT.TXT, so the program s

    

alarm to gets() reads one band of argument from the book rather than the keyboard.



    

We can alter ascribe and achievement at the aforementioned time. Try active the program

    

with the afterward command to alter stdin to the book INPUT.TXT and alter

    

stdout to JUNK.TXT:



    

trial < INPUT.TXT > JUNK.TXT


    

Redirecting stdin and stdout can be advantageous in assertive situations. A allocation program, for example, could array either keyboard ascribe or the capacity of a deejay file. Likewise, a commitment account program could affectation addresses on-screen, forward them to the printer for commitment labels, or abode them in a book for some additional use.



    

When to Use fprintf()



    

The library action fprintf() is identical to printf(), except that we can specify the beck to which achievement is sent. The capital use of fprintf() involves deejay files. There are two additional uses.



    

Using stderr



    

One of C s predefined streams is stderr (standard error). A program s absurdity letters commonly are beatific to the beck stderr and not to stdout. Achievement to stdout can be redirected to a destination additional than the affectation screen. If stdout is redirected, the user ability not be acquainted of any absurdity letters the program sends to stdout. Clashing stdout, stderr can t be redirected and is consistently affiliated to the awning (at atomic in DOS--UNIX systems ability acquiesce redirection of stderr). By administering absurdity letters to stderr, we can be abiding the user consistently sees them. We do this with fprintf():



    

fprintf(stderr, "An absurdity has occurred.");


    

We can address a action to handle absurdity letters and then alarm the action if an absurdity occurs rather than calling fprintf():



    

error_message("An absurdity has occurred.");

    

void error_message(char *msg)

    

{

    

fprintf(stderr, msg);

    

}



    

By using our own action instead of anon calling fprintf(), we accommodate added adaptability (one of the advantages of structured programming). For example, in appropriate affairs we ability wish a program s absurdity letters to go to the printer or a deejay file. All we charge to do is adapt the error_message() action so that the achievement is beatific to the adapted destination.



    

Printer Achievement Beneath DOS



    

On a DOS or Windows system, we forward achievement to the printer by accessing the predefined beck stdprn. To use stdprn, you charge to about-face ANSI affinity off in your compiler. The program presents a simple example.



    

Sending achievement to the printer.

    

1: /* Demonstrates printer output. */

    

2:

    

3: #include <stdio.h>

    

4:

    

5: main()

    

6: {

    

7: float f = 2.0134;

    

8:

    

9: fprintf(stdprn, "
This bulletin is printed.
");

    

10: fprintf(stdprn, "And now some numbers:
");

    

11: fprintf(stdprn, "The aboveboard of %f is %f.", f, f*f);

    

12:

    

13: /* Forward a anatomy feed. */

    

14: fprintf(stdprn, "f");

    

15:

    

16: acknowledgment 0;

    

17: }

    

This bulletin is printed.

    

And now some numbers:

    

The aboveboard of 2.013400 is 4.053780.



    

This achievement is printed by the printer. It won t arise on-screen If DOS arrangement

    

has a printer affiliated to anchorage LPT1:, we can abridge and run this program.

    

It prints three curve on the page. Band 14 sends an "f" to the printer. f

    

is the escape arrangement for a anatomy feed, the command that causes the printer

    

to beforehand a page (or, in the case of a laser printer, to banish the accepted

    

page).



    

DOs and DONTs



    



        

  1. DO use fprintf() to make programs that can forward achievement to stdout, stderr,

        

    stdprn, or any additional stream.



  2.     

  3. DO use fprintf() with stderr to book absurdity letters to the screen.


  4.     

  5. DO yield advantage of the accepted input/output streams that C provides.

        



  6.     

  7. DO accept the aberration amid echoed and nonechoed input.


  8.     

  9. DO accept the aberration amid buffered and unbuffered ascribe


  10.     

  11. DO make functions such as error_message to create your cipher added structured

        

    and maintainable.



  12.     

  13. DO yield advantage of continued characters in your programs. If using continued

        

    characters, you should try to be constant with additional programs.



  14.     

  15. DO use the gets() and scanf() functions instead of the fgets() and fscanf()

        

    functions if you re using the accepted ascribe book (stdin) only.



  16.     

  17. DON T use stderr for purposes additional than press absurdity letters or warnings.

        



  18.     

  19. DON T anytime try to alter stderr.


  20.     

  21. DON T use non-ANSI accepted functions if portability is a affair


  22.     

  23. DON T rename or change the accepted streams unnecessarily.


  24.     

  25. DON T try to use an ascribe beck such as stdin for an achievement action such

        

    as fprintf().



  26.     

  27. DON T overlook to analysis the ascribe beck for added characters.


  28.     



    



 


 output, input, program, printer, error, fprintf, stdout, stderr, screen, stdin, redirection, message, messages, function, stream, command, stdprn, trial, redirect, redirecting, standard, redirected, following, entering, printed, display, programs, keyboard, destination, printf, sends, functions, characters, streams, prompt, symbol, system, named, teaching, create, myself, example, , error messages, disk file, input was, stdout can, test txt, stdin and, input and, fprintf stdprn, error message, input txt, teaching myself, command line, > symbol, entering trial, < stdio h>, file already exists, command line prompt, file rather than, input from stdin,

Share Redirecting Ascribe and Achievement :: Ascribe & Achievement:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Unions & Structures
What is a Union?/h1PIf we are accepting the beneath anamnesis to use in our program, for archetype 64K, we can use a individual anamnesis area for added than one capricious this is alleged union. You can use the unios in the followig locations./Pp

Final Words - Ascribe & Achievement in C :: Ascribe & Achievement
PThis tutorial accomplished you about how C uses streams, alleviative all ascribe and achievement as a arrangement of bytes. You aswell learned that C has 5 predefined streams:/PPRE stdin The keyboard stdout The screen stde

What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams

Unions & Structures
What is a Union?/h1PIf we are accepting the beneath anamnesis to use in our program, for archetype 64K, we can use a individual anamnesis area for added than one capricious this is alleged union. You can use the unios in the followig locations./Pp

What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams

Unions & Structures
What is a Union?/h1PIf we are accepting the beneath anamnesis to use in our program, for archetype 64K, we can use a individual anamnesis area for added than one capricious this is alleged union. You can use the unios in the followig locations./Pp

What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams

What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams

What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams

What Is a Stream? :: Ascribe & Achievement
PA beck is a arrangement of characters. Added exactly, it is a arrangement of bytes of data. A arrangement of bytes abounding into a program is an ascribe stream; a arrangement of bytes abounding out of a program is an achievement stream. By absorption on streams