Programming in C Lecture Tiina Niklander

Similar documents
CS240: Programming in C

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

Input / Output Functions

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

Standard C Library Functions

PROGRAMMAZIONE I A.A. 2017/2018

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

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

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

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

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

C Basics And Concepts Input And Output

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

File System User API

Standard File Pointers

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

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)

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

File IO and command line input CSE 2451

Computer Programming Unit v

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

C programming basics T3-1 -

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

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

CSCI 171 Chapter Outlines

Input/Output and the Operating Systems

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

Lecture 9: File Processing. Quazi Rahman

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

Standard I/O in C, Computer System and programming in C

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

HIGH LEVEL FILE PROCESSING

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

File I/O. Preprocessor Macros

ENG120. Misc. Topics

Ch 11. C File Processing (review)

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Computer programming

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

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

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

C: How to Program. Week /June/18

System Software Experiment 1 Lecture 7

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

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

IO = Input & Output 2

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

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

UNIX System Programming

Chapter 12. Files (reference: Deitel s chap 11) chap8

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

Naked C Lecture 6. File Operations and System Calls

M.CS201 Programming language

Lecture 03 Bits, Bytes and Data Types

Pointers and File Handling

File I/O. Arash Rafiey. November 7, 2017

2009 S2 COMP File Operations

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

CS240: Programming in C

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

C mini reference. 5 Binary numbers 12

Chapter 11 File Processing

CE Lecture 11

Continued from previous lecture

Chapter 14 - Advanced C Topics

Data File and File Handling

C File Processing: One-Page Summary

#define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */ #include <perlio.h> /* Usually via #include <perl.h> */

Input/Output: Advanced Concepts

UNIX input and output

The Software Platform consists of device stacks and software services. This section describes both parts and how they are related to each other.

SWEN-250 Personal SE. Introduction to C

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

8. Characters, Strings and Files

Programming Fundamentals

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CAAM 420 Notes Chapter 2: The C Programming Language

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.

EM108 Software Development for Engineers

Lecture 8: Structs & File I/O

Programming & Data Structure

The Design of C: A Rational Reconstruction: Part 2

25.2 Opening and Closing a File

Library Functions. General Questions

Computational Methods of Scientific Programming Fall 2007

C introduction: part 1

structs as arguments

Transcription:

Programming in C Lecture 4 24.9.2018 Tiina Niklander 2018 1

Week 3 exercises Week 3 exercises cover Files Open, close Different read functions Update Moving the read/write position counter Doubly linked list Was already in last week s material Mathematical functions Math.h 2018 2

Strings and memory allocation Limits the scope of variable to this block As variable, memory reservation at creation char name[50]; char name[] = Tiina Niklander ; As variable, memory reservation run time char *name; if (name = malloc (40 * sizeof(char))) ErrorHandling; As constant (=cannot be changed) #define NAME Tiina Niklander const char *name = Tiina ; static const char *name = Tiina ; Const means that pointer cannot be changed 2018 3

Strings and memory allocation K&R Ch 5.5 char amessage[] = now is the time ; /* an array */ char *pmessage = now is the time ; /* a pointer to constant*/ amessage: now is the time\0 Normal array variable that can be modified pmessage: now is the time\0 Pmessage can change and point to another string NOTE: string constant that cannot be changed! 2018 4

sizeof is a unary operator, not a function Array a has 19 bytes allocated (remember \0 ) Some systems threat a as pointer and then the printed value is 8. What does the following code print? #include <stdio.h> main() { char a[] = "Helsinki is a city"; char *b = "Helsinki is a city"; printf("a %ld %ld\n",sizeof(a),sizeof(*a)); printf("b %ld %ld\n",sizeof(b),sizeof(*b)); } Prints: a 19 1 b 8 1 One character occupies one byte 18 characters 64-bit architecture = 8 bytes character occupies one byte - even as part of string constant 2018 5

Files 2018 6

Files (Müldner, chapter 5) Text file binary File handle (vs. file pointer) Open and close How to use a file Errors in opening (and closing) Standard Files Basic I/O-operations for files Read and write Examples and idioms 2018 7

