• No results found

File Management in C

In document C Notes (Page 125-132)

do-while Loop:

Category 3: Functions with arguments and return values

9. File Management in C

9.1 INTRODUCTION

A file is a place on the disk where a group of related data is stored.

Like most other languages, C supports a number of functions that have the ability to perform basic file operations, which include:

Naming a file, Opening a file,

Reading data from a file, Writing data to a file, and Closing a file.

High level I/O functions

Function Name Operation

fopen() Creates a new file for use Opens an existing file for use.

fclose() Closes a file which has been opened for use.

Getc() Reads a character from a file.

putc() Writes a character to a file.

fprintf() Writes a set of data values to a file.

fscanf() Reads a set of data values from a file.

getw() Reads an integer from a file.

putw() Writes an integer to file.

fseek() Sets the position to a desired point in the file Ftell() Gives the current position in the file

rewind() Sets the position to the beginning of the file.

9.2 DEFINING AND OPENING A FILE

If we want to store data in a file in the secondary memory, we must specify certain things about the file, to the operating system. They include:

1. Filename.

2. Data Structure.

3. Purpose.

Following is the general format for declaring and opening a file:

FILE *fp;

fp = fopen(“filename”, “mode”);

The first statement declares the variable fp as a “pointer to the data type FILE”. As stated earlier, FILE is a structure that is defined in the I/O library. The second statement opens the file named filename and assigns as identifier to the FILE the pointer fp. This pointer which contains all the information about the file is subsequently used as a communication link between the system and the program.

The second statement also specifies the purpose of opening this file. The mode does this job. Mode can be one of the following:

r open the file for reading only.

w open the file for writing only.

a open the file for appending (or adding) data to it.

Note that both the filename and mode are specified as strings. They should be enclosed in double quotation marks.

When trying to open a file, one of the following things may happen:

1. When the mode is „writing‟ a file with the specified name is created if the file does not exist. The contents are deleted, if the file already exists.

2. When the purpose is „appending‟, the file is opened with the current contents safe. A file with the specified name is created if the file does not exist.

3. If the purpose is „reading‟, and if it exists, then the file is opened with the current contents safe; otherwise an error occurs.

Consider the following statements:

FILE *p1, *p2;

p1 = fopen(“data”,”r”);

p2 = fopen(“results”,”w”);

Many recent compilers include additional modes of operation.

They include:

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.

We can open and use a number of files at a time. This number however depends on the system we use.

9.3 CLOSING A FILE

A file must be closed as soon as all operations on it have been completed. The general form is:

fclose(file_pointer);

9.4 INPUT/OUTPUT OPERATIONS ON FILES

Once a file is opened, reading out of or writing to it is accomplished using the standard I/O routines that are listed.

The getc and putc Functions

The simplest file I/O functions are getc and putc. These are analogous to getchar and putchar functions and handle one character at a time. Assume that a file is opened with mode w and file pointer fp1. Then, the statement

putc(c, fp1);

writes the character contained in the character variable c to the file associated with FILE pointer fp1. Similarly, getc is used to read a character from a file that has been opened in read mode. For example, the statement

c = getc(fp2);

would read a character from the file whose file pointer is fp2.

The file pointer moves by one character position for every operation of getc or putc. The getc will return an end-of-file marker EOF, when end of the file has been reached. Therefore, the reading should be terminated when EOF is encountered.

Example: Writing to and Reading from a File

#include<stdio.h>

#include<conio.h>

