© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.

Slides:



Advertisements
Similar presentations
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
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,
UNIT 15 File Processing.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
© 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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
File Handling Spring 2013Programming and Data Structure1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
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:
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()
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
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.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
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)
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Files A collection of related data treated as a unit. Two types Text
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
CSC Programming for Science Lecture 18: More Data Files.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
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.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
C++ Basic Input and Output (I/O)
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
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.
Chapter 7 Text Input/Output Objectives
CS 261 – Recitation 7 Fall 2013 Oregon State University
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Programming in C Input / Output.
CSE1320 Files in C Dr. Sajib Datta
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
File Input and Output.
Programming in C Input / Output.
EPSII 59:006 Spring 2004.
EECE.2160 ECE Application Programming
Introduction to C Programming
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
ICS103: Programming in C 6: Pointers and Modular Programming
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors

C file input and output  Most of this set of notes shows only the differences from C++ file input and output.  A few slides in this set of notes discusses the pitfalls of using the end of file test to determine when to stop reading data. These slides are labelled to indicate they apply to both C and C++ © Janice Regan, CMPT

2 Reading/Writing data (1)  Use scanf to read data from the keyboard  scanf(“value of num is %lf \n”,num);  Waits on-screen for keyboard entry  Value entered at keyboard is placed in the memory location reserved for variable num, that is the entered value is "assigned" to num

© Janice Regan, CMPT Reading/Writing data (2)  printf( “Enter the value of num: %d“, num);  Value from the memory location reserved for variable num, is displayed on the screen  “Enter the value of num: %d“ is called the format statement

© Janice Regan, CMPT C File I/O Libraries  To access the objects and functions needed for file input and output  You need the library stdio.h #include

© Janice Regan, CMPT Connect to your file  First declare a pointer to the file identifier, You will refer to the file using the file identifier in the remainder of your code.  This process of connecting to the file is called “opening the file”, To connect you make the pointer point at the file FILE *myInFileId = NULL; myInFileId = fopen(“infile.txt”, “r”) FILE *myOutFileId = NULL: myOutFileId = fopen(“outfile.txt”, “w”);

© Janice Regan, CMPT Checking if the file opened  Need to check that the file was actually opened FILE *myInFileId = NULL; fopen(“infile.txt“, “r”); if ( myInFIleId == NULL ) { printf("File open failed.\n“); exit(1); }

© Janice Regan, CMPT Using files for input / output  Within C programs information is read from files or written to files  The file name (path) of the file saved on the computer’s disk for example infile.txt or outfile.txt is used only to connect to the file in your program  All access to the file once it has been opened is done using the file ID for example myInFileId or myOutFileId

© Janice Regan, CMPT Reading data from a file  To read from your file  You have now declared and connected to your file,  To read data from your input stream use myInFileId in an fscanf, to indicate which file your are reading from double mouse=0.0; fscanf( myInFileId, “%lf”, &mouse);

© Janice Regan, CMPT Writing data to a file  cout is an output stream  You have now declared and connected to your own output stream  To write data to this stream use myOutFileId in your fprint statement to indicate which file you are writing to int cat=0; fprintf(myOutFileId, “number of cats is %d”, cat);

Variable types for output streams  In C when we print to the screen we usually use printf(), we can also use fprintf()  The first value inside the brackets of the fprintf( ) is a variable of type FILE *  stdout and stderr are predefined variables of type FILE*  stdout = standard output  stderr = standard error © Janice Regan, CMPT

Printing information to the screen  When we would print using the cout stream in C++, we can use the standard output in C  The standard output is stdout  Two equivalent approaches printf( “This is note number %d\n”, num); fprintf( stdout, “This is note number %d\n”, num); © Janice Regan, CMPT

Printing errors to the screen  When we would print using the cerr stream in C++, we can use the standard error output in C  Standard error output is stderr fprintf( stderr, “This is note number %d\n”, num); © Janice Regan, CMPT

Reading information from the keyboard  When we would read using the cin stream in C++, we can use the standard input in C  The standard output is stdin  Two equivalent approaches scanf( “%d”, &num); fscanf( stdin, “%d”, &num); © Janice Regan, CMPT

© Janice Regan, CMPT Breaking the connection  When the program has completed reading from or writing to the file, the connection to the file is no longer needed.  The programmer needs to break the connection between the program and the file fclose(myInFileId); fclose(myOutFileId);

© Janice Regan, CMPT Formatting Output  We have seen how to format C output using C conversions using a printf statement  We can use the same C conversions in the same ways to format information we are writing to a file using an fprintf statement

© Janice Regan, CMPT Appending to a File FILE *myOutFileId == NULL; myOutFileId = fopen("important.txt", “a”);  If file doesn’t exist it will be created  If file already exists information will be added to the end of the existing file

© Janice Regan, CMPT Checking if data was read // Data is read from a file, one line (three values at a time // Each time a line is read the cost is updated // After all data is read the cost is printed to the screen FILE *inFile = NULL; double v1, cost; inFIle = fopen(“filename”, “r”); // check if file has been opened while ( fscanf(inFile, “%lf “, v1) == 1) { cost += v1; } fclose(inFile); printf(“total cost is %lf \n“,cost);

return value of printf or fprintf while ( fscanf(inFile, “%lf “, v1) == 1)  The statement in red is a function call  When a function is called and executes it will return a value. The functions fscanf and scanf return an integer  The integer represents the number of variables that were read successfully by the printf or fprintf statement © Janice Regan, CMPT

return value of printf or fprintf  if fprintf(inFile, “%lf %d %f“, v1, v2, v3)  If a writing error occurs returns a negative number  If writing is successful returns the number of characters written © Janice Regan, CMPT

return value of scanf or fscanf  if fscanf(inFile, “%lf %d %f“, &v1, &v2, &v3)  Reads beyond the end of file its return value is -1  Is unable to read the 1 st character of the 1 st variable because that character cannot be part of a variable of the type of the first variable its return value is 0  Reads the value of the 1 st variable but is unable to read the 1st character of the 2 nd variable its return value is 1  Reads the value of two variables but is unable to read the 1 st character of the 3 rd variable its return value is 2  Successfully reads all three variables its return value is 3 © Janice Regan, CMPT

© Janice Regan, CMPT Checking end of file // Data is read from a file, one line (three values at a time // Each time a line is read the cost is updated // After all data is read the cost is printed to the screen FILE *inFile = NULL; double v1, cost; inFIle = fopen(“filename”, “r”); // check if file has been opened while ( !feof()) { fscanf(inFile, “%lf %d %f “, &v1, &v2, &v3); cost += v1; } fclose(inFile); printf(“total cost is %lf \n“,cost);

Using EOF in C and C++  In some examples we have checked to see if we were at the end of the file.  The test for end of file is only true when the EOF bit is set. This bit is set when an attempt is made to read BEYOND the end of the file  When the last piece of data in the file is read, there has been no attempt to read beyond the end of the file so the test for EOF will return false (not at EOF)  If there is a new line or space after the last variable’s value in the file, then you will need one read to read the blank line or space and one additional read to read beyond the end of the file © Janice Regan, CMPT