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

Similar documents
CSE2301. Introduction

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Input/Output and the Operating Systems

C-Refresher: Session 10 Disk IO

File I/O. Preprocessor Macros

File Handling. Reference:

CS240: Programming in C

25.2 Opening and Closing a File

File IO and command line input CSE 2451

EM108 Software Development for Engineers

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

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

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

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

UNIX System Programming

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

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

2009 S2 COMP File Operations

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

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.

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

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

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

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)

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

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

Operating System Labs. Yuanbin Wu

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

System Software Experiment 1 Lecture 7

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

Input / Output Functions

Naked C Lecture 6. File Operations and System Calls

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

Standard File Pointers

Computer Programming Unit v

Text Output and Input; Redirection

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

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

C PROGRAMMING. Characters and Strings File Processing Exercise

Computer System and programming in C

Simple Output and Input. see chapter 7

File and Console I/O. CS449 Spring 2016

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

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

System Programming. Standard Input/Output Library (Cont d)

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

C PROGRAMMING Lecture 6. 1st semester

C Programming Language

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

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

Intermediate Programming, Spring 2017*

Computer programming

Goals of this Lecture

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

structs as arguments

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

ENG120. Misc. Topics

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

Engineering program development 7. Edited by Péter Vass

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

Introduction to file management

Standard C Library Functions

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

C programming basics T3-1 -

M.CS201 Programming language

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

CSI 402 Lecture 2 (More on Files) 2 1 / 20

Fundamentals of Programming & Procedural Programming

Input/Output: Advanced Concepts

MODULE 3: Arrays, Functions and Strings

Data File and File Handling

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

CS240: Programming in C

File and Console I/O. CS449 Fall 2017

Course organization. Course introduction ( Week 1)

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

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

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

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

The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL.

HIGH LEVEL FILE PROCESSING

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

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

C Programming 1. File Access. Goutam Biswas. Lect 29

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.

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Advanced C Programming Topics

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

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

CAAM 420 Notes Chapter 2: The C Programming Language

Programming & Data Structure

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

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

Operating System Labs. Yuanbin Wu

Transcription:

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 did CSE2301 File Access Random Numbers Testing and Debugging These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University Introduction In this part, we introduce Testing and debugging Errors I/O Time and date Random number generation Streams and Files Stream: any source of input or any destination for output. Files, but could be also devices such as printers or network ports. Accessing streams is done via file pointer that is of type FILE *. Standard streams stdin, stdout, stderr. 1

Files You must open the file before you read or write to it (what about stdin, ). The system checks the file, and returns a small non-negative integer known as file descriptor, all reads and writes are through this file descriptor. 0,1,2 are reserved for stdin, stdout, and stderr. Files FILE *fp1; FILE *fopen(char *name, char *mode) fp1=fopen(name, mode); Do not assume file will open, always check for a null pointer. Name is a character string containing the name of the file, mode is a character string to indicate how the file will be used Mode could be r, w, a, r+,... Files To read or write characters from a file int fgetc(file *fp); Returns a byte from a file, or EOF if it encountered the end of file int fputc(int c, FILE *fp); Writes the character c to the file (where to write it?) Be aware of \ in the file name it might be treated as escape char. use /, or \ \ 2

opening a file FILE *fp fp = fopen( name, r ); if(fp == NULL) {printf ( ); exit }.. OR if((fp=fopen(name, r ) == NULL) {..} Character I/O putchar(ch) writes one char to stdout fputc(ch, fp) writes ch to fp (same for putc) putc is usually implemented as a macro or function, fputc is a function. putchasr is defined as #define putchar(c) putc((c, stdout) If error, return EOF Character I/O int fgetc(file *); int getc(file *); int getchar(void); /* from stdin */ int ungetc(int c, FILE *fp); Read char is unsigned char converted to int (must be int for EOF to work properly). while((ch = getc(fp) )!= EOF { bla bla bla } 3

Line I/O int fputs(const char * s, FILE *fp); int puts(const char * s); puts adds a newline char after s, fputs doesn t. Both return EOF in case of error Line I/O char *fgets(char * s, int n, FILE *fp); char *gets(char * s); gets reads character till a new line (discards) fgets reads characters till a newline or n-1 characters. if newline is read, it is added to the string. Block I/O size_t fread(void * ptr, size_t size, size_t nmemb, FILE *fp); size_t fwrite(void * ptr, size_t size, size_t nmemb, FILE *fp); The function reads nmemb elements of data each is size bytes long from the stream pointed to by fp and returns the actual number of items successfully read. 4

Position in Files int fseek(file *stream, long offset, int whence); The fseek() function shall set the file-position indicator for the stream pointed to by stream. If a read or write error occurs, the error indicator for the stream shall be set and fseek() fails. The new position, measured in bytes from the beginning of the file, shall be obtained by adding offset to the position specified by whence. The specified point is the beginning of the file for SEEK_SET, the current value of the file-position indicator for SEEK_CUR, or end-of-file for SEEK_END. Position in File some problems when dealing with text files. See example in the lecture. Formatted I/O we can use fprintf and fscanf with the first parameter a file pointer. Error? 5

Formatted I/O for scanf and fscanf, error may be End-of-file feof(fp) returns a non-zero value Read error ferror(fp) returns a nonzero value A matching error, neither of the above two indicators returns a non-zero. I/O size_t fread(void *restrict ptr, size_t size, size_t ntimes, FILE *restrict stream) size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); Random Numbers #include <stdlib.h> Int rand(void) Returns a random number between 0 and RAND_MAX Each time the program runs the function returns the same sequence Important in debugging, but sometimes we want to return random numbers every time we run the program drand48() uses a much more elaborate random number generator use srand(seed) first 6

