Files: C A Programming Language
Files: C A Programming Language
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.
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.
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 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()
rewind()
remove() fflush() The C Programming Language
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;
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
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 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
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
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);
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
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);
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
Reading a Character
Two equivalent functions that input a character are getc() and fgetc(). The prototype of getc() is int getc(FILE *fp);
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
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);
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
Erasing Files
The remove() function erases the specified file. Its prototype is int remove(const char *filename);
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
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);
22