File Input and Output.

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,
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 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
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,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSE1301 Computer Programming: Lecture 19 File I/O
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
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.
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.
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.
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: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
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:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
 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.
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.
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.
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 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
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
What you need for the 1st phase of project
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
File Handling.
Fundamental of Programming (C)
Line at a time I/O with fgets() and fputs()
Programming in C Input / Output.
C Preprocessing File I/O
File Handling in C.
EPSII 59:006 Spring 2004.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
Chapter 11 Files chap8.
File I/O.
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

File Input and Output

We consider only text I/O. Binary I/O is a more advanced topic. Objectives You will be able to Write C programs that read text data from disk files. Write C programs that write text data to disk files. We consider only text I/O. Binary I/O is a more advanced topic. Also random access I/O Not covered in this course.

File I/O Text File Input and Output is similar to Keyboard Input and Screen Output. Have to identify the file. Before we can read or write a file, we have to open the file. fopen() Standard IO Library Function Call to fopen() identifies a file. Specifies what we want to do with the file. Creates a data structure used by I/O library functions. Returns a pointer to that data structure.

FILE is a struct defined in stdio.h The FILE Struct FILE is a struct defined in stdio.h Used by the standard IO library functions on file read and write operations. Each file read or write operation passes a pointer to a FILE struct to a library function. Details of the struct definition are of no concern to us. An opaque object.

A FILE corresponds to a single instance of reading or writing a file. The FILE Struct A file can be open for reading by any number of different programs at the same time. Each has a separate FILE struct that holds information such as where the next read should occur. A FILE corresponds to a single instance of reading or writing a file. Stream is more descriptive. FILE is standard terminology.

The fopen() function FILE* fopen ( char* filename, char* mode) Text string just as we would use at the command line Can specify just name of a file in the current working directory Example: "test.txt" Can specify full path. Example: "/home/gamma/turnerr/Ch_15/test.txt“ mode What we want to do "r" to Read "w" to Write "a" to Append ...

The fopen() function Example: FILE* pFile; pFile = fopen("test.c", "r"); File Name (Current Working Directory) Read the file Returns NULL in case of error (e.g. File not found)

Opening a File for Reading FILE* pFile; pFile = fopen("test.c", "r"); if (pFile == NULL) { printf ("Error opening file\n"); return 1; }

Windows and Slashes Beware, Windows users: If you try to open a file using an absolute path, like fp = fopen("C:\Users\rtindell\silly.c","r"); you will get an error. Why? Because the \r is interpreted as an escape character. Two solutions: double your slashes: fp = fopen("C:\\Users\\rtindell\\silly.c","r"); or use Unix-style separators: fp = fopen("C:/Users/rtindell/silly.c","r"); The Windows-aware compiler will make the appropriate replacement.

To read a line of text from a file, use function fgets(). Similar to to the evil gets() function except Reads from an open file rather than from the keyboard. Call specifies maximum number of chars to read (including null terminator.) If new line char terminates the read, it is included in the input area.

Function fgets() char* fgets (char* buffer, int max, FILE* pFile) Returns NULL in case of no more characters or error address of input buffer Maximum number of chars to read (including terminating null) Pointer returned by fopen() Returns buffer when successful. Reads to end of line, but no more than max-1 characters. Newline character is included in the buffer (unlike gets.) Null char terminator is added to chars read from file.

Function fgets() Safer than gets() because you can specify the maximum number of chars to read. Can be used instead of gets() to read a line of text from the keyboard: #define MAX_LINE 80 ... char input_buffer[MAX_LINE]; fgets (input_buffer, MAX_LINE, stdin); Automatically defined FILE* for keyboard

Reading a File #include <stdio.h> #define MAX_LEN 1000 int main(void) { char buffer[MAX_LEN]; FILE* pFile; pFile = fopen("test.txt", "r"); if (pFile == NULL) printf ("Error opening file\n"); return 1; } while ( fgets (buffer, MAX_LEN, pFile ) != NULL ) printf ("%s", buffer); return 0;

A File to Read Use editor to create a short text file called test.txt First line of test.txt Second line Third and final line

Compile and Run test.c

