Welcome! COMP s1. Programming Fundamentals

Similar documents
Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

COMP s1 Lecture 1

Lecture 8: Structs & File I/O

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

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

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

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Standard File Pointers

Lecture 7: file I/O, more Unix

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

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

Standard C Library Functions

Inter-Process Communication

CSE 303: Concepts and Tools for Software Development

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

CpSc 1111 Lab 4 Formatting and Flow Control

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

C for C++ Programmers

C Introduction Lesson Outline

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

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

CS4023 Week06 Lab Exercise

COMP1511 revision lecture

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

High Performance Computing

CSE 12 Spring 2018 Week One, Lecture Two

2009 S2 COMP File Operations

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

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

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

CS1003: Intro to CS, Summer 2008

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

Continued from previous lecture

COMP26120: Algorithms and Imperative Programming

Contents. A Review of C language. Visual C Visual C++ 6.0

Semester 2, 2018: Lab 1

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Pipelines, Forks, and Shell

Algorithms, Data Structures, and Problem Solving

The Design of C: A Rational Reconstruction: Part 2

Input / Output Functions

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Pointers and File Handling

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

CpSc 1111 Lab 5 Formatting and Flow Control

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

EL2310 Scientific Programming

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Lecture 14: more class, C++ streams

A Quick Look at C for C++ Programmers

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

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

1 SAT-DANCE-HEULE INTRO 1

Physics 306 Computing Lab 1: Hello, World!

Computer Programming: Skills & Concepts (CP) Variables and ints

Goals of this Lecture

Debugging! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 5!

Figure 1 Ring Structures

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

Introduction to Computing Lecture 03: Basic input / output operations

Programming & Data Structure

CS201 Lecture 2 GDB, The C Library

Introduction to Programming. Lecture 2: Introduction to C

Hacking C Code - Local Machine

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Process Management! Goals of this Lecture!

CSE 12 Spring 2016 Week One, Lecture Two

CS61 Lecture II: Data Representation! with Ruth Fong, Stephen Turban and Evan Gastman! Abstract Machines vs. Real Machines!

Programming and Data Structures Prof. N. S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

8. Characters, Strings and Files

25.2 Opening and Closing a File

If Statements, For Loops, Functions

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

C++ basics Getting started with, and Data Types.

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

Fundamentals of Programming Session 4

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

C programming basics T3-1 -

ECE 435 Network Engineering Lecture 2

CSS Crash Course for Fearless Bloggers by Gill Andrews

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Transcription:

Welcome! COMP1511 18s1 Programming Fundamentals

COMP1511 18s1 Lecture 11 1 Files, Memory Andrew Bennett <andrew.bennett@unsw.edu.au>

Overview 2 after this lecture, you should be able to have a basic understanding of the different types of i/o in C read and write files using C i/o functions and file redirection feel more comfortable working with argc/argv have a basic understanding of memory in C (note: you shouldn t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember: programming is like learning any other language, it takes consistent and regular practice.)

Admin 3 Don t panic! assignment 1 due YESTERDAY nice work :) week 5 weekly test due thursday don t be scared! lab marks released post in class forum email your tutor don t forget about help sessions! see course website for details

introducing: i/o 4 (input/output)

input/output? 5 i/o (input/output) various ways our programs can take input and give output

input/output? 6 you ve already seen several ways printf, scanf, getchar, putchar, fgets,.

input/output? 7 these all work with stdin and stdout (standard input and standard output) stdin scanf, getchar, fgets stdout printf, putchar

Other i/o 8 stdin and stdout go to the terminal but there are other options stderr (standard error) files (e.g. input.txt )

stderr 9 still goes to the terminal semantically different to stdout used to print errors can be redirected separately to stdout

stderr 1 can access with fprintf fprintf(stderr, "Uh oh, something went wrong!\n");

fprintf 11 just like printf, but works with files // stdout is a "file descriptor" fprintf(stdout, "Hello, world!\n"); // stderr is also a "file descriptor" fprintf(stderr, "Uh oh, something went wrong!\n"); // can also work with real files FILE\* output_file = fopen("output.txt", "w"); fprintf(output_file, "Hello!\n");

