0% found this document useful (0 votes)
33 views22 pages

Files: C A Programming Language

Files in C are used to store information permanently and access it when needed. The file system in C uses functions like fopen() to open files, fclose() to close them, fputc() and fgetc() to write and read characters, and fputs() and fgets() to handle strings. Other functions allow erasing files with remove(), seeking positions with fseek(), and flushing streams with fflush().

Uploaded by

Pankaj Malviya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views22 pages

Files: C A Programming Language

Files in C are used to store information permanently and access it when needed. The file system in C uses functions like fopen() to open files, fclose() to close them, fputc() and fgetc() to write and read characters, and fputs() and fgets() to handle strings. Other functions allow erasing files with remove(), seeking positions with fseek(), and flushing streams with fflush().

Uploaded by

Pankaj Malviya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Files

C A programming Language

objective

Explain how information are stored in memory in the form of files. Explain how to open and close the file . Explain how to read and write inside a file.

The C Programming Language

Files

Many application require that information be written to or read from an auxiliary memory device.Such information is stored on the memory in the form of a file. Thus ,files allow us to store information permanently, and to access and alter that information whenever necessary.

The C Programming Language

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
4

The C Programming Language

File System Basics

The ANSI C file system is composed of several interrelated functions. The most common of these are shown in Table.
Name fopen() fclose() putc() fputc() getc() fgetc() fseek() Function Opens a file Closes a file Writes a character to a file Same as putc() Reads a character from a file Same as getc() Seeks to a specified byte in a file

fprintf()
fscanf() feof() ferror()

Is t a file what printf() is to the console


Is to a file what scanf() is to the console Returns true if end-of-file is reached Returns true if an error has occurred

rewind()
remove() fflush() The C Programming Language

Resets the file position indicator to the beginning of the file


Erases a file Flushes a file

The File Pointer

A file pointer is a pointer to information that defines various things about the file, including its name, status, and the current position of the file. To obtain a file pointer variable, use a statement like this: FILE *fp;

The C Programming Language

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
7

The C Programming Language

Opening a file

The fopen() function opens a stream for use and links a file with that stream. Then it returns the file pointer associated with that file. The fopen() function has this prototype: FILE *fopen(const char *filename, const char *mode);

The C Programming Language

The following fragment uses fopen() to open a file names TEST for output. FILE *fp; fp=fopen(test, w); while technically correct, you will usually see the preceding code written like this: FILE *fp; if((fp=fopen(test,w))==NULL) { printf(cannot open file.\n); exit(1); }
9

The C Programming Language

Different modes used in a file


Mode r w A Rb Wb Ab r+ w+ a+ r+b w+b a+b write.
The C Programming Language

Meaning Open a text file for reading. Create a text file for writing. Append to a text file. Open a binary file for reading. Create a binary file for writing. Append to a binary file. Open a text file for read/ write. Creates a text file for read/write. Append or create a text file for read/write. Open a binary file for read / write. Create a binary file for read/ write. Append or create a binary file for read /
10

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream

The C Programming Language

11

Closing a File

The fclose() function closes a stream that was opened by a call to fopen(). fclose() also frees the file control block associated with the stream, making it available for reuse. The fclose() function has this prototype: int fclose(FILE *fp);

The C Programming Language

12

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
13

The C Programming Language

Writing a Character

Two equivalent functions that output a character are putc() and fputc(). The prototype of this function is int putc(int ch, FILE *fp);

The C Programming Language

14

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
15

The C Programming Language

Reading a Character

Two equivalent functions that input a character are getc() and fgetc(). The prototype of getc() is int getc(FILE *fp);

The C Programming Language

16

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
17

The C Programming Language

Working with Strings: fputs() and fgets()

C supports the related functions fputs() and fgets(), which read and write character strings from and to a disk file. They have the following prototypes: int fputs(const char *str, FILE *fp); char *fgets(char *str, int length, FILE *fp);

The C Programming Language

18

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
19

The C Programming Language

Erasing Files

The remove() function erases the specified file. Its prototype is int remove(const char *filename);

The C Programming Language

20

Files

File System Basic Opening a File Closing a File Writing a Character Reading a Character Working with Strings: fputs() and fgets() Erasing Files Flushing a Stream
21

The C Programming Language

Flushing a Stream

The fflush() function is used to flush the contents of an output stream, The prototype is shown here: int fflush(FILE *fp);

The C Programming Language

22

You might also like