What if input is stopped by the limit? #include <stdio.h> #define MAX_LEN 10 int main() { char buffer[MAX_LEN]; FILE* pFile; pFile = fopen("test.txt", "r"); if (pFile == NULL) printf ("Error opening file\n"); return 1; } while ( fgets (buffer, MAX_LEN, pFile ) != NULL ) printf ("%s", buffer); return 0;

Output looks the same! What’s going on here?

Separate the outputs. #include <string.h> #include <stdio.h> #define MAX_LEN 10 int main() { char buffer[MAX_LEN]; FILE* pFile; pFile = fopen("test.txt", "r"); if (pFile == NULL) printf ("Error opening file\n"); return 1; } while (fgets (buffer, MAX_LEN, pFile) != NULL) printf ("\n----------\n"); printf ("length is %d\n", (int) strlen(buffer)); printf ("%s", buffer); return 0;

Separate the outputs. Tenth char is the null terminator. Fifth char is the newline char. Sixth char is the null terminator.

Use “w” as the mode in fopen() to write Writing a File Use “w” as the mode in fopen() to write Creates file if it does not exist. Overwrites old version if file does exist. Use “a” to append to an existing file.

Undo the effects of fopen(); Writes any buffered data to the file. Closing a File int fclose (FILE* pFile); Undo the effects of fopen(); Writes any buffered data to the file. Frees resources used by the file. Done automatically when program ends. Good practice to close files when finished.

Writing to a File int fputs (char* s, FILE* pFile) Pointer to text string to be written Pointer returned by fopen() Returns EOF on error Returns zero when successful EOF is a symbolic constant for an integer value defined in stdio.h Usually -1 (But don’t assume this value. Use the symbol EOF.) Similar to puts() BUT ... fputs() DOES NOT append “\n” to the line like puts() does. fputs() DOES NOT output the null terminiator.

#include <stdio.h> #define MAX_LEN 1000 int main() { Program puts.c #include <stdio.h> #define MAX_LEN 1000 int main() { FILE* pFile; pFile = fopen("foo.txt", "w"); if (pFile == NULL) printf ("Error opening file\n"); return 1; } fputs("Humpty Dumpty sat on a wall\n", pFile); fputs("Humpty Dumpty had a great fall\n", pFile); fclose(pFile); printf ("File foo.txt written\n"); return 0; End of Presentation

Character I/O We next discuss library functions to read and write single characters (bytes). These functions work equally well for text and binary files. The functions discussed here treat characters as values of type integer One reason is that the input functions indicate an end-of-file (or error) condition by returning EOF, which is a negative integer constant. The output function we consider has the following prototype: int fputc(int c, FILE *stream); If a write error occurs, the error indicator is set for the file and the function returns EOF

Character Input Prototype: int fgetc(FILE *stream); This function reads a character from the file connected to the file handle stream. The function treats the character as an unsigned char value, which is then converted to int type before it's returned. At end of file, the function sets the stream's end-of-file indicator and returns EOF. If a read error occurs, the function sets the stream's error indicator and returns EOF.

Character Input A variant of fgetc is frequently used, and behaves exactly as fgetc int getc(FILE *stream) The difference is that getc is implemented as a macro and avoids the overhead of a function call. Don't worry about this for now. Similarly, there is another option for fputc (putc). However, there are a few dangers presented by putc and getc, so until you are more experienced, stick to fputc and fgetc. To copy one file to another, you should use fgetc and fputc as it will work for either text or binary files.

Formatted Output to a File A “file” version of printf allows us to do formatted output to a file. int fprintf (FILE* pFile, char* format, ...) Returns EOF on error Pointer to FILE struct open for write Format string (Same as for printf) Variables to be formatted Usually ignored

Formatted File Input A “file” version of scanf allows us to read numbers, and other formatted information, from a file. int fscanf (FILE* pFile, char* format, ...) Pointer to open FILE struct Format string (Same as for scanf) Variables to read into Returns number of variables filled, or EOF on error

fscanf() Format string same as for scanf() Percent sign field specifies conversion. Space in the format says to skip over white space in the input file. Any character other than space or format specifier must appear in the input file If present it is removed. If not present results in an error.

Checking for End of File int feof (FILE* pFile) Returns true when you have attempted to read beyond the end of the file. Returns false when end of file has not been reached. Check after a read operation and before processing the input data.