Files
------
A File is a collection of similar records.
Basic Operations on Files
------------------------------
1. Creating a file
2. Opening the file
3. Writing data into the file.
4. Reading data from the file.
5. Closing the file.
Types of Files in terms of storage
---------------------------------------
1. Text Files
2. Binary Files
Types of Files in terms of accessing
------------------------------------------
1. Sequential Files
2. Random Files
File Opening Modes
-----------------------
fopen("studentdata","w");
w- writing
r -reading
a-appending
w+
a+
r+
File Operation Functions
-----------------------------
fputc or putc ----
fgetc or getc
putw
getw
fprintf
fscanf
fclose
fcloseall()
Random Access Functions
rewind
fseek
ftell
/* Prg on creating a file */
#include <stdio.h>
#include <conio.h>
void main()
{
char ch1='c',ch2='i',ch3='v',ch4='i',ch5='l';
FILE *fp;
clrscr();
fp=fopen("civildata.dat","w");
fputc(fp,ch1);
fputc(fp,ch2);
fputc(fp,ch3);
fclose(fp);
fopen("civildata.dat","a");
fputc(fp,ch4);
fputc(fp,ch5);
fclose(fp);
fp=fopen("civildata.dat","r");
ch1=fgetc(fp);
ch2=fgetc(fp);
ch3=fgetc(fp);
ch4=fgetc(fp);
ch5=fgetc(fp);
printf("%c%c%c%c%c",ch1,ch2,ch3,ch4,ch5);
fclose(fp);
getch();
}
output
-------
civil
Functions
------------
A Function is a block of code that has a specific task to do.
Functions are of two types:
1. Standard or Library Functions
2. User-Defined Functions
The Functions which are designed by the user are called User-Defined
Functions(UDF's).
We can define the udf's in the following four models:
1. Function with no arguments and no return value.
2. Function with arguments but no return value.
3. Function with arguments and return value.
4. Function with no arguments but return value.
We can define our own function (udf) with the following syntax:
return-type function-name(arguments)
{
statement(s);
....
....
}
If the return-type is void, means function returns nothing.
If the arguments is void, takes nothing.
1. Function with no arguments and no return value
/* Prg on model 1 of udfs */
#include <stdio.h>
#include <conio.h>
void welcome(void)
{
printf("Welcome to the world of Functions");
}
void main()
{
clrscr();
welcome();
getch();
}
output
--------
Welcome to the world of Functions
2. Function with arguments but no return value
/* Prg on model 2 of User Defined Functions */
#include <stdio.h>
#include <conio.h>
void add(int x,int y)
{
int z;
z=x+y;
printf("The Addition Value is %d\n",z);
}
void main()
{
int a=10,b=20;
clrscr();
add(a,b);
getch();
}