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

Size: px
Start display at page:

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

Transcription

1 Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017

2 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 2/33 Contents About this Slide Set I/O redirection with the shell stdin, stdout, and stderr The void * type Binary file I/O in C

3 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 3/33 Outline of Slide Set 8 About this Slide Set I/O redirection with the shell stdin, stdout, and stderr The void * type Binary file I/O in C

4 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 4/33 About this Slide Set The next two sections of this Slide Set provide material on text file operations with the standard C library, beyond what was covered in Slide Set 7. The remaining sections of this Set have to with binary file operations with the standard C library.

5 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 5/33 Outline of Slide Set 8 About this Slide Set I/O redirection with the shell stdin, stdout, and stderr The void * type Binary file I/O in C

6 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 6/33 I/O redirection with the shell Slide Set 7 showed the process of getting the name of a file, opening a file, reading or writing text to or from a file, and closing the file. For a program that reads from no more than one input file, and writes to no more than one output file, the shell offers a simpler method for users to specify names of files, using a technique called I/O redirection. Let s suppose in Cygwin there s an executable called prog.exe in the working directory. The next slide shows some example Cygwin Terminal commands and their effects...

7 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 7/33./prog.exe no redirection here input from keyboard output to terminal window./prog.exe < foo.txt input not from keyboard but from file foo.txt output to terminal window./prog.exe > bar.txt input from keyboard output not to terminal but to file bar.txt./prog.exe < baz.txt > quux.txt input from file baz.txt output to file quux.txt

8 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 8/33 That was just a quick introduction to redirection Redirection works very well with Cygwin on Microsoft Windows and terminal windows with macos and Linux. It also works, somewhat less well, using Command Prompt windows on Microsoft Windows. There s a lot more to redirection than what you ve just seen, but there s no time for more detail right now in ENCM 339. A good thing to look up, if you have time, is use of the (pipe) symbol, which redirects the output of one program to be the input of another program.

9 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 9/33 Outline of Slide Set 8 About this Slide Set I/O redirection with the shell stdin, stdout, and stderr The void * type Binary file I/O in C

10 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 10/33 stdin, stdout, and stderr stdin, stdout and stderr are declared in <stdio.h>, and can be thought of as constants of type FILE*. They correspond to input or output streams opened before main starts, and closed when a program terminates.

11 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 11/33 stdin stdin is sometimes called standard input. By default, it comes from keyboard input, but it can come from a file or elsewhere when redirection is used. For example, this function call... n = fscanf(stdin, "%d", &i);... has the same effect as... n = scanf("%d", &i);

12 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 12/33 stdout stdout is sometimes called standard output. By default, it goes to terminal output, but it can go to a file or elsewhere when redirection is used. For example, this function call... fprintf(stdout, "%g", x);... has the same effect as... printf("%g", x);

13 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 13/33 stderr stderr is sometimes called standard error, and is the preferred channel for error messages. As with stdout, text sent to stderr appears by default in the terminal window. However, text sent to stderr will appear in the terminal window even if stdout has been redirected. This is useful, because error messages are often a very different kind of output from normal output.

14 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 14/33 Example separation of stdout and stderr Let s consider a tiny example, too simple to be really practical. A program is supposed to read text representations of integers, separated by whitespace, and report as output the count, average, maximum, and minumum of those integers. Input files could be valid or not, for example... good.txt bad.txt I don t think so! 33

15 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 15/33 Suppose the name of the executable is a.exe. Referring to the example input files on the previous slide, it s fine if the command./a.exe < good.txt > report1.txt silently writes all its output to a file, but it s a possible problem if the command./a.exe < bad.txt > report2.txt silently writes error messages to report2.txt when it might be much more helpful to see the error messages in a terminal window. The program on the next two slides shows how to separate normal output to stdout and error-message output to stderr.

