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

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher.
Advertisements

File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
Guide To UNIX Using Linux Third Edition
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
File Handling Spring 2013Programming and Data Structure1.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C Programming Lecture 12 : File Processing
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
11 C File Processing.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
An Introduction to C Programming
File I/O.
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
Programming in C Input / Output.
What you need for the 1st phase of project
File I/O We are used to reading from and writing to the terminal:
Chapter 11 – File Processing
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Text and Binary File Processing
File Input and Output.
File Handling.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
Programming in C Input / Output.
C Preprocessing File I/O
CSc 352 File I/O Saumya Debray Dept. of Computer Science
I/O CS580U - Fall 2018.
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

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

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 Better version #define PI ( ) Use the symbolic constant circle_length = 2 * PI * radius ;

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 compiler- dependent commands into a file.

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) =??? 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 any file Beware of including header files twice #include "MyFileName.c"

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

/* example.c */ #include #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" 2. Write your programs 3. Run "make" or "make -f makefile" Makefile is a list of rules and commands

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

HelloWorld Makefile (1) #This file contains information used by a program called make to #automate building the program APP = hello1 APPID = LFh1 RCP = hello.rcp PRC = hello1.prc SRC = helloMain.c CC = m68k-palmos-gcc PILRC = pilrc OBJRES = m68k-palmos-obj-res BUILDPRC = build-prc CFLAGS = -g

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

HelloWorld 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 /* MyProg.c */ int main ( int argc, char *argv[] ) {... > myProg one two three argc = 4 argv[0] = "myProg" argv[1] = "one" argv[2] = "two" argv[3] = "three“ argv[4] = NULL

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 "C" treats files as a series of bytes Basic library routines for file I/O #include

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

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: "\n\r" –Apple: RETURN at the end of line: "\r"

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 Reading (r) –sequential –random Writing (w) –sequential –random –appending (a) fopen() revisited FILE *fOut; fOut = fopen("c:\\data\\log.txt", "w" );

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

Binary and Random I/O Binary I/O readSize = fread(dataPtr, 1, size, myFile); //size of data read, if < size then encountered an error. writeSize = fwrite(dataPtr, 1, size, myFile); Positioning for random I/O fseek(myFile, 0, SEEK_SET); fseek(myFile, 10, SEEK_CUR); fseek(myFile, 0, SEEK_END);

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 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, *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 ^ 0xFF), out ); //UTF-16 vs UTF-8 (Unicode Byte Order mark) } //Unicode Transformation Format return 0; }