Files
--> A file is a defined data type used as data structure. A file is known as a string.
--> Files are used to stored data permanently to the external devices such as floppy disc and also to read data from external storage devices.
Operations on files:
* Open the file
* Updating the file
* Closing the file
File Pointers:
File pointer in the internal name or logical name given to the file which is opened for specific purpose.
Syntax:
File *file_pointer_name;
fopen( ):
This function is used to open a file in the specified mode.
Syntax:
File file_pointer_name = fopen("file_name", "mode");
File Modes:
Mode Symbol
Read "r"
Write "w"
Append "a"
Readwrite "r+"
Writeread "w+"
Binarywrite "wb"
Read "rb"
fclose( ):
This function is used to close the opened file.
Syntax:
fclose(file_pointer);
FILE ACCESSING
1. Sequential Accessing
2. Random Accessing
Sequential Accessing Functions:
1) putc( ):
This function is used to write character by character into the file.
Syntax:
putc(character, fp);
2) fputc( ):
This function is used to write character by character into the file.
Syntax:
fputc(character, fp);
3) getc( ):
This function is used to read single character from the file.
Syntax:
character_variable = getc(filepointer);
4) fgetc( ):
This function is used to read single character from the file.
Syntax:
character_variable = fgetc(filepointer);
5) feof( ):
This function is used to indicate end of file. This function returns zero, if the end of file is not reached. This function returns non-zero, if the end of file is reached.
Syntax:
feof(filepointer);
6) fputs( ):
This function is used to write a string into the file.
Syntax:
fputs(string, filepointer);
7) fgets( ):
This function is used to read a string from the file.
Syntax:
fgets(string, no. characters, filepointer);
8) fprintf( ):
This function is used to write formatted input into the file.
Syntax:
fprintf(filepointer, "format specifier", arg1, arg2,...... argn);
9) fscanf( ):
This function is used to read formatted output from the file.
Syntax:
fscanf(filepointer, "format specifier", &arg1, &arg2,...... &argn);
10) fwrite( ):
This function is used to write an entire structure into file.
Syntax:
fwrite(&structure variable_name, sizeof(structure), n, filepointer);
11) fread( ):
This function is used to read a structure from the file.
Syntax:
fread(&structure variable_name, sizeof(structure), n, filepointer);
12) ferror( ):
This function is used to findout error when file read write operation is carried out.
Syntax:
ferror(filepointer);
13) perror( ):
It is a standard library function which prints the error messeges specified by the compiler.
Syntax:
perror(filepointer);
Randoom Accessing Functions:
1) ftell( ):
This function is used to indicate the position of the file pointer. This function returns the long integer value.
Syntax:
ftell(FILE *stream);
2) fseek( ):
This function is used to move the file pointer either forwards or backwards to the specified number of byte the given position.
Syntax:
fseek(FILE *stream, long int offset, int position);
--> Here offset indicates no. of bytes to move from the given position.
--> Here position can take three values
* 0 indicates beginning of the file
* 1 current position
* 2 end of the file
3) rewind( ):
This function is used to remove the file pointer to the starting position of the file.
Syntax:
rewind(filepointer);
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
--> Filename is a string of characters that make up a valid filename for the operating system. It may contain two parts, a primary name and an optional period with the execution.
--> Data structure of a file is defined as FILE in the library of standard Input/output definitions. Therefore, all files should be declared as type FILE before they are used. FILE is a defined data type.
--> When we open a file, we must specify what we want to do with the file.
Declaring a file
FILE *fp;
fp = fopen("filename", "mode");
--> The first statement declares the variable fp as a pointer to the data type FILE.
--> The second statement also specifies the purpose of opening this file.
Closing a file:
After the required operations such as reading, writing or appending operations on a file are done, the file needs to be closed.
Syntax:
fclose(filepointer);
The fclose( ) function has one parameter i.e filepointer for a particular file to be closed. If more than one files are to be closed then more than one fclose( ) function can be written with the respective filepointers.
--> A file is a defined data type used as data structure. A file is known as a string.
--> Files are used to stored data permanently to the external devices such as floppy disc and also to read data from external storage devices.
Operations on files:
* Open the file
* Updating the file
* Closing the file
File Pointers:
File pointer in the internal name or logical name given to the file which is opened for specific purpose.
Syntax:
File *file_pointer_name;
fopen( ):
This function is used to open a file in the specified mode.
Syntax:
File file_pointer_name = fopen("file_name", "mode");
File Modes:
Mode Symbol
Read "r"
Write "w"
Append "a"
Readwrite "r+"
Writeread "w+"
Binarywrite "wb"
Read "rb"
fclose( ):
This function is used to close the opened file.
Syntax:
fclose(file_pointer);
FILE ACCESSING
1. Sequential Accessing
2. Random Accessing
Sequential Accessing Functions:
1) putc( ):
This function is used to write character by character into the file.
Syntax:
putc(character, fp);
2) fputc( ):
This function is used to write character by character into the file.
Syntax:
fputc(character, fp);
3) getc( ):
This function is used to read single character from the file.
Syntax:
character_variable = getc(filepointer);
4) fgetc( ):
This function is used to read single character from the file.
Syntax:
character_variable = fgetc(filepointer);
5) feof( ):
This function is used to indicate end of file. This function returns zero, if the end of file is not reached. This function returns non-zero, if the end of file is reached.
Syntax:
feof(filepointer);
6) fputs( ):
This function is used to write a string into the file.
Syntax:
fputs(string, filepointer);
7) fgets( ):
This function is used to read a string from the file.
Syntax:
fgets(string, no. characters, filepointer);
8) fprintf( ):
This function is used to write formatted input into the file.
Syntax:
fprintf(filepointer, "format specifier", arg1, arg2,...... argn);
9) fscanf( ):
This function is used to read formatted output from the file.
Syntax:
fscanf(filepointer, "format specifier", &arg1, &arg2,...... &argn);
10) fwrite( ):
This function is used to write an entire structure into file.
Syntax:
fwrite(&structure variable_name, sizeof(structure), n, filepointer);
11) fread( ):
This function is used to read a structure from the file.
Syntax:
fread(&structure variable_name, sizeof(structure), n, filepointer);
12) ferror( ):
This function is used to findout error when file read write operation is carried out.
Syntax:
ferror(filepointer);
13) perror( ):
It is a standard library function which prints the error messeges specified by the compiler.
Syntax:
perror(filepointer);
Randoom Accessing Functions:
1) ftell( ):
This function is used to indicate the position of the file pointer. This function returns the long integer value.
Syntax:
ftell(FILE *stream);
2) fseek( ):
This function is used to move the file pointer either forwards or backwards to the specified number of byte the given position.
Syntax:
fseek(FILE *stream, long int offset, int position);
--> Here offset indicates no. of bytes to move from the given position.
--> Here position can take three values
* 0 indicates beginning of the file
* 1 current position
* 2 end of the file
3) rewind( ):
This function is used to remove the file pointer to the starting position of the file.
Syntax:
rewind(filepointer);
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
--> Filename is a string of characters that make up a valid filename for the operating system. It may contain two parts, a primary name and an optional period with the execution.
--> Data structure of a file is defined as FILE in the library of standard Input/output definitions. Therefore, all files should be declared as type FILE before they are used. FILE is a defined data type.
--> When we open a file, we must specify what we want to do with the file.
Declaring a file
FILE *fp;
fp = fopen("filename", "mode");
--> The first statement declares the variable fp as a pointer to the data type FILE.
--> The second statement also specifies the purpose of opening this file.
Closing a file:
After the required operations such as reading, writing or appending operations on a file are done, the file needs to be closed.
Syntax:
fclose(filepointer);
The fclose( ) function has one parameter i.e filepointer for a particular file to be closed. If more than one files are to be closed then more than one fclose( ) function can be written with the respective filepointers.
0 comments:
Post a Comment