16 #include <stdio.h> #include <stdlib.h> #include <limits.h> // has min and max values for types int main(void) { int count = 0, max = INT_MIN, min = INT_MAX, nscan, item; double sum = 0.0; while (1) { nscan = scanf("%d", &item); if (nscan!= 1) break; count++; sum += item; if (item > max) max = item; if (item < min) min = item; }

17 } if (nscan == 0) { fprintf(stderr, "Unexpected character in input.\n"); exit(1); } if (nscan == EOF &&!feof(stdin)) { fprintf(stderr, "Input failed before end-of-file.\n"); exit(1); } if (count > 0) { printf("%d numbers read, average value %g.\n", count, sum / count); printf("maximum %d, minimum %d.\n", max, min); } else printf("input stream was nothing but whitespace.\n"); return 0;

18 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 18/33 Outline of Slide Set 8 About this Slide Set I/O redirection with the shell stdin, stdout, and stderr The void * type Binary file I/O in C

19 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 19/33 The void * type It would be reasonable to guess that the type void * is used to indicate pointer-to-nothing, but that s incorrect. In fact, void * means pointer to any kind of data. A variable or function parameter of this type is typically used to hold the address of a block of bytes that could be used to hold data of many different types. It s important to note that the void * type is very different from the null pointer value! A null pointer value definitely does not point to usable data.

20 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 20/33 Here are two of several important uses for variables and parameters of void * type... base address of a block of bytes to be copied to or from the file system with binary file I/O functions fwrite and fread we re about to look at that; base address of a dynamically allocated block of bytes, managed using library functions such as malloc and free that s the subject of a future slide set.

21 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 21/33 Outline of Slide Set 8 About this Slide Set I/O redirection with the shell stdin, stdout, and stderr The void * type Binary file I/O in C

22 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 22/33 Binary file I/O in C Files, in most modern operating systems, are sequences of 8-bit bytes. Text file is the name given to any kind of file in which the bytes are used to represent a sequence of characters. Binary file is the name given to any kind of file that is not a text file some, most, or all of the bytes in the file are used to represent things that are not characters. Binary file was a somewhat odd choice of name text files and binary files are both essentially sequences of 0 s and 1 s! In ENCM 339, we ll look briefly at the basics of doing binary file I/O with the C library.

23 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 23/33 An int written to a text file... Before we look at binary file operations, it s good to think about what happens when a number is written to a text file. On the next slide there s a code fragment and a depiction of the first 7 bytes of the file the code fragment creates...

24 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 24/33 int i = ; FILE *fp = fopen("f.txt", "w"); fprintf(fp, "%d\n", i); ASCII 9 ASCII 8 ASCII 7 ASCII 6 ASCII 5 ASCII 4 ASCII \n Assuming a 4-byte (so, 32-bit) int type, the bits of the variable i will be That 4-byte sequence does not look in any obvious way like the 6-byte representation of the number that gets written to the file. Let s make a few notes about that.

25 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 25/33 General ideas about binary file I/O Binary file I/O operations with standard libraries for languages such as C, C++, Java, Python, etc., look very different from one language to the next. But there is an essential unifying concept: Binary file output is a simple copy of a sequence of bytes from program memory to the same number of bytes in a file. Binary file input is a simple copy of a sequence of bytes from a file to the same number of bytes in program memory.

26 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 26/33 C details: Use of fopen for binary file output The fopen function can be used to open binary files as well as text files. Here s a review of the fopen prototype from <stdio.h>... FILE *fopen(const char *path, const char *mode); path is used for the name of the file. Mode "wb" is like "w", but for binary files. Mode "ab" can be used to append data to the end of an existing binary file.

27 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 27/33 C details: Use of fwrite for binary file output Here s another prototype from <stdio.h>... size_t fwrite(const void *ptr, size_t el_size, size_t el_count, FILE *stream); Let s make notes about parameters, what the function does, and what the return value says.

