Lecture 8: Structs & File I/O

Similar documents
Lecture 7: file I/O, more Unix

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Computer Programming: Skills & Concepts (CP) Files in C

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

UNIX input and output

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Process Management! Goals of this Lecture!

Standard C Library Functions

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CS 261 Fall Mike Lam, Professor. Structs and I/O

CSC209 Review. Yeah! We made it!

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSI 402 Lecture 2 Working with Files (Text and Binary)

Input / Output Functions

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

CS240: Programming in C

25.2 Opening and Closing a File

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 14: more class, C++ streams

File Access. FILE * fopen(const char *name, const char * mode);

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

C Basics And Concepts Input And Output

ENG120. Misc. Topics

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Welcome! COMP s1. Programming Fundamentals

fopen() fclose() fgetc() fputc() fread() fwrite()

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CAAM 420 Notes Chapter 2: The C Programming Language

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

2009 S2 COMP File Operations

Intermediate Programming, Spring 2017*

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

UNIT IV-2. The I/O library functions can be classified into two broad categories:

File Handling in C. EECS 2031 Fall October 27, 2014

Creating a Shell or Command Interperter Program CSCI411 Lab

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

C programming basics T3-1 -

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Syntax and Variables

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

Lecture 23: System-Level I/O

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

Input/Output and the Operating Systems

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

BLM2031 Structured Programming. Zeyneb KURT

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

High Performance Computing

Inter-Process Communications (IPC)

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

UNIX System Programming

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

Binghamton University. CS-220 Spring Includes & Streams

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Bash command shell language interpreter

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CSE 333 SECTION 3. POSIX I/O Functions

DATA STRUCTURES USING C

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

Lecture 03 Bits, Bytes and Data Types

structs as arguments

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

C: How to Program. Week /Mar/05

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

CSE 410: Systems Programming

Programming in C. What is C?... What is C?

System Software Experiment 1 Lecture 7

C File Processing: One-Page Summary

Unix Processes. What is a Process?

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS240: Programming in C

C Language, Token, Keywords, Constant, variable

Inter-Process Communication

All the scoring jobs will be done by script

C PROGRAMMING. Characters and Strings File Processing Exercise

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Transcription:

....... \ \ \ / / / / \ \ \ \ / \ / \ \ \ V /,----' / ^ \ \.--..--. / ^ \ `--- ----` / ^ \. ` > < / /_\ \. ` / /_\ \ / /_\ \ `--' \ /. \ `----. / \ \ '--' '--' / \ / \ \ / \ / / \ \ (_ ) \ (_ ) / / \ \ \ / / / / \ \ / / \ \....... / _ \ / _ \ / (----`--- ----` _),----'`--- ----` _) (----` \ \ / / \ \.----) \ \----. `--' `----. `--' \ \----..----) / _ `. \ / \ \ / _ `. / Lecture 8: Structs & File I/O October 18, 2018 Hank Childs, University of Oregon

Let s Grade 2D! Might be late: Katharine B. Dylan C. Yuchen G. Alyssa H. Kevin L.

Let s talk about 2C! The project wasn t hard but learning all the stuff surrounding it is!!! If you spent ~10 hours working on it, don t worry. You are building your skills and it will pay off big time.

2E: Extend 2C For Error Checking

Hank s OH Monday: 1030-1130 Thursday: 1230-130 Everything should be normal until the week of Nov 5th

Structs, typedef, union

Simple Data Types float double int char unsigned char All of these are simple data types

Structs: a complex data type Structs: mechanism provided by C programming language to define a group of variables Variables must be grouped together in contiguous memory Also makes accessing variables easier they are all part of the same grouping (the struct)

struct syntax C keyword struct means struct definition is coming semi-colon!!! Declaring an instance This struct contains 6 doubles, meaning it is 48 bytes. accesses data members for a struct

Nested structs accesses dir part of Ray accesses directionz part of Direction (part of Ray)

typedef typedef: tell compiler you want to define a new type saves you from having to type struct every time you declare a struct.

Other uses for typedef Declare a new type for code clarity typedef int MilesPerHour; Makes a new type called MilesPerHour. MilesPerHour works exactly like an int. Also used for enums & unions same trick as for structs typedef saves you a word Note: enums discussed in lab, unions discussed next

So important: struct data member access is different with pointers Pointers: use -> Instances (i.e., not pointers): use.

Unions Union: special data type store many different memory types in one memory location When dealing with this union, you can treat it as a float, as an int, or as 4 characters. This data structure has 4 bytes

Unions Why are unions useful?

Unions Example

Unions Example

File I/O

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.)

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 ); Close when you are done with fclose Note: #include <stdio.h>

Reading / Writing

Example

File Position Indicator File position indicator: the current location in the file If I read one byte, the one byte you get is where the file position indicator is pointing. And the file position indicator updates to point at the next byte But it can be changed

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?

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

2F

Streams in Unix

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

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.)

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 103-133)

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