Random Numbers void srand(unsigned int seed) Seeds the random number generator For a truly random number that will not be repeated every time you run the program open and read from /dev/random or /dev/urandom Random Numbers FILE *fp1; int c; fp1=fopen( /dev/urandom, r ); c=getc(fp1); printf( %d\n, (int)c); fclose(fp1); Time In time.h time_t time(time_t *x) Returns the number of seconds from jan. 1 1970 If x is not NULL, the value is stored in the variable pointed to by x Could be used to seed the RNG srand((unsigned int) time(null)); 7

Testing You wrote your program, compiled it (correcting syntax errors), ran it, and tested it, but it failed, what to do? There is a bug somewhere, to remove it we have to know where is it first. After finding it, remove/correct it changing as little code as possible to minimize introducing new bugs. Testing/Debugging Testing to find out as many things about the problem as possible Try to isolate the bug, what caused it Correct it. After that Test to see if the problem is solved Every thing that worked before is working No new errors are introduced Instrument your code Insert debugging statements. i=f(j); printf( f.in %d out %d \n,i,j); That shouldn t be in the final version You have to remove it after debugging, that may cause extra errors if you are not very careful in removing it 8

Instrument your code You can use conditional compilation #define DEBUG 1. i=f(j); #if DEBUG printf( f:in %d out %d\n,i,j); #endif main( ) {. function1( ) printf( before function 1.. ); printf( After function 1.. ); printf( before function 2.. ); function2 (.) printf( After function 3.. ); printf( before function 3.. ); Debugging function3 (..) Crashes, where is the problem Examples 2 examples on the use of gdb 9

Debugger Next Advance one statement, do not step into calls. Step Advance one statement in the program List show your program Run run Break line number, function name, fun:line Debugger Print: variable X: contents of an address Backtrace:The "backtrace" command tells gdb to list all the function calls (that leads to the crash) in the stack frame. del [n] delete break point number n Common Crash Causes Unaligned memory access (access to more than one byte must be aligned at a particular size), depends on the CPU. Using uninitialized pointers Going outside the array 10

Writing Good Code Debugging tools are no substitute to good programming practice. Be very careful with pointers and memory allocation and de-allocation Good modular design. Clear logic, boundary condition testing, assertions, limited global data, Assigning responsibility Core files When a program dumps core, it creates a dump files called core in the current directory. The file is an image of memory, registers including stack and data at time of crash. Can use gdb program core Some extra tools for memory dmalloc, electric fence, bcheck, valgrind Errors #include <errno.h> Defines an int errno and some other constants For example, math function (in case of error) sets errno to ERANGE or EDOM An errno of 0, means no error errno is not reset by itself, you must reset it. 11

Example #include <stdio.h> #include <errno.h> #include <math.h> #define POSITIVE 25 #define NEGATIVE -25 int main() { double ret; errno = 0; ret = sqrt(negative); if (errno == EDOM) /*EDOM Signifies Domain Error*/ printf("domain Error : Invalid Input To Function\n"); else printf("valid Input To Function\n"); errno = 0; ret = sqrt(positive); return 0; } Source : http://www.daniweb.com/code/snippet614.html if (errno == EDOM) printf("domain Error : Invalid Input To Function\n"); else printf("valid Input To Function\n"); Example #include <stdio.h> #include <errno.h> #include <math.h> void test(double value) { double ret; errno = 0; ret = sqrt(value); if ( errno ) { perror("sqrt"); } else { printf("ret = %g\n", ret); } } int main() { test(-25); test(25); } sqrt: Numerical argument out of domain Source : http://www.daniweb.com/code/snippet614.html 12