28 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 28/33 C binary file output example int i = ; FILE *fp = fopen("f.dat", "wb"); fwrite((void *) &i, sizeof(int), 1, fp); Let s make some notes about the arguments used in the call to fwrite. On a PC, Mac, and many other platforms, the first 4 bytes of f.dat would contain these bits: least-significant byte of most-significant byte of

29 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 29/33 Let s review the examples from slides 24 and 28 on the same slide. First, text file output from slide 24: int i = ; FILE *fp = fopen("f.txt", "w"); fprintf(fp, "%d\n", i); Then, binary file output from slide 28: int i = ; FILE *fp = fopen("f.dat", "wb"); fwrite((void *) &i, sizeof(int), 1, fp); ASCII 9 ASCII 8 ASCII 7 ASCII 6 ASCII 5 ASCII 4 ASCII \n

30 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 30/33 C details: Use of fopen for binary file input The fopen prototype from <stdio.h>... FILE *fopen(const char *path, const char *mode); Use mode "r" to open a text file for input. Use mode "rb" to open a binary file for input.

31 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 31/33 C details: Use of fread for binary file input Here s yet another prototype from <stdio.h>... size_t fread(void *ptr, size_t el_size, size_t el_count, FILE *stream); Let s make notes about parameters, what the function does, and what the return value says.

