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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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,
Files in C Rohit Khokher.
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.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
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.
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
File I/O.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
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.
Binary Search Tree For a node: The left subtree contains nodes with keys less than the node's key. The right subtree contains nodes with keys greater than.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
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.
File Handling Spring 2013Programming and Data Structure1.
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.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File IO and command line input CSE 2451 Rong Shi.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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:
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
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.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Advanced Programming in the UNIX Environment Hop Lee.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
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.
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
File I/O.
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)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
CSE1320 Files in C Dr. Sajib Datta
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Input/Output and the Operating Systems
File Input and Output.
File Access (7.5) CSE 2031 Fall January 2019.
Lecture Starting K&R Chapter 7 and Appendix B
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
File Access (7.5) CSE 2031 Fall February 2019.
Module 12 Input and Output
Standard I/O Library Implementation
File I/O & UNIX System Interface
CS1100 Computational Engineering
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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

2 Intro to Homework 7 Data Structures behind the “Mapping” Maplist L int length Block *head Block *tail char *key char *data Block *next NULL All “real” Blocks Are linked in here “”

3 Intro to Homework 7 Why include the fake head and tail Blocks? So that we can use a pointer to the Block previous to the Block that we are accessing For example: Deleting a Block from the list: if(strcmp(bptr->next->key, key) == 0) { sptr = bptr->next; bptr->next = bptr->next->next; free((void *)sptr->key); free((void *)sptr->data); free((void *)sptr); }

4 Intro to Homework 7 If we didn’t do that, we would need to either: –Use a doubly linked list so that we could back up to remove a Block from the linked list OR –Save a pointer to the previous Block as we moved down the linked list so that we could back up to remove a Block from the linked list Both of these solutions can be made to work, but are a bit “messy” by comparison

5 File Access, K&R 7.5 Dealing with named files is surprisingly similar to dealing with stdin and stdout. Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */ header contains a structure definition with typedef name FILE that contains component variables (buffer, etc.) used in file I/O You don't need to know the details of these structs to use simple file I/O Use standard file access functions such as fopen()

6 File Access To open a file Function prototype fopen : FILE * fopen(const char *name, const char * mode); To ask fopen to open file (character string "name") in a particular "use mode". fp = fopen(name, mode) fp is the return value: set to NULL if fopen fails Legal “use mode” values: "r" for read, "w" for write, and "a" for append

7 File Access An "r" mode file can be used like stdin Function prototype getc: int getc(FILE *stream); To get a char from an "r" file c = getc(fp); c is the return value: Return value is the character read from the file OR Set to EOF if getc fails

8 File Access A “w“ or “a" mode file can be used like stdout Function prototype putc: int putc(int c, FILE *stream); To put a character to a “w” or “a” file status = putc(c, fp) Return value: set to EOF if putc fails

9 File Access When we fopen a file in "w" or "a" mode: If the file does not already exist, it will be created (like the vi editor creates a new file). If the file does already exist, then "w" mode fopen will destroy the old contents (like the command mv) and "a" mode will append new contents to the end of the existing file (like "save" command in mail).

10 File Access When you have finished reading from a file or writing to a file, call fclose to close the file status = fclose(fp); Function prototype: int fclose(FILE *stream);) Function fclose( ) returns: Zero for success OR EOF if an error occurs

11 File Access Every file open requires resources and there is a limit on number of files open at any one time –close each fp when done using it –close all files before program termination FILE structure has a buffer for disk data in memory When putc returns data may not get written to file THE DATA IS NOT SAFELY ON DISK YET! Functions fclose( ) and fflush( ) flush buffer to disk

12 File Access When a C program is started, the operating system opens three files and provides file pointers (FILE *) to them: stdin, stdout, and stderr We can now define getchar and putchar as macros: #define getchar( ) getc(stdin) #define putchar(c) putc((c), stdout) /* why (c) in parens? */ Other file oriented analogs for input / output functions: int fscanf(FILE *fp, char *format,...);/* mode must be "r" int fprintf(FILE *fp, char *format,...);/* mode "w" or "a"

13 Error Handling Trying to read a file that does not exist is an error, There are other errors as well: reading or writing a file without appropriate permission There are three streams opened by the O/S when a program begins execution, stdin, stdout, and stderr. stderr usually goes to the screen even if stdout is redirected to a file prog... >outfile/* redirect stdout; overwrite*/ prog... >&outfile/* redirect stdout and stderr */ prog... >>outfile/* redirect stdout; append */

14 Error Handling How to write a program so error msgs go to stderr? char *prog = argv[0];/* pick up command name */ if ((fp = fopen(*++argv, "r")) == NULL){ fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(1); } Variable prog is a pointer to char array containing the command name used to invoke this program and *argv is a pointer to char array containing the file name that couldn’t be opened

15 Error Handling The exit(int) function (arg: 0-255) terminates program execution and returns argument to invoking process (debugger, shell, fork parent) Of course, a "return value" from main program would do this as well, but exit() will terminate execution as if we executed a return from main(), and can be called from anywhere in program! A zero returned by a program means no error You can use conventions for meaning of non-zero values - best to keep the values positive

16 Error Handling To show how the return value may be used: UNIX conditional sequence && % gcc myprog.c && a.out Second program executes only if first returns == 0 UNIX conditional sequence || % gcc myprog.c || echo compilation failed Second program executes only if first returns != 0

17 Error Handling Note a problem with program on pg. 163 To handle errors, should use: #include The function ferror() tells us the last error that occurred for a stream if (ferror(stdout)) { fprintf(stderr, “%s: error writing stdout\n”, prog); exit(2); }

18 Error Handling If not exiting, to avoid retaining a stale error value from a previous failed operation, use: void clearerr(FILE *stream); Example: clearerr(stdout);

19 Error Handling More generally, errno.h contains a macro "errno" that can be tested; it is zero if there is no problem and non-zero otherwise Text in B1.7 says errno "may" contain an error number; it will contain one if there has been an error—any error, not just in a stream—unless the error is so serious it corrupted the error structs We can use the function perror to write out the error msg associated with errno, but we have to test for error right after it occurs to get right one

20 Error Handling We can use the function perror to write out standard error message associated with errno, but we have to test for error right after it occurs to get correct one if (errno != 0) { perror("Error at myprog: exiting."); exit(2); } perror will print out a standard error message corresponding to integer in errno, as if by: fprintf(stderr, "%s: %s\n", s, "error message");