Introduction to Systems Programming CS 0449 C Preprocessing

  • Slides: 25
Download presentation
Introduction to Systems Programming (CS 0449) • C Preprocessing • Makefile • File I/O

Introduction to Systems Programming (CS 0449) • C Preprocessing • Makefile • File I/O

C Preprocessor • Modifies C code "to save typing" – Define constants – Define

C Preprocessor • Modifies C code "to save typing" – Define constants – Define macros – Include files – Other parameters (time of compilation. . . )

Preprocessor constants Define a symbolic constant like so #define PI 3. 141526 Better version

Preprocessor constants Define a symbolic constant like so #define PI 3. 141526 Better version #define PI ( 3. 141526 ) Use the symbolic constant circle_length = 2 * PI * radius ;

Preprocessor constants (2) Check if constant defined ( #ifdef ) #define VERBOSE. . .

Preprocessor constants (2) Check if constant defined ( #ifdef ) #define VERBOSE. . . #ifdef VERBOSE printf("I am extremely glad to see you !n"); #else printf("Hi !n"); #endif #pragma – this directive is for inserting compilerdependent commands into a file.

Preprocessor Macros Parameterized Macros: Similar to function calls. Symbolic parameters ! #define SQUARE( x

Preprocessor Macros Parameterized Macros: Similar to function calls. Symbolic parameters ! #define SQUARE( x ) x*x Better version: #define SQUARE( x ) ((x) * (x)) Usage: What will be the output for each version? int x x = SQUARE ( 1 + 2 + 3 ); (1+2+3*1+2+3) =? ? ? printf( " x = %d n", x ); is x=11? , or is it, x=36? How do you fix it to generate 36? ((1+2+3) * (1+2+3))

Including files • Used to include header files • Can be used to include

Including files • Used to include header files • Can be used to include any file • Beware of including header files twice #include "My. File. Name. c"

Header files • Usually define function prototypes, user defined types and global variables. •

Header files • Usually define function prototypes, user defined types and global variables. • Avoid including twice int x; /* included from my. Header. h */ • Standard header file header #ifndef My. Header. File_H #define My. Header. File_H. . . /* header file contents goes here */ #endif

/* example. c */ #include <stdio. h> #define ONE 1 main(){ if(ONE != 1)

/* example. c */ #include <stdio. h> #define ONE 1 main(){ if(ONE != 1) return 0; printf("The answer is %d. n", myfunc(2, 6) ); } myfunc( int a, int b) { int i = ONE, j = ONE; for( ; i <= b; ++i) j = j * a; return j; } See this Example

Makefile • Script file to automate program compilation and linking (making) 1. Write the

Makefile • Script file to automate program compilation and linking (making) 1. Write the "makefile" 2. Write your programs 3. Run "make" or "make -f makefile" • Makefile is a list of rules and commands http: //www. gnu. org/software/make/

Makefile • Comments start with "#" # This is a makefile for Hello World

Makefile • Comments start with "#" # This is a makefile for Hello World application • Variable definitions, usually in capital letters CC = m 68 k-palmos-gcc • Main target definition all: hello 1. prc • Dependency definitions %. o: %. c $(CC) $(CFLAGS) -c $< -o $@

Hello. World Makefile (1) #This file contains information used by a program called make

Hello. World Makefile (1) #This file contains information used by a program called make to #automate building the program APPID RCP PRC SRC = hello 1 = LFh 1 = hello. rcp = hello 1. prc = hello. Main. c CC = m 68 k-palmos-gcc PILRC = pilrc OBJRES = m 68 k-palmos-obj-res BUILDPRC = build-prc CFLAGS = -g

Hello. World Makefile (2) all: $(PRC): grc. stamp bin. stamp; $(BUILDPRC) $(APP) $(APPID) *.

Hello. World Makefile (2) all: $(PRC): grc. stamp bin. stamp; $(BUILDPRC) $(APP) $(APPID) *. grc *. bin ls -l *. prc grc. stamp: $(APP) $(OBJRES) $(APP) touch $@ $(APP): $(SRC: . c=. o) $(CC) $(CFLAGS) $^ -o $@

Hello. World Makefile (3) bin. stamp: $(RCP) $(PILRC) $^ $(BINDIR) touch $@ %. o:

Hello. World Makefile (3) bin. stamp: $(RCP) $(PILRC) $^ $(BINDIR) touch $@ %. o: %. c $(CC) $(CFLAGS) -c $< -o $@ depend dep: $(CC) -M $(SRC) >. dependencies clean: rm -rf *. o $(APP) *. bin *. grc *. stamp *~ veryclean: clean rm -rf *. prc *. bak

Command Line Arguments /* My. Prog. c */ int main ( int argc ,

Command Line Arguments /* My. Prog. c */ int main ( int argc , char *argv[] ) {. . . > my. Prog one argc = 4 argv[0] = "my. Prog" argv[1] = "one" argv[2] = "two" argv[3] = "three“ argv[4] = NULL two three

Introduction to Systems Programming (CS 0449) File Input / Output Ch. 11 Deitel &

Introduction to Systems Programming (CS 0449) File Input / Output Ch. 11 Deitel & Deitel book

What is a File • A file is a collection of related data •

What is a File • A file is a collection of related data • "C" treats files as a series of bytes • Basic library routines for file I/O #include <stdio. h>

Basics About Files • Files must be opened and closed #include <stdio. h>. .

Basics About Files • Files must be opened and closed #include <stdio. h>. . . FILE * my. File; my. File = fopen ("C: \data\myfile. txt", "r"); // Name, Mode (r: read) if ( my. File == NULL ){ // (w: write) /* Could not open the file */. . . }. . . fclose ( my. File ); Note: status = fclose(file-variable) status = 0 if file closed successfully- Error otherwise.

End-line Character • Teletype Model 33 (long time ago. . . ) used 2

End-line Character • Teletype Model 33 (long time ago. . . ) used 2 characters at the end of line. – RETURN character – LINE FEED character • Computer age – UNIX: LINE FEED at the end of line: "n" – MS-DOS/Windows: both characters: "nr" – Apple: RETURN at the end of line: "r"

File Types • Text (ASCII) files • Binary files • Special (device) files stdin

File Types • Text (ASCII) files • Binary files • Special (device) files stdin - standard input (open for reading) stdout - standard output (open for writing) stderr - standard error (open for writing)

Operations with Files • Writing (w) • Reading (r) – sequential – random –

Operations with Files • Writing (w) • Reading (r) – sequential – random – appending (a) • fopen() revisited FILE *f. Out; f. Out = fopen("c: \data\log. txt", "w" );

Useful File I/O Functions • fopen(), fclose() files • fprintf ( my. File, "format.

Useful File I/O Functions • fopen(), fclose() files • fprintf ( my. File, "format. . . ", . . . ) I/O • fscanf ( my. File, "format. . . ", . . . ) • fgets(), fputs() • fgetc(), fputc() I/O • feof() -- open/close -- formatted -- for line I/O -- for character -- end of file detection, when

Binary and Random I/O • Binary I/O read. Size = fread(data. Ptr, 1, size,

Binary and Random I/O • Binary I/O read. Size = fread(data. Ptr, 1, size, my. File); //size of data read, if < size then encountered an error. write. Size = fwrite(data. Ptr, 1, size, my. File); • Positioning for random I/O fseek(my. File, 0, SEEK_SET); fseek(my. File, 10, SEEK_CUR); fseek(my. File, 0, SEEK_END);

Buffered v. s. Unbuffered I/O //no immediate write to file, instead buffer data and

Buffered v. s. Unbuffered I/O //no immediate write to file, instead buffer data and then flush after program finished • Buffered I/O may improve performance • Buffered I/O is with f. . . () functions – fopen(), fwrite() • Unbuffered I/O – open(), write()

Streams v. s. Records • Stream - a file interpreted as a stream of

Streams v. s. Records • Stream - a file interpreted as a stream of bytes • Record set - a file interpreted as a set of records, structures – The structures can be of the same size, or – each record can be of different size

Example: En/De-Crypter int main ( int argc, char * argv[] ) { FILE *in,

Example: En/De-Crypter int main ( int argc, char * argv[] ) { FILE *in, *out; in = fopen ( argv[1], "rb"); out = fopen ( argv[2], "wb"); if ( ! in | | ! out ){ printf( "Error opening files ! n" ); return -1; } while( ! feof ( in ) ){ ch = fgetc ( in ); fputc ( (ch ^ 0 x. FF) , out ); //UTF-16 vs UTF-8 (Unicode Byte Order mark) } //Unicode Transformation Format return 0; }