CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18.

Slides:



Advertisements
Similar presentations
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Advertisements

UNIT 15 File Processing.
Homework #4CS-2301 B-term Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Text File Input and Output. Overview Text File Buffered I/O Functions fopen Function Demo of File Write and Read.
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
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.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
1 Lecture09: Global Variables, File I/O, and Variable-Length Arguments 5/16/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
File Handling Spring 2013Programming and Data Structure1.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
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.
C Hints and Tips The preprocessor and other fun toys.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Minimal standard C program int main(void) { return 0 ; }
C Programming Lecture 12 : File Processing
Linked List (II) CGS 3460, Lecture 37 Apr 12, 2006 Hen-I Yang.
THE PREPROCESSOR
Lecture 18 CIS 208 Wednesday, March 30, Test2 April 15 th : Happy Tax day. Review: Wednesday, the 13 th.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
Advanced Programming in the UNIX Environment Hop Lee.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
External Files: Abstractly, a file can be thought of as a stream of data (either char or binary). C has two groups of files: standard files, such as stdin,
Chapter 4 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.
ECE Application Programming
Lecture 11 File input/output
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Computer Science 210 Computer Organization
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
Introduction to Programming
Computer Science 210 Computer Organization
File I/O We are used to reading from and writing to the terminal:
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
Programming and Data Structure
File Input and Output.
File Access (7.5) CSE 2031 Fall January 2019.
File Access (7.5) CSE 2031 Fall February 2019.
C Preprocessing File I/O
Module 12 Input and Output
File I/O & UNIX System Interface
CS1100 Computational Engineering
EECE.2160 ECE Application Programming
I/O CS580U - Fall 2018.
Week 2 - Friday CS222.
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18

2 The C preprocessor: Conditional Inclusion #if, #elif, #else, #endif –Allow us to define conditional statements for what the compiler should compile #define name and #undef name defined( name ) returns 1 if name is defined with a #define statement and 0 otherwise #ifdef and #ifndef are same respectively as #if defined and #if !defined #ifdef DEBUG printf(“Debug mode”); i++; #else printf(“Real mode”); #endif

3 Streams I/O done through streams; two kinds: text and binary –Text streams are sequences of lines, each of which is a sequence of characters terminated by a newline –Binary streams are sequences of characters corresponding to internal representation of data. Streams created by opening files and referenced using stream pointers (FILE *) Normally three standard streams are automatically open: –stdin(stream for standard input - from keyboard) –stdout(stream for standard output - to screen) –stderr(stream for standard error output - to screen)

4 Functions for opening & closing Open a file: –FILE *fopen(char *name, char *mode); –Opens file with name name using mode mode –Mode is taking values: “r” (open text for reading), “w” (open text for writing), etc… –For binary files add “b” at the end, e.g. “rb” (open binary for reading) –Usage: FILE *fp=fopen(“infile”, “r”); –Returns NULL if some problem occurs during the opening of the file (e.g. file is not found, or permissions are not set correctly etc.) Close a file: –int flcose(FILE *fp);

5 Some other functions Defined in int fprintf(FILE *fp, char *format, …); int fscanf(FILE *fp, char *format, …); int fgetc(FILE *fp); int fputc(int c, FILE *fp); char *fgets(char *s, int n, FILE *fp); int fputs(char *s, FILE *fp); int feof(FILE *fp); For more functions read appendix B of the K&R book

6 Example (from Kelley&Pohl - modified) #include void double_space(FILE *ifp, FILE *ofp) { int c; while ((c=getc(ifp)) != EOF) { putc(c, ofp); if (c==‘\n’) putc(‘\n’, ofp); /* duplicate Newline */ } int main(int argc, char **argv) { FILE *ifp = fopen(argv[1], “r”); FILE *ofp = fopen(argv[2], “w”); double_space(ifp, ofp); fclose(ifp); fclose(ofp); }

7 Announcements Wednesday: Mock quiz Friday: Quiz (the real one) – Make sure you come We are done with covering the K&R book Read the appendices by yourselves and look especially at appendix B where the library functions are presented Homework 4 is challenging and you should start as early as possible; I’ll announce extra office hours for the next week to allow you to come and talk to me about the homework I will do some revision, answer questions and do miscellaneous programming (debugging) skills next