Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

Hank Childs, University of Oregon April 17 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Guide To UNIX Using Linux Third Edition
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Homework Reading Programming Assignments
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Hank Childs, University of Oregon May 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
File Handling Spring 2013Programming and Data Structure1.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Input/output streams. Character Input / Output and Input Validation Input and output devices: keyboards, disk drives, mouse, monitors, printers, network.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Hank Childs, University of Oregon April 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
C is a high level language (HLL)
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
CSC Programming for Science Lecture 18: More Data Files.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Hank Childs, University of Oregon April 7 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Error handling I/O Man pages
Hank Childs, University of Oregon
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Hank Childs, University of Oregon
An Introduction to C Programming
File I/O.
CSC215 Lecture Input and Output.
Hank Childs, University of Oregon
CS111 Computer Programming
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
File I/O We are used to reading from and writing to the terminal:
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Fundamental of Programming (C)
Programming in C Input / Output.
I/O CS580U - Fall 2018.
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 6: file I/O, finish Unix

Announcements Projects – 4A assigned: due in class on Weds – 2B prompt available Saturday AM, due on Friday No class on Weds April 27 – likely a short YouTube replacement lecture

Outline Review File I/O Project 2B Redirection Pipes

Outline Review File I/O Project 2B Redirection Pipes

Makefile example: multiplier lib

Unix command: tar tar cvf 330.tar file1 file2 file3 – puts 3 files (file1, file2, file3) into a new file called 330.tar scp ssh ix tar xvf 330.tar ls file1 file2 file

Memory Errors Array bounds read Array bounds write

Memory Errors Free memory read / free memory write Vocabulary: “dangling pointer”: pointer that points to memory that has already been freed.

Memory Errors Freeing unallocated memory

Memory Errors Freeing non-heap memory

Memory Errors NULL pointer read / write NULL is never a valid location to read from or write to, and accessing them results in a “segmentation fault” – …. remember those memory segments?

Memory Errors Uninitialized memory read

Memory error in action

Project 4A Posted now You will practice debugging & using a debugger – There are 3 programs you need to debug In this case, “debug” means identify the bug – Does not mean fix the bug Can use gdb or lldb May want to run on ix Worksheet due in class next week

ASCII Character Set image source: granneman.com There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention

signed vs unsigned chars signed char (“char”): – valid values: -128 to 127 – size: 1 byte – used to represent characters with ASCII values -128 to -1 are not valid unsigned char: – valid values: 0 to 255 – size: 1 byte – used to represent data

character strings A character “string” is: – an array of type “char” – that is terminated by the NULL character Example: char str[12] = “hello world”; – str[11] = ‘\0’ (the compiler did this automatically) The C library has multiple functions for handling strings

Character strings example

memcpy I mostly use C++, and I still use memcpy all the time

if-then-else int val = (X < 2 ? X : 2);  if (X<2) { val = X; } else { val = 2; }

Outline Review File I/O Project 2B Redirection Pipes

File I/O: streams and file descriptors Two ways to access files: – File descriptors: Lower level interface to files and devices – Provides controls to specific devices Type: small integers (typically 20 total) – Streams: Higher level interface to files and devices – Provides uniform interface; easy to deal with, but less powerful Type: FILE * Streams are more portable, and more accessible to beginning programmers. (I teach streams here.) Streams are more portable, and more accessible to beginning programmers. (I teach streams here.)

File I/O Process for reading or writing – Open a file Tells Unix you intend to do file I/O Function returns a “FILE * – Used to identify the file from this point forward Checks to see if permissions are valid – Read from the file / write to the file – Close the file

Opening a file FILE *handle = fopen(filename, mode); Example: FILE *h = fopen(“/tmp/330”, “wb”); Note: #include Close when you are done with “fclose”

Reading / Writing

Example

fseek

ftell

We have everything we need to make a copy command… fopen fread fwrite fseek ftell Can we do this together as a class?

argc & argv two arguments to every C program argc: how many command line arguments argv: an array containing each of the arguments “./a.out hank childs”  argc == 3  argv[0] = “a.out”, argv[1] = “hank”, argv[2] = “childs”

Return values in shells $? is the return value of the last executed command

Printing to terminal and reading from terminal In Unix, printing to terminal and reading from terminal is done with file I/O Keyboard and screen are files in the file system! – (at least they were …)

Standard Streams Wikipedia: “preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution” Three standard streams: – stdin (standard input) – stdout (standard output) – stderr (standard error) What mechanisms in C allow you to access standard streams?

printf Print to stdout – printf(“hello world\n”); – printf(“Integers are like this %d\n”, 6); – printf(“Two floats: %f, %f”, 3.5, 7.0);

fprintf Just like printf, but to streams fprintf(stdout, “helloworld\n”); –  same as printf fprintf(stderr, “helloworld\n”); – prints to “standard error” fprintf(f_out, “helloworld\n”); – prints to the file pointed to by FILE *f_out.

buffering and printf Important: printf is buffered So: – printf puts string in buffer – other things happen – buffer is eventually printed But what about a crash? – printf puts string in buffer – other things happen … including a crash – buffer is never printed! Solutions: (1) fflush, (2) fprintf(stderr) always flushed

Outline Review File I/O Project 2B Redirection Pipes

Project 2B 0 1

Outline Review File I/O Project 2B Redirection Pipes

Unix shells allows you to manipulate standard streams. “>” redirect output of program to a file Example: – ls > output – echo “this is a file” > output2 – cat file1 file2 > file3

Unix shells allows you to manipulate standard streams. “<” redirect file to input of program Example: – python < myscript.py Note: python quits when it reads a special character called EOF (End of File) You can type this character by typing Ctrl-D This is why Python quits when you type Ctrl-D – (many other programs too)

Unix shells allows you to manipulate standard streams. “>>” concatenate output of program to end of existing file – (or create file if it doesn’t exist) Example: – echo “I am starting the file” > file1 – echo “I am adding to the file” >> file1 – cat file1 I am starting the file I am adding to the file

What’s happening here? ls is outputting its error messages to stderr

Redirecting stderr in a shell

Redirecting stderr to stdout Convenient when you want both to go to the same stream

Outline Review File I/O Project 2B Redirection Pipes

c functions: fork and pipe fork: duplicates current program into a separate instance – Two running programs! – Only differentiated by return value of fork (which is original and which is new) pipe: mechanism for connecting file descriptors between two forked programs Through fork and pipe, you can connect two running programs. One writes to a file descriptor, and the other reads the output from its file descript Only used on special occasions. (And one of those occasions is with the shell.) Only used on special occasions. (And one of those occasions is with the shell.)

pipes in Unix shells represented with “|” output of one program becomes input to another program

Very useful programs grep: keep lines that match pattern, discard lines that don’t match pattern

Very useful programs sed: replace pattern 1 with pattern 2 – sed s/pattern1/pattern2/g s means substitute g means “global” … every instance on the line sed is also available in “vi” :%s/pattern1/pattern2/g (% means all lines) :103,133s/p1/p2/g (lines ) sed is also available in “vi” :%s/pattern1/pattern2/g (% means all lines) :103,133s/p1/p2/g (lines )

Bonus topic: wildcards ‘*’ is a wildcard with unix shells ‘?’ is a wildcard that matches exactly one character

Other useful shell things ‘tab’: auto-complete esc=: show options for auto-complete Ctrl-A: go to beginning of line Ctrl-E: go to end of line Ctrl-R: search through history for command