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

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

Standard C Library Functions

File IO and command line input CSE 2451

C mini reference. 5 Binary numbers 12

C PROGRAMMING. Characters and Strings File Processing Exercise

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

System Software Experiment 1 Lecture 7

8. Characters, Strings and Files

Input / Output Functions

CS240: Programming in C

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

Lecture 8: Structs & File I/O

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

Standard File Pointers

Input/Output and the Operating Systems

C for C++ Programmers

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

C programming basics T3-1 -

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

211: Computer Architecture Summer 2016

PROGRAMMAZIONE I A.A. 2017/2018

Intermediate Programming, Spring 2017*

C Basics And Concepts Input And Output

Arrays, Strings, & Pointers

ENG120. Misc. Topics

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

Introduction to file management

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

Computer Programming Unit v

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 8 C Characters and Strings

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

PRINCIPLES OF OPERATING SYSTEMS

2009 S2 COMP File Operations

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

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

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

Lecture 03 Bits, Bytes and Data Types

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

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

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

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

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

Kurt Schmidt. October 30, 2018

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

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

Review Topics. Final Exam Review Slides

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

Model Viva Questions for Programming in C lab

File (1A) Young Won Lim 11/25/16

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

Simple Output and Input. see chapter 7

CSE 333 Lecture 7 - final C details

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

CS 241 Data Organization Pointers and Arrays

Lecture 9: File Processing. Quazi Rahman

UNIX input and output

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

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Programming refresher and intro to C programming

Naked C Lecture 6. File Operations and System Calls

Input/Output: Advanced Concepts

Chapter 8: Character & String. In this chapter, you ll learn about;

C Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings

File I/O. Preprocessor Macros

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Advanced C Programming Topics

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

C File Processing: One-Page Summary

ECE551 Midterm Version 1

CAAM 420 Notes Chapter 2: The C Programming Language

Lab # 4. Files & Queues in C

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

25.2 Opening and Closing a File

CS201 Lecture 2 GDB, The C Library

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

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

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

Lectures 5-6: Introduction to C

CSE 333 SECTION 3. POSIX I/O Functions

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

C-String Library Functions

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

C-Refresher: Session 10 Disk IO

IO = Input & Output 2

Lecture 7: file I/O, more Unix

SWEN-250 Personal SE. Introduction to C

Transcription:

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

Typedefs A typedef is a way to create a new type name Basically a synonym for another type Useful for shortening long types or providing more meaningful names Names are usually postfixed with "_t" typedef unsigned char byte_t; byte_t b1, b2; Use the size_t typedef (defined to be the same as long unsigned int in the stu headers) for non-negative sizes and counts const size_t STR_SIZE = 1024;

Structs A struct contains a group of related sub-variables New "kind" of type (like pointers were) Similar to classes from Java, but without methods and everything is public Sub-variables are called fields Struct variables are declared with struct keyword struct vertex { double x; double y; bool visited; ; int main() { struct vertex p1; p1.x = 4.2; p1.y = 5.6; p1.visited = false; double dist(struct vertex p1, struct vertex p2) { return sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );

Typedef structs Convention: create a typedef name for struct types E.g., struct vertex -> vertex_t More concise and readable For projects, we'll provide structs and typedefs in headers typedef struct vertex { double x; double y; bool visited; vertex_t; int main() { vertex_t p1; p1.x = 4.2; p1.y = 5.6; p1.visited = false; double dist(vertex_t p1, vertex_t p2) { return sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );

Struct memory layout Fields are stored (mostly) contiguously in memory Each field has a fixed offset from the beginning of the struct offset 0 p1 typedef struct vertex { double x; double y; bool visited; vertex_t; struct vertex int main() { vertex_t p1; p1.x = 4.2; p1.y = 5.6; p1.visited = false;

Struct memory layout Fields are stored (mostly) contiguously in memory Each field has a fixed offset from the beginning of the struct offset 0 p1.x 4.2 p1 typedef struct vertex { double x; double y; bool visited; vertex_t; struct vertex int main() { vertex_t p1; p1.x = 4.2; p1.y = 5.6; p1.visited = false;

Struct memory layout Fields are stored (mostly) contiguously in memory Each field has a fixed offset from the beginning of the struct p1.x offset 0 offset 8 p1.y 4.2 5.6 p1 typedef struct vertex { double x; double y; bool visited; vertex_t; struct vertex int main() { vertex_t p1; p1.x = 4.2; p1.y = 5.6; p1.visited = false;

Struct memory layout Fields are stored (mostly) contiguously in memory Each field has a fixed offset from the beginning of the struct offset 0 p1.x offset 8 p1.y p1.visited 4.2 5.6 0 p1 typedef struct vertex { double x; double y; bool visited; vertex_t; struct vertex offset 16 int main() { vertex_t p1; p1.x = 4.2; p1.y = 5.6; p1.visited = false;

Struct memory layout Given the following code, how much space will be allocated for the "data" variable? Assume chars are one byte each and ints are four bytes each. struct stuff { char a; char b; char c; int x; data; A) 4 bytes B) 7 bytes C) 8 bytes D) 16 bytes E) There is not enough information to know.

Struct data alignment By default, the compiler is allowed to insert padding Used to align fields on word-addressable boundaries, improving speed Use " attribute (( packed ))" to prevent this in GCC You'll see this in the elf.h header file for P1 Caution: this is non-standard and potentially non-portable typedef struct { char a; char b; char c; int x; stuff_t; sizeof(stuff_t) == 8 typedef struct attribute (( packed )) { char a; char b; char c; int x; stuff_t; sizeof(stuff_t) == 7 4-byte word 4-byte word a b c x 4-byte word a b c x 4-byte word padding

Function parameters In C, parameters are passed by value Values are copied to a function-local variable at call time Local changes are not visible to the caller unless returned It is expensive to pass large structs by value Must copy the entire struct even if it is not all needed Alternative: pass variables by reference using a pointer Local changes through the pointer are visible to the caller Local changes to the pointer are not visible to the caller Parameters can be passed as const Shouldn't be changed by the function (checked by compiler) Useful for ensuring you don't accidentally overwrite a by-reference parameter pointer

Struct pointers New "->" (arrow) operator dereferences a pointer to a struct and accesses a field in that struct vertex_t v; vertex_t *vp = &v; (*vp).x = 1.0; vp->y = 2.0; // set field "x" // set field "y" typedef struct vertex { double x; double y; bool visited; vertex_t; Faster than passing the entire struct! (copy 8 bytes instead of 17) double dist(vertex_t *p1, vertex_t *p2) { return sqrt( (p1->x p2->x) * (p1->x - p2->x) + (p1->y p2->y) * (p1->y - p2->y) );

Struct memory layout Given the following code, which of the following are valid ways to set the c field of the data variable to X? typedef struct stuff { char a; char b; char c; int x; stuff_t; stuff_t data; stuff_t *ptr = &data; A) data.c = X ; B) data->c = X ; C) ptr.c = X ; D) ptr->c = X ;

File I/O C standard library provides opaque file stream handles: FILE* Internal representation is implementation-dependent File manipulation functions: Open a file: fopen Mode: read ( r ), write ( w ), append ( a ) Read a character: fgetc Read a line of text: fgets Read binary data: fread Set current file position: fseek Write formatted text: fprintf These are all documented in the function reference (on website) Write binary data: fwrite Close a file: fclose

Standard I/O Buffered "file" streams: stdin, stdout, stderr (type is FILE*) Like System.in, System.out, and System.err in Java Available to all programs; no need to open or close Flushed when newline ( \n ) encountered (included by fgets!) Use CTRL-D to indicate end-of-file when typing input from the terminal Formatted input/output (scanf / printf) Variable number of arguments (varargs) Format string and type specifiers: %d for signed int, %u for unsigned int %c for chars, %s for strings (arrays of chars) %f or %e for float, %x for hex, %p for pointer Prepend l for long or ll for long long (e.g., %lx = long hex) Include number for fixed-width field (e.g., %20s for a 20-character field) Many more useful options; see documentation for details

Standard I/O What is wrong with the following code? char buffer[20]; fgets(buffer, 30, stdin); A) The buffer is not initialized before calling fgets. B) The buffer is the wrong size. C) The buffer size parameter is wrong. D) The call to fgets has too few parameters. E) There is nothing wrong with this code.