Files Just sequence of bytes. EOF byte at the end of the file. Two types; only difference in handling Text files handled by rows. At the end of each line a end-of-line byte \n (Indicates beginning of new line) Binary file has NO special bytes within the file. NOTE: Different operating systems may use different ways (e.g. byte sequences) to indicate end of line or file! 2018 8

Streams = file descriptors Unix handles all input and output as streams (in C: file descriptors) user input via keyboard print to screen files Predefined: stdin, stdout, stderr But also (NOTE: not covered on this course!) Unix pipes and redirecting data communication socket mouse input, other devices 2018 9

File descriptors (a.k.a file handle) File descriptor is a pointer to the FILE structure, which is passed to functions handling it. Defining a variable for file descriptor: FILE*handle; FILE *myfile1, *myfile2; typedef FILE* P_FILE; /* you can define a NEW type!!! */ P_FILE myfile1, myfile2; 2018 10

Header file: <stdio.h> Functions fclose, fopen, freopen feof, ferror, clearerr, perror fprintf, printf, sprintf, vfprintf, vprintf, vsprintf fscanf, scanf, sscanf fgetc, fgets, fputc, fputs, getc, getchar, gets, putc, putchar, puts fflush, fseek, ftell, fgetpos, fsetpos, rewind, ungetc fread, fwrite, remove, rename, tmpfile, tmpname, setbuf, setvbuf + others 2018 11

Open file Open with fopen() before using. Open connects the file descriptor and the file. Must give file name and the usage mode. handle = fopen( filename, r ); myfile1 = fopen( MyFile.txt, w ); myfile2 = fopen( test.out, wb ); 2018 12

Usage modes for text files "r" read from existing file (from beginning) "w" write to existing file (overwrite) or to new file that is created here (at beginning) "a" write to end of existing file (append) or to new file (at end) "r+" read and write, behaves like"r" "w+" read and write, behaves like"w" "a+" read and write, behaves like"a" If you want to handle binary file, add b to the string somewhere:"r+b". If file has been opened for both reading and writing, between I/O operations one of the following functions must be called: fseek(), fsetpos(), rewind() or fflush(). 2018 13

File open can fail! If the file open fails, fopen returns NULL (= erityinen nolla-arvo), muuten osoittimen tiedostoon eli tiedostokahvan. SO: Every time must check!! tiedostoa avattaessa on siis varmistuttava, että avaus onnistui! if (filehandle = fopen(fname, fmode)) == NULL) /* statements for the error handling */ 2018 14

About file names and maximum number of open files File name can contain also path. Different OS use different path names. Might contain character with special meaning in C language. (E.g. DOS/Windows uses \) In C \ is used as an indicator that the next character will have special meaning => In DOS \ must be replaced with \\ FILENAME_MAX (stdio.h) indicates the maximum allowed length of a file name. Number of concurrently open files is limited: at most FOPEN_MAX (stdio.h) allowed. 2018 15

Close Close the file, when not used anymore fclose(). File descriptor released from file and file closed. If fclose fails, it returns EOF. ALWAYS CHECK! if (fclose(myfile1) == EOF) /* statements for situation where close failed */ 2018 16

Standard files: stdin, stdout, stderr Always usable, predefined file descriptors (small integers 0,1 and 2) No open or close needed! stdin standard in; keyboard or redirection by OS stdout standard out; screen or redirection by OS stderr standard err; separate from stdout (usually screen also) FILE *inhandle;. inhandle = stdin; /* inhandle becomes a synonym to stdin*/ 2018 17

Basic I/O-operations for files Functions for files similar than the ones only using stdin or stdout: int fgetc (filehandle) ~ int getchar() int fputc (int, filehandle) ~ int putchar(int) int fscanf (filehandle,..) ~ int scanf( ) int fprintf (filehandle, ) ~ int printf( ) 2018 18

Reading a string: fgets read one line or maximum-1 chars NEVER use gets!! Use fgets() also with stdin, when not using scanf, or write your own: char *getline(char *s, int n) { int i, c; for (i = 0; i < n - 1 && (c = getchar())!= EOF && c!= (int)'\n'; i++) s[i] = c; if (c== \n ) { s[i]=c; ++i; } s[i] = '\0'; return c!= EOF? s : NULL; } Add arg: FILE * filedesc Change to: fgetc(filedesc); Kernighan&Ritchie: chapter 1.9 string array 2018 19

