0% found this document useful (0 votes)
30 views7 pages

File Handling and Command Line Arguments

A file is an object that stores data used by computer programs. Files are created using software and display as icons. The document discusses creating and opening files in C using functions like fopen(), fclose(), getc(), putc(), fprintf(), and fscanf(). It also covers reading and writing text and binary files, and using command line arguments in programs.

Uploaded by

13 Smarty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views7 pages

File Handling and Command Line Arguments

A file is an object that stores data used by computer programs. Files are created using software and display as icons. The document discusses creating and opening files in C using functions like fopen(), fclose(), getc(), putc(), fprintf(), and fscanf(). It also covers reading and writing text and binary files, and using command line arguments in programs.

Uploaded by

13 Smarty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Definition of Files-

A file is an object on a computer that stores data, information, settings, or


commands used with a computer program. In a GUI (graphical user interface),
such as Microsoft Windows, files display as icons that relate to the program that
opens the file.

A file is created using a software program on the computer. For example, to


create a text file you would use a text editor, to create an image file you would
use an image editor, and to create a document you would use a word processor.

CREATING A FILE

Before starting the work with files, it is necessary to set up a buffer area known
as file buffer.

This file buffer is space in main memory and is used to store information
temporarily when information being transferred between memory and data file.

This is done by-

FILE * fvar;

Where FILE is the structure datatype in C which establishes the buffer area.

fvar is a pointer variable that contains the starting address of buffer area. This is
known as stream pointer of buffer variable.

One stream pointer is required for each file which we want to use whether we
want to read from the file or to write to the file.

Standard function for File Handling-

Function Description

fopen() create a new file or open a existing file

fclose() closes a file


getc() reads a character from a file

putc() writes a character to a file

fscanf() reads a set of data from a file

fprintf() writes a set of data to a file

getw() reads a integer from a file

putw() writes a integer to a file

fseek() set the position to desire point

ftell() gives current position in the file

rewind() set the position to the beginning point

feof() detects end of file marker in the file

Modes for working on files-

Mode Description

r opens a text file in reading mode

w opens or create a text file in writing mode.

a opens a text file in append mode

r+ opens a text file in both reading and writing mode

w+ opens a text file in both reading and writing mode


a+ opens a text file in both reading and writing mode

rb opens a binary file in reading mode

wb opens or create a binary file in writing mode

ab opens a binary file in append mode

rb+ opens a binary file in both reading and writing mode

wb+ opens a binary file in both reading and writing mode

ab+ opens a binary file in both reading and writing mode

Opening a File or Creating a File-


The fopen() function is used to create a new file or to open an existing file.
General Syntax:
*fp = FILE *fopen(const char *filename, const char *mode);
Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to the
opened(or created) file.

filename is the name of the file to be opened and mode specifies the purpose of
opening the file.

Closing a File-
The fclose() function is used to close an already opened file.
General Syntax :
int fclose( FILE *fp);
Here fclose() function closes the file and returns zero on success, or EOF if
there is an error in closing the file. This EOF is a constant defined in the header
file stdio.h.

Input/Output operation on File-


In program getc() and putc() are the simplest functions which can be used to
read and write individual characters to a file.
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
while( (ch = getchar()) != EOF)
{
putc(ch, fp);
}
fclose(fp);
fp = fopen("one.txt", "r");
while( (ch = getc(fp)! = EOF)
printf("%c",ch);
fclose(fp); // closing the file pointer
getch();
}

Reading and Writing to File using fprintf() and fscanf()-


#include<stdio.h>
#include<conio.h>
struct emp
{
char name[10];
int age;
};

void main()
{
struct emp e;
FILE *p,*q;
p = fopen("one.txt", "a");
q = fopen("one.txt", "r");
printf("Enter Name and Age:");
scanf("%s %d", e.name, &e.age);
fprintf(p,"%s %d", e.name, e.age);
fclose(p);
do
{
fscanf(q,"%s %d", e.name, e.age);
printf("%s %d", e.name, e.age);
}
while(!feof(q));
fclose(q);
getch();
}

In this program, we have created two FILE pointers and both are referring to the
same file but in different modes.
fprintf() function directly writes into the file, while fscanf() reads from the file,
which can then be printed on the console using standard printf() function.

Difference between Append and Write Mode-


Write (w) mode and Append (a) mode, while opening a file are almost the
same. Both are used to write in a file. In both the modes, new file is created if it
doesn't exists already.

The only difference they have is, when you open a file in the write mode, the
file is reset, resulting in deletion of any data already present in the file.

While in append mode this will not happen. Append mode is used to append or
add data to the existing data of file(if any). Hence, when you open a file in
Append(a) mode, the cursor is positioned at the end of the present data in the
file.

Reading and Writing in a Binary File-


A Binary file is similar to a text file, but it contains only large binary data. The
Opening modes are mentioned in the table for opening.
fread() and fwrite() functions are used to read and write is a binary file.
fwrite(data-element-to-be-written, size_of_elements, number_of_elements,
pointer-to-file);
fread() is also used in the same way, with the same arguments
like fwrite() function.

Simple example of writing into a binary file-


const char *mytext = "Hello World";
FILE *bfp= fopen("test.txt", "wb");
if (bfp)
{
fwrite(mytext, sizeof(char), strlen(mytext), bfp);
fclose(bfp);
}

Command Line Arguments

It is possible to pass some values from the command line to your C programs
when they are executed. These values are called command line arguments and
many times they are important for your program especially when you want to
control your program from outside instead of hard coding those values inside
the code.
The command line arguments are handled using main() function arguments
where argc refers to the number of arguments passed, and argv[] is a pointer
array which points to each argument passed to the program.

Program for example-


#include <stdio.h>

int main( int argc, char *argv[] )


{

if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}

It should be noted that argv[0] holds the name of the program itself
and argv[1] is a pointer to the first command line argument supplied, and
*argv[n] is the last argument. If no arguments are supplied, argc will be one,
and if you pass one argument then argc is set at 2.

You pass all the command line arguments separated by a space, but if argument
itself has a space then you can pass such arguments by putting them inside
double quotes " " or single quotes ' '.

You might also like