Security issues Input: beware of buffer overruns Like carelessly copying strings, reading input improperly is a common source of security vulnerabilities Best practice: declare a fixed-size buffer and use safe input functions (e.g., fgets) You may NOT use unsafe functions in this course! (e.g., gets) Here is a partial list of unsafe functions; see function reference on website for complete list UNSAFE atoi atof gets strcat strcpy Safer alternative strtol strtod fgets strncat snprintf Be careful with code that you find online never use code that you don't fully understand and have verified to be safe.

Projects You are now a C programmer! We have now covered all topics necessary for P0 and P1 There is certainly more to learn about C, but we have covered all the necessary topics for this course References and resources on our website On Thursday, we ll cover a few more useful things and some technicalities that we ve glossed over Now all you need is practice :)

Exercise Let's write a simple version of the 'cat' utility Copy all text from standard in to standard out No need to open/close a real file Handle a line at a time To reduce memory requirements What is the basic form of our code? What variable(s) will we need?

Simple cat program #include <stdio.h> int main (int argc, char **argv) { const int BUF_SIZE = 1024; char buffer[buf_size]; while ( ) { printf("%s", buffer); return 0;

Simple cat program #include <stdio.h> int main (int argc, char **argv) { const int BUF_SIZE = 1024; char buffer[buf_size]; while ( ) { printf("%s", buffer); return 0; CS 261 C function reference: w3.cs.jmu.edu/lam2mo/cs261/c_funcs.html

Documentation

Documentation the 'restrict' keyword means "this is the only active pointer to this variable"

Simple cat program #include <stdio.h> int main (int argc, char **argv) { const int BUF_SIZE = 1024; char buffer[buf_size]; while ( ) { printf("%s", buffer); return 0;

Simple cat program #include <stdio.h> int main (int argc, char **argv) { const int BUF_SIZE = 1024; char buffer[buf_size]; while (fgets(,, )!= NULL) { printf("%s", buffer); return 0;

Simple cat program #include <stdio.h> int main (int argc, char **argv) { const int BUF_SIZE = 1024; char buffer[buf_size]; while (fgets(buffer, BUF_SIZE, stdin)!= NULL) { printf("%s", buffer); return 0;

Exercise Write a program that reverses every line from standard in (stdin) Reminder: to compile your program (after creating rev.c): gcc -o rev rev.c To test your program (after creating input.txt):./rev <input.txt (or just./rev and type text followed by CTRL-D) Hint: use fgets() to read the input a line at a time into a char array, printing the characters in reverse after reading each line char* fgets (char *str, int count, FILE *stream) Read a line of text input from a file (returns str, count is max chars) size_t strlen (char *str) Calculate the length of a null-terminated string Sample input: Hello, world! My name is Bob. ENOD Sample output:!dlrow,olleh.bob si eman ym DONE