Reading one char or int from a file Reading one character if ((c=fgetc(filehandle)) == EOF) /*statements for the end-of-file handling */ Reading one integer if (fscanf (filehandle, %d, &i)!= 1) /* statements to do when read fails*/ 2018 20

Short task (2 minutes) Write a program that reads three decimal numbers from file num and prints their sum. - Structure of the program - statements in each phase 2018 21

Example: Read three decimal numbers from file num and print their sum. int main() { FILE *f; double x, y, z; File open idiom! if((f = fopen("num", "r")) == NULL) { fprintf(stderr, "Not opened: %s\n", "num"); return EXIT_FAILURE; } 2018 22

if(fscanf(f, "%lf%lf%lf", &x, &y, &z)!= 3) { fprintf(stderr, "Read from file failed\n"); return EXIT_FAILURE; } printf("%f\n", x + y + z); File read idiom if(fclose(f) == EOF) { fprintf(stderr, "File close failed\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } File close idiom Liisa Marttinen & Tiina Niklander 2018 23

Process one line char by char /* Read one line and print it in stdin*/ while((c = fgetc(handle))!='\n') if (c == EOF) break; else putchar(c); if(c!= EOF) putchar(c); /* Just locate the end of line */ while(((c = fgetc(handle))!='\n )&&(c!=eof)); /* Count the number of characters on the line */ while(((c = fgetc(handle))!='\n )&&(c!=eof)) ccount++; 2018 24

Write a program that counts and prints the number of lines in file test.txt. STRUCTURE OF THE PROGRAM: Definitions (header files, types, variables etc.) Open file Count the number of lines in file: While not EOF { If read \n increment counted } Print the number of lines Close file 2018 25

More functions: ungetc, feof ungetc (char, filehandle); Return the read character back to buffer. Actually moves the read position backwards one character. Does not change the file content. while (cond. (c = fgetc (filehandle))) process c; ungetc (c, filehandle); feof(filehandle); Only one character of pushback is guaranteed per file. [K&R Ch 7.8.3] Test the end of file. Works only after read!!! Returns 0, if at end of file, otherwise something else. 2018 26

File open if ((filehandle = fopen(fname, fmode)) ==NULL) /* failed */ if (close(filehandle) == EOF). /* failed */ if ((c = fgetc(filehandle)) == EOF) /* error */ if (fscanf (filehandle, %d, i)!=1) /* error */ File close Read one char Read one number while ((c= fgetc(filehandle))!= \n ) while ((c= fgetc(filehandle))!= EOF) Read to end of line Read to end of file Liisa Marttinen & Tiina Niklander www.cs.helsinki.fi 2018 27

Low-level I/O (Not covered in the exercises) File descriptors are only integers no FILE type definition open to open the I/O device read andwrite to access the device Map to system calls!!! 2018 28

Low-level I/O: simple example K&R: Ch 8.2 NOTE: stdin and stdout can be redirected to any file or device by OS!!! #include "syscalls.h" int main () { char buf [BUFSIZ]; int n; } while ( (n = read(stdin, buf, BUFSIZ) ) > 0 write (stdout, buf, n); return 0; 2018 29

Function arguments with multiple * 2018 30

read_lines (***array, *size); Why three asterisk characters in the signature? How to use in the call: char **linearray == NULL; int linecount = 0; read_line (&linearray, & linecount); Need to make linearray to point to the allocated array need to change the value of the pointer -> pass the address of the pointer Faculty of Science Department of Computer Science www.cs.helsinki.fi 24.9.2018 31

The three asterisks: char *** array main *array **array Single char is ***array linearray January read_lines (char **) February array (char *) (char) (char ***) Because you need to change value of linearray (in main), you need to pass its address to the function Faculty of Science Department of Computer Science www.cs.helsinki.fi 24.9.2018 32

Precompiler, more about macros, conditional compilation 2018 33

Precompiler, conditional compilation, macros (Müldner Ch. 6) C precompiler Macros Including external files ( using since week1) Simple macros (last weeks slides) Macros with parameters (Week5 exercises) Predefined macros Conditional compilation Use in debugging Assert-macro Use with and in header files Use to increase portability 2018 34

C precompiler # at the beginning of the line Line for precompiler => separate syntax Processed before compilation of the actual code Purpose? Macros: replace text with some other text Include external files #include <stdio> Conditional compilation: only part of the source code in compiled under certains conditions; used for debugging, portability, 2018 35

Macros Simple macros (no parameters) Shortening?; macro name replaced always with the same text Parameterized macros Parameters effect the replacement => More advanced, but has easily unexpected side effects Predefined macros Part of the C compiler implementation Useful in error situations 2018 36

Simple macro #define macroname macrovalue #define PI 3.14 #define PII 3.14159265358979323\ 846264338327950 #define SCREEN_W 80 #define SCREEN_H 25 MacroName usually with CAPITAL LETTERS! MacroValue until end of the line ( \n char) \ = continues to next line NOTE: NO = or ; (equal or semicolon)! 2018 37

Macro name replaced with macro value Precompiler replaces in source file every appearance of macroname by text in macrovalue. #define PI 3.14 i=pi; In compilation => i=3.14 #define PII =3.14; i=pii; In compilation => i ==3.14;; INCORRECT!!!! 2018 38

Assert macro (1) assert (int lauseke) (assert.h) Writing diagnostic information to stderr If condition untrue (false, 0), write to stderr condition, source code file name and line number: Assertion failed: cond, file filename, line linenumber (Tiedostonimi ja rivinumero saadaan makroista FILE ja LINE.) and stop the program execution with abort(). assert-macro is used to check that the program is working as expected: preconditions, post conditions, assumptions about variable values. etc. assert (i>=0) Example macros: assert (b*b 4*a*c >=0 assert (0>=i && i <size) 2018 39

Assert macro (2) assert() is usually in use and monitors the program performance. Operation controlled by macro NDEBUG ( = no debug) definition. If NDEBUG defined, assert does nothing. DefineNDEBUG either with #define NDEBUG in code or as command line argument for the compiler gcc DNDEBUG. 2018 40

Example: using assert #include<assert.h> void open_record(char *record_name) { assert(record_name!=null); /* Rest of code */ } } int main(void) { open_record(null); 2018 41

#define EMPTY (maxused == 0) #define TEST if (!(EMPTY? current == 0 : \ 0 < current && current <=maxused)) {\ fprintf(stderr, invariant failed; current = %d \t; \ maxused= %d\n, current, maxused); \ exit(1); } (maxused == 0)? current == 0 : 0 < current && current <=maxused Conditional operation 2018 42

Predefined macros LINE FILE TIME STDC row number of this line in source code name of the source file compilation time 1, if compiled according to C standard if (n>10) { /* error situation */ } fprintf (stderr, too large value of n in file %s return EXIT_FAILURE; row %d! \n", FILE, LINE ); 2018 43

Undefining a macro #undef PI If you want to redefine PI, it must first be undefined Can course problems otherwise! Can be undefined as command line argument to compiler 2018 44

Debugging code #define DEB /* just defining */ #ifdef DEB /*some debugging statements*/ printf("value of i = %d", i); #endif /* actual code */ Command line! gcc UDEB prog.c undefine macro gcc DDEB prog.c define macro 2018 45

Example: Example int main() { int i, j; printf("enter two integer values: "); if(scanf(%d%d, &i, &j)!= 2) return EXIT_FAILURE; #ifdef DEB printf("entered %d and %d\n", i, j); #endif printf("sum = %d\n", i + j); return EXIT_SUCCESS; } Liisa Marttinen & Tiina Niklander www.cs.helsinki.fi Advantages? 2018 46

External files (header files) Two formats: affect the search path #include filename /* User s own file, searched first from the local directory*/ #include <filename> /*System file, searched first from the system directory*/ Usually with file extension.h 2018 47

Standard Header Files stdio.h - the basic declarations needed to perform I/O ctype.h - for testing the state of characters math.h - mathematical functions, such as abs() and sin() + many more 2018 48