1 of 11

File Management

{C}

Programming

Programming for Problem Solving (PPS)

GTU # 3110003

USING

Prof. Nilesh Gambhava

Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot

2 of 11

File management is what you have, and how you want to manipulate it. - Anonymous

3 of 11

Why File Management?

  • In real life, we want to store data permanently so that later we can retrieve it and reuse it.
  • A file is a collection of characters stored on a secondary storage device like hard disk, or pen drive.
  • There are two kinds of files that programmer deals with:
    • Text Files are human readable and it is a stream of plain English characters
    • Binary Files are computer readable, and it is a stream of processed characters and ASCII symbols

Binary File

110100110101000101101110101011101011101001101010001011011101010111010111010011

Text File

Hello, this is a text file. Whatever written here can be read easily without the help of a computer.

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

3

4 of 11

File Opening Modes

  • We can perform different operations on a file based on the file opening modes

Mode

Description

r

Open the file for reading only. If it exists, then the file is opened with the current contents; otherwise an error occurs.

w

Open the file for writing only. A file with specified name is created if the file does not exists. The contents are deleted, if the file already exists.

a

Open the file for appending (or adding data at the end of file) data to it. The file is opened with the current contents safe. A file with the specified name is created if the file does not exists.

r+

The existing file is opened to the beginning for both reading and writing.

w+

Same as w except both for reading and writing.

a+

Same as a except both for reading and writing.

Note: The main difference is w+ truncate the file to zero length if it exists or create a new file if it doesn't. While r+ neither deletes the content nor create a new file if it doesn't exist.

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

4

5 of 11

File Handling Functions

  • Basic file operation performed on a file are opening, reading, writing, and closing a file.

Syntax

Description

fp=fopen(file_name, mode);

This statement opens the file and assigns an identifier to the FILE type pointer fp.

Example: fp = fopen("printfile.c","r");

fclose(filepointer);

Closes a file and release the pointer.

Example: fclose(fp);

fprintf(fp, 

“control string”,

list);

Here fp is a file pointer associated with a file. The control string contains items to be printed. The list may includes variables, constants and strings.

Example: fprintf(fp, "%s %d %c", name, age, gender);

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

5

6 of 11

File Handling Functions

Syntax

Description

fscanf(fp, 

“control string”,

list);

Here fp is a file pointer associated with a file. The control string contains items to be printed. The list may includes variables, constants and strings.

Example: fscanf(fp, "%s %d", &item, &qty);

int getc(

FILE *fp);

getc() returns the next character from a file referred by fp; it require the FILE pointer to tell from which file. It returns EOF for end of file or error.

Example: c = getc(fp);

int putc(int c, 

FILE *fp);

putc() writes or appends the character c to the FILE fp. If a putc function is successful, it returns the character written, EOF if an error occurs.

Example: putc(c, fp);

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

6

7 of 11

Write a C program to display content of a given file.

#include <stdio.h>

void main()

{

    FILE *fp; //p is a FILE type pointer

    char ch; //ch is used to store single character

    fp = fopen("file1.c","r"); //open file in read mode and store file pointer in p

    do {  //repeat step 9 and 10 until EOF is reached

        ch = getc(fp); //get character pointed by p into ch

        putchar(ch); //print ch value on monitor

    }while(ch != EOF); //condition to check EOF is reached or not

    fclose(fp); //free up the file pointer pointed by fp

}

1

2

3

4

5

6

7

8

9

10

11

12

13

Program

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

7

8 of 11

Write a C program to copy a given file.

#include <stdio.h>

void main()

{

    FILE *fp1, *fp2; //p and q is a FILE type pointer

    char ch; //ch is used to store temporary data

    fp1 = fopen("file1.c","r"); //open file “file1.c” in read mode

    fp2 = fopen("file2.c","w"); //open file “file2.c” in write mode

    do { //repeat step 9 and 10 until EOF is reached

        ch = getc(fp1); //get character pointed by p into ch

        putc(ch, fp2); //print ch value into file, pointed by pointer q

    }while(ch != EOF); //condition to check EOF is reached or not

    fclose(fp1); //free up the file pointer p

    fclose(fp2); //free up the file pointer q

    printf("File copied successfully...");

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Program

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

8

9 of 11

File Positioning Functions

  • fseek, ftell, and rewind functions will set the file pointer to new location.
  • A subsequent read or write will access data from the new position.
  • fseek() - It is used to moves the reading control to different positions using fseek function.
  • ftell() - It tells the byte location of current position in file pointer.
  • rewind() - It moves the control to beginning of a file.

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

9

10 of 11

Write a C program to count lines, words, tabs, and characters

Lines = 22, tabs = 0, words = 152, characters = 283

#include <stdio.h>

void main()

{

    FILE *p;

    char ch;

    int ln=0,t=0,w=0,c=0;

    p = fopen("text1.txt","r");

    ch = getc(p);

    while (ch != EOF) {

        if (ch == '\n'

            ln++;

        else if(ch == '\t')

            t++;

        else if(ch == ' ')

            w++;

        else

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Program

Output

            c++;                                    

        ch = getc(p);

    }

    fclose(p);

    printf("Lines = %d, tabs = %d, words = %d, characters = %d\n",ln, t, w, c);

}

17

18

19

20

21

22

23

Program (contd.)

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

10

11 of 11

Thank you