void main() {

FILE *f1;

char c;

clrscr();

printf(“Data Input\n\n”);

f1 = fopen(“INPUT”, “w”);

while((c=getchar()!=EOF) putc(c,f1);

fclose(f1);

printf(“\nData Output\n\n”);

f1 = fopen(“INPUT”,”r”);

while((c=getc(f1))!=EOF) printf(“%c”,c);

fclose(f1);

getch();

}

9.5 The fprintf and fscanf Functions

So far, we have seen functions which can handle only one character or integer at a time. Most compilers support two other functions, namely fprintf and fscanf, that can handle a group of mixed data simultaneously.

The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf and scanf functions, except of course that they work on files. The first argument of these functions is a file

pointer which specifies the file to be used. The general form of fprintf is

fprintf(fp, “control string”, list);

where fp is a file pointer associated with a file that has been opened for writing. The control string contains output specifications for the items in the list. The list may include variables, constants and strings.

Example:

fprintf(f1, “%s %d %f”,name,age,7.5);

Here, name is an array variable of type char and age is int variable.

The general format of fscanf is

fscanf(fp, “control string”, list);

This statement would cause the reading of the items in the list from the file specified by fp, according to the specifications contained in the control string. Example:

fscanf(f2, “%s %d”, item, &quantity);

Like scanf, fscanf also returns the number of items that are successfully read. When the end of file is reached, it returns the value of EOF.

Example: Handling of files with mixed Data Types

#include<stdio.h>

#include<conio.h>

void main() {

FILE *fp;

int number, quantity, i;

float price, value;

char item[10], filename[10];

clrscr();

printf(“Input file name\n”);

scanf(“%s”,filename);

fp = fopen(filename, “w”);

printf(“Input inventory data\n\n”);

printf(“Enter Item name, Number, price and quantity (3 records):\n”);

for(i=1;i<=3;i++) {

fscanf(stdin, “%s %d %f %d”,item, &number, &price, &quantity);

fprintf(fp, “%s %d %.2f %d”, item, number, price, quantity);

}

fclose(fp);

fprintf(stdout, “\n\n”);

fp = fopen(filename, “r”);

printf(“Item name Number price quantity value\n”);

for(i=1;i<=3;i++) {

fscanf(fp, “%s %d %f %d”,item, &number, &price, &quantity);

value = price * quantity;

fprintf(stdout, “%-8s %7d %8.2f %8d %11.2f\n”,item, number, price, quantity, value);

}

fclose(fp);

getch();

}

9.6 COMMAND LINE ARGUMENTS

What is a command line argument? It is a parameter supplied to a program when the program is invoked. This parameter may represent a filename the program should process. For example, if we want to execute a program to copy the contents of a file named X_FILE to another one named Y_FILE, then we may use a command line like

C:\TC>PROGRAM X_FILE Y_FILE

PROGRAM is the filename where the executable code of the program is stored. This eliminates the need for the program to request the user to enter the filenames during execution. How do these parameters get into the program?

We know that every C program should have one main function and that it marks the beginning of the program. But what we have not mentioned so far is that it can also take arguments like other functions. In fact main can take two arguments called argc and argv and the information contained in the command line is passed on to the program through these arguments, when main is called up by the system.

The variable argc is an argument counter that counts the number of arguments on the command line. The argv is an argument vector and represents an array of character pointers that point to the command line arguments. The size of this array will be equal to the

value of argc. For instance, for the command line given above, argc is three and argv is array of three pointers to strings as shown below:

argv[0] PROGRAM argv[1] X_FILE argv[2] Y_FILE

In order to access the command line arguments, we must declare the main function and its parameters as follows:

main(argc,argv);

int argc;

char *argv[];

The first parameter in the command line is always the program name and therefore argv[0] always represents the program name.

Example: Command line Arguments

#include<stdio.h>

#include<conio.h>

void main(argc,argv) int argc;

char *argv[];

{

FILE *fp;

int i;

char word[15];

clrscr();

fp = fopen(argv[1],"w");

printf("\nNo.of arguments in Command line=%d\n\n",argc);

for(i=2;i<argc;i++)

fprintf(fp,"%s",argv[i]);

fclose(fp);

printf("Contents of %s file\n\n",argv[1]);

fp=fopen(argv[1],"r");

for(i=2;i<argc;i++) {

fscanf(fp,"%s",word);

printf("%s",word);

}

fclose(fp);

printf("\n\n");

for(i=0;i<argc;i++)

printf("%*s\n",i*5,argv[i]);

getch();

}

In document C Notes (Page 125-132)

Related documents