Q-1 11/29/98 CSE / ENGR 142 Programming I Input/Output, Libraries, and Files © 1998 UW CSE.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
C Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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.
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,
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 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.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
Topic 13 – Various Other Topics. Enumerated Types.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Guide To UNIX Using Linux Third Edition
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
C Programming Tutorial – Part I CS Introduction to Operating Systems.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
Chapter 3 Input and Output
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
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. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Files A collection of related data treated as a unit. Two types Text
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
University of Washington Computer Programming I
Chapter 7 Text Input/Output Objectives
C Programming Tutorial – Part I
File Access (7.5) CSE 2031 Fall July 2018.
File Input/Output.
Programming in C Input / Output.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
File Input and Output.
Fundamental of Programming (C)
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
Lesson #2 Introduction to C.
EPSII 59:006 Spring 2004.
Chapter 11 Files chap8.
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

Q-1 11/29/98 CSE / ENGR 142 Programming I Input/Output, Libraries, and Files © 1998 UW CSE

Q-2 11/29/98 Textbook Readings Loose ends; combination of review and scattered textbook material Libraries: –Chapter 2 (here and there) –Chapter 13.2 (skim) Files: –Chapter 2.7 pp –Chapter 5.5 pp –Chapter 12.1

Q-3 11/29/98 Review: what's input/output? Central Processing Unit Main Memory Monitor Network Disk (files) Keyboard mouse

Q-4 11/29/98 Why File I/O?  Large volume of input data  Large volume of output data  More permanent storage of data  Transfer to other programs  Multiple simultaneous input and/or output streams

Q-5 11/29/98 Files A "file" is a collection of data on disk –managed by the user and the operating system –permanent A "file name" is how the user and OS know the file –follows OS naming rules (DOS: 8.3) We'll look at files used in compiling We'll look more at keyboard I/O We'll look at using text files in a C program First we’ll look at data files

Q-6 11/29/98 DATA FILES Business Data: customer files, payroll files, … Scientific Data: weather data, environmental data, topographic maps, … Image Data: web images, satellite images, medical images, … Web Data: HTML, GIF, JPEG, PNG, XML, …

Q-7 11/29/98 Business Data File NAME SSN BIRTH ADDRESS John Jones /1/ th NE, Seattle Sally Smith /3/ th NE, Seattle 98105

Q-8 11/29/98 Scientific Data File X Y ELEVATION RAINFALL

Q-9 11/29/98 Files Used in Compiling Source Files –.c files: C programs and functions –.h ("header") files: fragments of C code real-world projects may contain hundreds of source files! Compiled Files (system-dependent names) –object files: compiled C code ready to link –libraries: collections of compiled C functions –executable files: linked machine-language, ready to load into memory

Q-10 11/29/98 Header files (.h) Fragments of C code: –Function Prototypes –Symbolic Constants –Global Variable Declarations –Type Definitions (typedef) stdio.h hw.c GP142.h compiler linker ANSI lib compiler GP142.c.exe file

Q-11 11/29/98 Libraries Files of compiled, pre-written programs Why? Reuse existing code Enhance portability Hide system dependencies ANSI C Standard Libraries

Q-12 11/29/98 Keyboard Dangers What happens if the user types A in the following situation? int score ; scanf(“%d”, &score) ; while (score != 0) { input buffer printf(“%d \n”, score) ; A... scanf(“%d”, &score) ; }

Q-13 11/29/98 scanf’s Return Value scanf actually returns an int –tells the number of values successfully read: see Section 5.5. int status, id, score ; double grade ; status = scanf(“%d %lf %d”, &id, &grade, &score) ; if (status < 3) printf(“Error in input \n”) ;

Q-14 11/29/98 More Robust Input /* Robustly read an integer, consuming nondigits */ int read_int (void) { int status, input ; char junk ; status = scanf(“%d”, &input) ; while (status < 1) { /* unsuccessful read */ scanf(“%c”, &junk) ; /* consume 1 char */ status = scanf(“%d”, &input) ; /* try again */ } return(input) ; }

Q-15 11/29/98 Files as Streams of Characters keyboard/screen are special cases input / output streams of characters Multiple streams can be used simultaneously In reality, stream flows through a buffer rather than directly into or out of variables. program variables abc12 12 hx119 8s error\n 12, 13, 13

Q-16 11/29/98 Files as Records with Fields Business and Scientific Data program structure merged file data file 2 data file 1

Q-17 11/29/98 File variables Reminders: –A file is a collection of data on disk –A file name is how the user and OS know the file permanent name, follows OS naming rules A file variable is a variable in the C program which represents the file –temporary: exists only when program runs –follows C naming rules

Q-18 11/29/98 What's in stdio.h? Prototypes for I/O functions. Definitions of useful #define constants –Example: EOF for End of File Definition of FILE struct to represent information about open files. –File variables in C are pointers to the FILE struct. FILE *myfile;

Q-19 11/29/98 Opening A File "Opening" a file: making a connection between the operating system (file name) and the C program (file variable) –library function fopen –specify "r" (read, input) or "w" (write, output) Files must be opened before they can be used Files stdin/stdout (used by scanf/printf) are automatically opened & connected to the keyboard and display