32 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 32/33 C binary file input example Let s assume that before the following code fragment has been reached, file g.dat exists, and size has some meaningful value... size_t count; double arr[5]; FILE *fp = fopen("g.dat", "rb"); count = fread((void *) &d, sizeof(double), 5, fp); if (count!= 5) { // Couldn t fill array -- handle error. } What will happen if everything goes right? What are some things that can go wrong?

33 ENCM 339 Fall 2017 Section 01 Slide Set 8 slide 33/33 Concluding remarks about binary files In ENCM 339, you ve been given just a brief introduction to binary file operations. To write production code that works with binary files you need to do careful research in two areas: exact specifications for all the binary file formats your program will read or write; number formats and memory organization rules for all the platforms your program is expected to run on. For well-known binary file formats, you can often find libraries to help with task of reading and writing files. Prefer text files to binary files, unless you must use binary files. Text files are easier to work with, and defects in text file I/O are easier to diagnose than defects in binary file I/O.

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSC209H Lecture 3. Dan Zingaro. January 21, 2015 CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without

More information

25.2 Opening and Closing a File

25.2 Opening and Closing a File Lecture 32 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 32: Dynamically Allocated Arrays 26-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor:

More information

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

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 3 slide 2/46

More information

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

More information

M.CS201 Programming language

M.CS201 Programming language Power Engineering School M.CS201 Programming language Lecture 16 Lecturer: Prof. Dr. T.Uranchimeg Agenda Opening a File Errors with open files Writing and Reading File Data Formatted File Input Direct

More information

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

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 1 slide 2/43

More information

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 9 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2018 ENCM 335 Fall 2018 Slide Set 9 slide 2/32

More information

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman page 1 of 6 University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman Fall 2018 MIDTERM TEST Thursday, November 1 6:30pm to 8:30pm Please do not write your

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 INPUT/OUTPUT INPUT AND OUTPUT Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or

More information

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

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

More information

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

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by

More information

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline. CMPE-013/L Outline File Processing File I/O Gabriel Hugh Elkaim Winter 2014 Files and Streams Open and Close Files Read and Write Sequential Files Read and Write Random Access Files Read and Write Random

More information

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 4 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 4 slide

More information

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016 The University of Calgary ENCM 339 Programming Fundamentals Fall 2016 Instructors: S. Norman, and M. Moussavi Wednesday, November 2 7:00 to 9:00 PM The First Letter of your Last Name:! Please Print your

More information

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman page 1 of 6 University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman Fall 2017 MIDTERM TEST Wednesday, November 1 7:00pm to 9:00pm This

More information

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

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 6 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 13 si 14: Unix interface for working with files. Cristina Nita-Rotaru Lecture 13/Fall 2013 1 Working with Files (I/O) File system: specifies how the information is organized

More information

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

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 4 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

Input/Output and the Operating Systems

Input/Output and the Operating Systems Input/Output and the Operating Systems Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1 I/O Functions Formatted I/O printf( ) and scanf( ) fprintf( ) and fscanf( ) sprintf( ) and sscanf( ) int printf(const char*

More information

Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types Lecture 03 Bits, Bytes and Data Types Computer Languages A computer language is a language that is used to communicate with a machine. Like all languages, computer languages have syntax (form) and semantics

More information

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions page 1 of 5 ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions Steve Norman Department of Electrical & Computer Engineering University of Calgary October 2018 Lab instructions and

More information

ENCM 339 Fall 2017 Tutorial for Week 8

ENCM 339 Fall 2017 Tutorial for Week 8 ENCM 339 Fall 2017 Tutorial for Week 8 for section T01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 2 November, 2017 ENCM 339 T01 Tutorial

More information

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

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL. Files Files enable permanent storage of information C performs all input and output, including disk files, by means of streams Stream oriented data files are divided into two categories Formatted data

More information

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

CSI 402 Lecture 2 Working with Files (Text and Binary) CSI 402 Lecture 2 Working with Files (Text and Binary) 1 / 30 AQuickReviewofStandardI/O Recall that #include allows use of printf and scanf functions Example: int i; scanf("%d", &i); printf("value

More information

ENCM 335 Fall 2018 Tutorial for Week 13

ENCM 335 Fall 2018 Tutorial for Week 13 ENCM 335 Fall 2018 Tutorial for Week 13 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 06 December, 2018 ENCM 335 Tutorial 06 Dec 2018 slide

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Lecture 9: File Processing. Quazi Rahman

Lecture 9: File Processing. Quazi Rahman 60-141 Lecture 9: File Processing Quazi Rahman 1 Outlines Files Data Hierarchy File Operations Types of File Accessing Files 2 FILES Storage of data in variables, arrays or in any other data structures,

More information

Stream Model of I/O. Basic I/O in C

Stream Model of I/O. Basic I/O in C Stream Model of I/O 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 15: Unix interface: low-level interface Cristina Nita-Rotaru Lecture 15/Fall 2013 1 Streams Recap Higher-level interface, layered on top of the primitive file descriptor

More information

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

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch. 1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions

More information

CSC 270 Survey of Programming Languages. Input and Output

CSC 270 Survey of Programming Languages. Input and Output CSC 270 Survey of Programming Languages C Lecture 8 Input and Output Input and Output C supports 2 different I/O libraries: buffered (higher level functions supported by ANSI standards) unbuffered (lower-level

More information

Slide Set 15 (Complete)

Slide Set 15 (Complete) Slide Set 15 (Complete) for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2017 ENCM 339 Fall 2017

More information

structs as arguments

structs as arguments Structs A collection of related data items struct record { char name[maxname]; int count; ; /* The semicolon is important! It terminates the declaration. */ struct record rec1; /*allocates space for the

More information

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions. UNIT-V Unit-5 File Streams Formatted I/O Preprocessor Directives Printf Scanf A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage

More information

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection 1 Files 1.1 File functions Opening Files : The function fopen opens a file and returns a FILE pointer. FILE *fopen( const char * filename, const char * mode ); The allowed modes for fopen are as follows

More information

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

Computer Programming: Skills & Concepts (CP) Files in C CP 20 slide 1 Tuesday 21 November 2017 Computer Programming: Skills & Concepts (CP) Files in C Julian Bradfield Tuesday 21 November 2017 Today s lecture Character oriented I/O (revision) Files and streams

More information

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

File Access. FILE * fopen(const char *name, const char * mode); File Access, K&R 7.5 Dealing with named files is surprisingly similar to dealing with stdin and stdout. Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */ header

More information

File I/O. Last updated 10/30/18

File I/O. Last updated 10/30/18 Last updated 10/30/18 Input/Output Streams Information flow between entities is done with streams Keyboard Text input stream data stdin Data Text output stream Monitor stdout stderr printf formats data

More information

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

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

2009 S2 COMP File Operations

2009 S2 COMP File Operations 2009 S2 COMP1921 9. File Operations Oliver Diessel odiessel@cse.unsw.edu.au Last updated: 16:00 22 Sep 2009 9. File Operations Topics to be covered: Streams Text file operations Binary file operations

More information

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee. Midterm Exam Nov 8th, 2012 COMS W3157 Advanced Programming Columbia University Fall 2012 Instructor: Jae Woo Lee About this exam: - There are 4 problems totaling 100 points: problem 1: 30 points problem

More information

Goals of this Lecture

Goals of this Lecture I/O Management 1 Goals of this Lecture Help you to learn about: The Unix stream concept Standard C I/O functions Unix system-level functions for I/O How the standard C I/O functions use the Unix system-level

More information

Lecture 8: Structs & File I/O

Lecture 8: Structs & File I/O ....... \ \ \ / / / / \ \ \ \ / \ / \ \ \ V /,----' / ^ \ \.--..--. / ^ \ `--- ----` / ^ \. ` > < / /_\ \. ` / /_\ \ / /_\ \ `--' \ /. \ `----. / \ \ '--' '--' / \ / \ \ / \ / / \ \ (_ ) \ (_ ) / / \ \

More information

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture! I/O Management! 1 Goals of this Lecture! Help you to learn about:" The Unix stream concept" Standard C I/O functions" Unix system-level functions for I/O" How the standard C I/O functions use the Unix

More information

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture! I/O Management! 1 Goals of this Lecture! Help you to learn about:" The Unix stream concept" Standard C I/O functions" Unix system-level functions for I/O" How the standard C I/O functions use the Unix

More information

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination The following regulations are taken from the front cover of a University of Calgary examination answer

More information

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing Chapter 14: File Processing Files and Streams C views each file simply as a sequential stream of bytes. It ends as if there is an end-of-file marker. The data structure FILE, defined in stdio.h, stores

More information

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments Lecture 7: Files opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments Lecture 5: Files, I/O 0IGXYVI*MPIW 0 opening/closing files reading/writing

More information

HIGH LEVEL FILE PROCESSING

HIGH LEVEL FILE PROCESSING HIGH LEVEL FILE PROCESSING 1. Overview The learning objectives of this lab session are: To understand the functions used for file processing at a higher level. o These functions use special structures

More information

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng Slide Set 4 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section

More information

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga Arrays and Strings Antonio Carzaniga Faculty of Informatics Università della Svizzera italiana February 23, 2015 Outline General memory model Definition and use of pointers Invalid pointers and common

More information

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2016 ENCM 339 Fall 2016 Slide Set 14 slide 2/35

More information

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

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as

More information

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

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID: Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering Final Exam Name: This exam is open book and notes. Read the questions carefully and focus your answers on what has been

More information

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

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12 BIL 104E Introduction to Scientific and Engineering Computing Lecture 12 Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents

More information

Data File and File Handling

Data File and File Handling Types of Disk Files Data File and File Handling Text streams are associated with text-mode files. Text-mode files consist of a sequence of lines. Each line contains zero or more characters and ends with

More information

Integer Multiplication and Division

Integer Multiplication and Division Integer Multiplication and Division for ENCM 369: Computer Organization Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 208 Integer

More information

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

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010 Computer Programming: Skills & Concepts (CP1) Files in C 18th November, 2010 CP1 26 slide 1 18th November, 2010 Today s lecture Character oriented I/O (revision) Files and streams Opening and closing files

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

File and Console I/O. CS449 Spring 2016

File and Console I/O. CS449 Spring 2016 File and Console I/O CS449 Spring 2016 What is a Unix(or Linux) File? File: a resource for storing information [sic] based on some kind of durable storage (Wikipedia) Wider sense: In Unix, everything is

More information

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based Chapter 5, Standard I/O Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based #include FILE *f; Standard files (FILE *varname) variable: stdin File Number: STDIN_FILENO

More information

Week 9 Lecture 3. Binary Files. Week 9

Week 9 Lecture 3. Binary Files. Week 9 Lecture 3 Binary Files 1 Reading and Writing Binary Files 2 Binary Files It is possible to write the contents of memory directly to a file. The bits need to be interpreted on input Possible to write out

More information

System Software Experiment 1 Lecture 7

System Software Experiment 1 Lecture 7 System Software Experiment 1 Lecture 7 spring 2018 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungyunkwan University http://csl.skku.edu SSE3032: System Software Experiment 1, Spring 2018

More information

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Fundamentals of Programming. Lecture 10 Hamed Rasifard Fundamentals of Programming Lecture 10 Hamed Rasifard 1 Outline File Input/Output 2 Streams and Files The C I/O system supplies a consistent interface to the programmer independent of the actual device

More information

8. Characters, Strings and Files

8. Characters, Strings and Files REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence

More information

File I/O Lesson Outline

File I/O Lesson Outline 1. Outline 2. File I/O Using Redirection #1 3. File I/O Using Redirection #2 4. Direct File I/O #1 5. Direct File I/O #2 6. File I/O Mode 7. FILE Pointer 8. Reading from a File 9. Writing to a File 10.scanf

More information

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable

More information

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University Introduction to Computer and Program Design Lesson 6 File I/O James C.C. Cheng Department of Computer Science National Chiao Tung University File System in OS Microsoft Windows Filename DriveID : /DirctoryName/MainFileName.ExtensionName

More information

Lecture 7: file I/O, more Unix

Lecture 7: file I/O, more Unix CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 7: file

More information

University of Calgary Department of Electrical and Computer Engineering ENCM 339: Programming Fundamentals, Section 01 Instructor: Steve Norman

University of Calgary Department of Electrical and Computer Engineering ENCM 339: Programming Fundamentals, Section 01 Instructor: Steve Norman page 1 of 9 University of Calgary Department of Electrical and Computer Engineering ENCM 339: Programming Fundamentals, Section 01 Instructor: Steve Norman Fall 2017 FINAL EXAMINATION Location: ENG 60

More information

Standard File Pointers

Standard File Pointers 1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf

More information

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

fopen() fclose() fgetc() fputc() fread() fwrite() The ability to read data from and write data to files is the primary means of storing persistent data, data that does not disappear when your program stops running. The abstraction of files that C provides

More information

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

Systems Programming. 08. Standard I/O Library. Alexander Holupirek Systems Programming 08. Standard I/O Library Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008 Last lecture:

More information

Introduction to Computer Programming Lecture 18 Binary Files

Introduction to Computer Programming Lecture 18 Binary Files Introduction to Computer Programming Lecture 18 Binary Files Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr 1 RECALL: Text File Handling

More information

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

This code has a bug that allows a hacker to take control of its execution and run evilfunc(). Malicious Code Insertion Example This code has a bug that allows a hacker to take control of its execution and run evilfunc(). #include // obviously it is compiler dependent // but on my system

More information

File IO and command line input CSE 2451

File IO and command line input CSE 2451 File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to

More information

Today s Learning Objectives

Today s Learning Objectives Today s Learning Objectives 15-123 Systems Skills in C and Unix We will Review ints and modular arithmetic Learn basic Data types and Formats How Conditionals and loops work How Arrays are defined, accessed,

More information

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2 page 1 of 11 ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Lab instructions and

More information

today cs3157-fall2002-sklar-lect05 1

today cs3157-fall2002-sklar-lect05 1 today homework #1 due on monday sep 23, 6am some miscellaneous topics: logical operators random numbers character handling functions FILE I/O strings arrays pointers cs3157-fall2002-sklar-lect05 1 logical

More information

Goals of this Lecture

Goals of this Lecture C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing

More information

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017 Computer Security Robust and secure programming in C Marius Minea marius@cs.upt.ro 12 October 2017 In this lecture Write correct code minimizing risks with proper error handling avoiding security pitfalls

More information

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

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

More information

Slide Set 5. for ENCM 501 in Winter Term, Steve Norman, PhD, PEng

Slide Set 5. for ENCM 501 in Winter Term, Steve Norman, PhD, PEng Slide Set 5 for ENCM 501 in Winter Term, 2017 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2017 ENCM 501 W17 Lectures: Slide

More information

Input / Output Functions

Input / Output Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Input / Output Functions Presentation G Read/Study: Reek Chapter 15 Gojko Babić 10-03-2018 Input and Output Functions The stdio.h contain

More information

C-Refresher: Session 10 Disk IO

C-Refresher: Session 10 Disk IO C-Refresher: Session 10 Disk IO Arif Butt Summer 2017 I am Thankful to my student Muhammad Zubair bcsf14m029@pucit.edu.pk for preparation of these slides in accordance with my video lectures at http://www.arifbutt.me/category/c-behind-the-curtain/

More information

CSCI 2132 Final Exam Solutions

CSCI 2132 Final Exam Solutions Faculty of Computer Science 1 CSCI 2132 Final Exam Solutions Term: Fall 2018 (Sep4-Dec4) 1. (12 points) True-false questions. 2 points each. No justification necessary, but it may be helpful if the question

More information

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng Slide Set 5 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100 FINAL EXAMINATION INTRODUCTION TO ALGORITHMS AND PROGRAMMING II 03-60-141-01 U N I V E R S I T Y O F W I N D S O R S C H O O L O F C O M P U T E R S C I E N C E Winter 2014 Last Name: First Name: Student

More information

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge Programming in C 4. Misc. Library Features, Gotchas, Hints and Tips Dr. Neel Krishnaswami University of Cambridge (based on notes from and with thanks to Anil Madhavapeddy, Alan Mycroft, Alastair Beresford

More information

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4 CS 220: Introduction to Parallel Computing Arrays Lecture 4 Note: Windows I updated the VM image on the website It now includes: Sublime text Gitkraken (a nice git GUI) And the git command line tools 1/30/18

More information

C Basics And Concepts Input And Output

C Basics And Concepts Input And Output C Basics And Concepts Input And Output Report Working group scientific computing Department of informatics Faculty of mathematics, informatics and natural sciences University of Hamburg Written by: Marcus

More information

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING C: Part 6 LANGUAGE OF THE C In part 6 of Steve Goodwins C tutorial we continue our look at file handling and keyboard input File handling Most software will at some time need to read from (or perhaps write

More information

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

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

CSci 4061 Introduction to Operating Systems. Input/Output: High-level CSci 4061 Introduction to Operating Systems Input/Output: High-level I/O Topics First, cover high-level I/O Next, talk about low-level device I/O I/O not part of the C language! High-level I/O Hide device

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

Programming & Data Structure

Programming & Data Structure File Handling Programming & Data Structure CS 11002 Partha Bhowmick http://cse.iitkgp.ac.in/ pb CSE Department IIT Kharagpur Spring 2012-2013 File File Handling File R&W argc & argv (1) A file is a named

More information

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

ENCM 335 Fall 2018 Lab 2 for the Week of September 24 page 1 of 8 ENCM 335 Fall 2018 Lab 2 for the Week of September 24 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2018 Lab instructions and other documents

More information

Process Management! Goals of this Lecture!

Process Management! Goals of this Lecture! Process Management! 1 Goals of this Lecture! Help you learn about:" Creating new processes" Programmatically redirecting stdin, stdout, and stderr" Unix system-level functions for I/O" The Unix stream

More information