UNIT 11 UNIX I/O Redirection

UNIT 11 UNIX I/O Redirection UNIT 11 UNIX I/O Redirection - Start

Added : 2018-02-23 Views :18K

Download Presentation

UNIT 11 UNIX I/O Redirection




Download Presentation - The PPT/PDF document "UNIT 11 UNIX I/O Redirection" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.



Presentations text content in UNIT 11 UNIX I/O Redirection

Slide1

UNIT 11

UNIX I/O Redirection

http://www.comp.nus.edu.sg/~cs1010/

Slide2

Unit 11: UNIX I/O Redirection

UNIX I/O Redirection

Unit11 - 2© NUS

Objective:

Learn how to use I/O redirection in UNIX to redirect input from a file and output to a file.

Slide3

Unit 11: UNIX I/O Redirection

UNIX I/O Redirection

Unit11 - 3© NUS

IntroductionInput Redirection

Output Redirection

Combining Input and Output Redirection

Slide4

1. Introduction

UNIX I/O Redirection

Unit11 - 4

© NUS

Recall in

Unit #4 Overview of C Programming

, it is mentioned that the default standard input stream (

stdin

) is the keyboard, and the default standard output stream (

stdout

) is the monitor.

In UNIX, you may run a program that normally reads input data interactively to read the input data

from a file

instead.

Likewise, you may write the output of a program

to a file

instead of printing it on the screen.

This is known as

input/output redirection

.

Note that this is an operating system (UNIX) feature and not a C feature.

Slide5

2. UNIX Input Redirection (1/3)

UNIX I/O Redirection

Unit11 - 5© NUS

Some programs read a lot of input data (

eg

: programs involving arrays), which makes it very inconvenient for users to key in that large amount of data interactively.

Instead, we may store the input data in a file, and let the program read the data from that file.

We may do it in 2 ways:

Read the file using

file processing functions

(

eg

:

fopen

()

,

fscanf

()

,

fprintf

()

) – these will be covered next time

Redirect

the input from the file instead of from

stdin

– we will do this for the moment

Slide6

2. UNIX Input Redirection (2/3)

UNIX I/O Redirection

Unit11 - 6© NUS

#include

<

stdio.h

>

int

main(

void

) {

int

num, sum =

0

;

printf(

"Enter integers, terminate with ctrl-d:

\n

"

);

while

(scanf(

"

%d

"

, &num) ==

1

) {

sum += num;

} printf("Sum = %d\n", sum); return 0;}

Unit11_Example.c

Running the program interactively:

$ a.outEnter ... ctrl-d:512-7023 User enters ctrl-d hereSum = 33

What does this mean?

Slide7

2. UNIX Input Redirection (3/3)

UNIX I/O Redirection

Unit11 - 7© NUS

Using an editor (eg: vim), create a text file to contain the input data. Let’s call the file

numbers

.

File

numbers

5

12

-7

0

23

Use the UNIX input redirection operator

<

to redirect input from the file

numbers

$

a.out

<

numbers

Enter ... ctrl-d:

Sum = 33

(This is how CodeCrunch runs your program. It redirects input from some file to feed your program.)

Slide8

3. UNIX Output Redirection (1/2)

UNIX I/O Redirection

Unit11 - 8© NUS

Instead of printing your output to the default

stdout

(monitor), you may redirect the output to a file as well.

Use the UNIX output redirection operator

>

.

$

a.out

>

outfile

5

12

-7

0

23

User enters ctrl-d here

Slide9

3. UNIX Output Redirection (2/2)

UNIX I/O Redirection

Unit11 - 9© NUS

The file

outfile

is created which captures

all

outputs of the program.

$

cat outfile

Enter integers, terminate with ctrl-d:

Sum = 33

Output redirection

>

fails if the specified output file already exists

If you want to append the output of a program to an existing file, you may use

>>

Slide10

4. Combining Input and Output Redirection

UNIX I/O Redirection

Unit11 - 10© NUS

You may combine both input and output redirection

$

a.out

<

numbers

>

outfile

Tip for lab exercises:

Using input redirection, you can download the given input files on the CS1010 website and run your program on these files.

Using output redirection, you may now generate your own output file and compare it with the expected output file provided on the CS1010 website.

Use the UNIX

diff

command to compare two files. Example:

diff file1 file2

If the two files compared are identical, no output will be generated by the

diff

command.

Read input from the file “numbers”

Save output to the file “

outfile

Slide11

Summary

UNIX I/O Redirection

Unit11 - 11© NUS

In this unit, you have learned about

Using UNIX input redirection

<

to redirect input from a file to a program

Using UNIX output redirection

>

to redirect output of a program to a file

Slide12

End of File

UNIX I/O Redirection

Unit11 - 12© NUS


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.
Youtube