Q-20 11/29/98 File Open Example /*usually done only once in a program*/ /*usually done near beginning of program*/ FILE *infilep, *outfilep; /*file variables*/ char ch; /* Open input and output files */ infilep = fopen (“Student_Data”, “r” ) ; outfilep = fopen (“New_Student_Data”, “w”) ;

Q-21 11/29/98 End of File (EOF) defined in stdio.h #define EOF (- some value ) –Usually -1 (but don’t depend on its value) –I/O library routines use EOF in various ways to signal end of file. –Your programs can check for EOF EOF is a status, not an input value

Q-22 11/29/98 Four Essential Functions for Text I/O FILE *filepi, *filepo ; int status; filepi = fopen (“name.ext”, “r”) ; /* read */ filepo = fopen (“name.ext”, “w”) ; /* write */ status = fscanf (filepi, “%... ”, &var,... ) ; /* fscanf returns EOF on end of file */ fprintf (filepo, “%... ”, var,... ) ; fclose (filepo);

Q-23 11/29/98 File Copy Example (streams) /* copy an input file to an output file */ /* files must already be open before this*/ status = fscanf (infilep, “%c”, &ch); while ( status != EOF ) { fprintf (outfilep, “%c”, ch) ; status = fscanf (infilep, “%c”, &ch); } printf (“File copied.\n”) ; fclose (infilep) ; fclose (outfilep) ;

Q-24 11/29/98 File Copy (Compact Edition) /* Many C programmers use this style*/... while ( fscanf (infilep, “%c”, &ch) != EOF ) fprintf (outfilep, “%c”, ch) ; printf (“File copied.\n”) ; fclose (infilep) ; fclose (outfilep) ;

Q-25 11/29/98 File Example: Implementing a Database Query #include int main(void) {FILE *inp, *outp; int age, j; char name[20], ssn[9], ch; inp = fopen (“db_file”, “r” ) ; outp = fopen (“result_file”, “w”) ; /* loop till the end-of-file * / while (fscanf (inp, “%c”, &name[0]) != EOF) { /* read name, ssn, age * / for (j = 1; j < 20; j++) fscanf(inp, “%c”, &name[j]); for (j = 0; j < 9; j++) fscanf(inp, “%c”,&ssn[j]); fscanf(inp, “%d”, &age); /* read line feed character */ fscanf(inp, “%c”, &ch); /* copy name, ssn to output if age > 20 */ if (age > 20) { for (j = 0; j < 20; j++) fprintf(outp, “%c”, name[j]); for (j = 0; j < 9; j++) fprintf(outp, “%c, ssn[j]); fprintf(outp, ”\n”); } fclose (inp) ; fclose (outp) ; return (0); } Equivalent query in SQL database language: SELECT NAME, SSN FROM DB_FILE WHERE AGE > 20;

Q-26 11/29/98 File Example: Expanding tabs #include int main(void) {FILE *infilep, *outfilep; char ch; int column = 0; /* Open input and output files */ infilep = fopen (“prog.c”, “r” ) ; outfilep = fopen (“tabless-prog.c”, “w”) ; /* process each input character */ while ( fscanf (infilep, “%c”, &ch) != EOF ){ if (ch == ‘\n’ || ch == ‘\r’) { /* end of line: reset column counter */ column = 0; fprintf (outfilep, “%c”, ch) ; } else if (ch == ‘\t’) { /* tab: output one or more spaces, */ /* to reach the next multiple of 8. */ do { fprintf (outfilep, “%c”, ‘ ‘) ; column++; } while ( (column % 8) != 0 ); } else { /* all others: count it, and copy it out */ column ++; fprintf (outfilep, “%c”, ch) ; } fclose (infilep) ; fclose (outfilep) ; return 0; } Input:a b \t c d \t e f Output:a bc de f

Q-27 11/29/98 File Example: Merging two sorted files #include #define MAXLINE 10000/*ASSUMES no line longer*/ int main(void) {FILE *in1p, * in2p, *outp; char buffer1[MAXLINE], buffer2[MAXLINE]; char *stat1, *stat2; in1p = fopen (“sorted-file1”, “r” ) ; in2p = fopen (“sorted-file2”, “r” ) ; outp = fopen (“merged-file”, “w”) ; stat1 = fgets(buffer1, MAXLINE, in1p); stat2 = fgets(buffer2, MAXLINE, in2p); while ( stat1 != NULL && stat2 != NULL) { if (strcmp(buffer1, buffer2) < 0) { fprintf (outp, “%s”, buffer1) ; stat1 = fgets(buffer1, MAXLINE, in1p); } else fprintf (outp, “%s”, buffer2) ; stat2 = fgets(buffer2, MAXLINE, in2p); } while( stat1 != NULL) { fprintf (outp, “%s”, buffer1) ; stat1 = fgets(buffer1, MAXLINE, in1p); } while( stat2 != NULL) { fprintf (outp, “%s”, buffer2) ; stat2 = fgets(buffer2, MAXLINE, in2p); } fclose (in1p) ; fclose (in2p) ; fclose (outp) ; return 0; } really should CHECK that no line is longer than MAXLINE