Files in C 12 we can work with files in C printing content to a file (just like printf) scanning content from a file (just like scanf)

Files in C 13 we saw the syntax earlier: // opens a file called "output.txt" in "writing" mode FILE\* output_file = fopen("output.txt", "w"); // prints "Hello!" to the file fprintf(output_file, "Hello!\n");

file redirection 14 we can also get the terminal to handle the file input/output for us // in a file "blah.c" printf("hello from a normal printf\n"); dcc -o blah blah.c./blah > output.txt $ cat output.txt Hello from a normal printf

string.h 15 #include <string.h> contains lots of functions to work with strings man 3 string note: many of these you can (and should, for practice) write yourself e.g.: int strlen(char *string) takes a string, returns the length. how would you write it yourself?

well 16 let s try it out!

https://www.youtube.com/watch?v=k4kkvyxxliy 17

up next: memory 18

Memory 19 effectively a GIANT array (4 GB big) everything is stored in memory somewhere variables are stored in memory the code for your program is stored in memory the code for library functions you use is stored in memory (printf, scanf, ) since everything is stored in memory, it has an address (similar to your home address)

Memory Addresses 2 since everything is stored somewhere in memory, everything has an address. we can get the address with & ( address of ) we print memory addresses in hexidecimal (why?)

Memory Layout 21 different things are stored in different places variables are stored on the stack dynamic memory (more on that next week) is stored on the heap

libraries & x magic your program ~x6 ~x1 the HEAP the STACK xbfffffff more magic xffffffff

6 1 r ) m e p w O n g g B e e w h b g " m 4 y F r a C F M o a h e a p r P E " y s ( e s d h a o d t E l F e m t! o ( e o y u F F r o b e e R s i G F a S b E d r f g m i r F g! t H K b x Y U t a o r! ( A F o w r w a ) d e o R x O A T " " x R a d n r s s R re ns F F r f o r h FF t w m o s l t H r P A m e F o F p x x g e x F o note: some people draw memory upside down (xffffffff at the top) keep an eye out for which way up any given diagram is

up next: references 22

address of 23 from earlier: we can get the address with & ( address of ) int number = 1; printf("the address of number is: %p\n", &number); we sometimes call this a reference i.e. &number is a reference to the variable number

address of 24 when we have the address of something, we know how to get to it int number = 1; printf("the address of number is: %p\n", &number); // might print something like: // The address of number is: xffd53f5

going to the address of 25 when we have the address of something, we know how to get to it we use the * operator for this: dereference ( de-reference go to the reference, to get the value there) int number = 1; printf("the address of number is: %p\n", &number); // might print something like: // The address of number is: xffd53f5 // we can print out the value at that address: printf("the value at address %p is %d\n", xffd53f5, *xffd53f5); // should print out "1"

going to the address of 26 we could do this directly in the printf:

int number = 1; printf("the address of number is: %p\n", &number); // might print something like: // The address of number is: xffd53f5 printf("the value at address %p is %d\n", &number, *(&number)); // should print out something like // The value at address xffd53f5 is 1 // we can print out the value at that address: printf("the value at address %p is %d\n", xffd53f5, *xffd53f5); // should print out something like // The value at address xffd53f5 is 1

storing the address of 27 but Andrew, why would we do that when we could just print out number directly? exactly. addresses are most useful for telling things far away (i.e. other functions) about the address of something in your function. we have a special type for storing the addresses of values int number = 1; int *number_address = &number; printf("the address of number is: %p\n", &number); printf("the value at address %p is %d\n", &number, *(&number)); printf("the value at address %p is %d\n", number_address *number_address);

we call these pointers because they point to the variable whose address they store

storing the address of 28 pointer syntax: [type] *[some_name] = &[something]; e.g. int *my_pointer = &my_variable; importantly: the value of the pointer is the address of the variable it points to

storing the address of 29 confused? that s okay. let s try it out!