file management in c programming
File management in c programming...
- In C support a numbers of function that have the ability to perform basic file operations, in which include:
- operations performed in the file handling.
1. Naming a file
2. Opening a file
3. Reading from a file
4. Writing data into a file
5. Closing a file
File operation functions in C...
files and Operation
- fopen()- Creates a new file for use or Opens a new existing file .
- fclose- Closes a file opened for use
- getc()- Reads a character from a file
- putc()-Writes a character in a file
- fprintf()-Write a set of data value to a file
- fscanf()- Reads a sets of data values from a file
- getw()- Read a integer from a file
- putw()- Write an integer to the file
- fseek()- Sets the position to a desire point in the file
- ftell()-Give the current position in the file
- rewind()- Sets the position to the beginingg of the file
- Defining and opening a file:
- here If we want to store data in a file into the secondary memory, we must specifys certain things about the file to the operating system. They include the fielname, data structure, purpose.
Opening a file in c...
FILE *fp;
fp=fopen(“filename”,”mode”);
- 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 functon .
- second statement opens the file named filename and assign an identifier to the FILE type pointer fp.
- This pointer, in which contains all the information about the file, is subsequently used as a communication links between the system and the program.
- second statement also specifies the purpose of opening the file.
R open the file for read only.
W open the file for writing only.
A open the file for appending data to it.
statements:
FILE *p1, *p2;
p1=fopen(“data”,”r”);
p2=fopen(“results”,”w”);
- this statements the p1 and p2 are created and assigne to open the files data and results respectively the file data is opened for reading and results is opened for writing.
- In case the results file already exists, its contents are deleted and the files are opened as a new file. If data file does not exist error will occurss.
Closing a file in c...
The input output library supports the function to close a file;
Syntax;
fclose(file_pointer);…
- In c A file must be closed as soon as all operations on it have been completed.
- This would close the file associated with the file pointers.
look at the following program.
….
FILE *p1 *p2;
p1=fopen (“Input”,”w”);
p2=fopen (“Output”,”r”);
….
fclose(p1);
fclose(p2)
- In The above program opens two files and closes them after all operations on them are completed, once a file is closed its file pointer can be reversed on other file.
- The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time.
- The putc function writes the character contained in character variable c to the file associated with the pointer fp1. ex putc(c,fp1); similarly getc function is used to read a character from a file that has been open in read mode. c=getc(fp2).
- The program shown below displays use of a file operations.
- The data enter through the keyboard and the program writes it.
- Character by character, to the file input.
- The end of the data is indicated by entering an EOF character, which is control-z. the file input is closed at this signal.
#include< stdio.h >
main()
{
file *f1;
printf(“Data input output”);
f1=fopen(“Input”,”w”); /*Open the file Input*/
while((c=getchar())!=EOF) /*get a character from key board*/
putc(c,f1); /*write a character to input*/
fclose(f1); /*close the file input*/
printf(“nData outputn”);
f1=fopen(“INPUT”,”r”); /*Reopen the file input*/
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f1);
}
getw and putw functions in c...
- getw and putw are integer-oriented functions.
- These are similar to get c and putc functions and are use to read and write integer values. These functions would be useful when we deal with only integer data.
- In The general forms of getw and putw are:
putw(integer,fp);
getw(fp);
/*Example program for using getw and putw functions*/
#include< stdio.h >
main()
{
FILE *f1,*f2,*f3;
int number I;
printf(“Contents of the data filenn”);
f1=fopen(“DATA”,”W”);
for(I=1;I< 30;I++)
{
scanf(“%d”,&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen(“DATA”,”r”);
f2=fopen(“ODD”,”w”);
f3=fopen(“EVEN”,”w”);
while((number=getw(f1))!=EOF)/* Read from data file*/
{
if(number%2==0)
putw(number,f3);/*Write to even file*/
else
putw(number,f2);/*write to odd file*/
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen(“ODD”,”r”);
f3=fopen(“EVEN”,”r”);
printf(“nnContents of the odd filenn”);
while(number=getw(f2))!=EOF)
printf(“%d%d”,number);
printf(“nnContents of the even file”);
while(number=getw(f3))!=EOF)
printf(“%d”,number);
fclose(f2);
fclose(f3);
}
fprintf & fscanf functions in c....
- The fprintf and fscanf function are identical to printf and scanf functions except that they woork on files.
Syntax;
fprintf(fp,”control string”, list);
- fp id a file pointer associated with a file that has been opened for writing.
- The control string is file output specifications list may include variable, constant and string.
fprintf(f1,%s%d%f”,name,age,7.5);
Here, name is an array variable of type char and age is an int variable
The general format of fscanf is
fscanf(fp,”controlstring”,list);
This statement would cause the reading of items in the control string.
Example:
fscanf(f2,”5s%d”,item,&quantity”);
Like scanf, fscanf also returns the number of items that are successfully read.
/*Program to handle mixed data types*/
#include< stdio.h >
main()
{
FILE *fp;
int num,qty,I;
float price,value;
char item[10],filename[10];
printf(“Input filename”);
scanf(“%s”,filename);
fp=fopen(filename,”w”);
printf(“Input inventory datann”0;
printf(“Item namem number price quantityn”);
for I=1;I< =3;I++)
{
fscanf(stdin,”%s%d%f%d”,item,&number,&price,&quality);
fprintf(fp,”%s%d%f%d”,itemnumber,price,quality);
}
fclose (fp);
fprintf(stdout,”nn”);
fp=fopen(filename,”r”);
printf(“Item name number price quantity value”);
for(I=1;I< =3;I++)
{
fscanf(fp,”%s%d%f%d”,item,&number,&prince,&quality);
value=price*quantity”);
fprintf(“stdout,”%s%d%f%d%dn”,item,number,price,quantity,
value);
}
fclose(fp);
}
Random access to files in c...
- Sometimes it is required to access only a particular part of the and not the completed file
- .This can be accomplished by using the following function.
1 > fseek
fseek function in c...
- The general format of fseek function is a s follows:
fseek(file pointer,offset, position);
- This function is used to move the file position to a desired location within the file.
- Fileptr is a pointer to the file concerned. Offset is a number or variable of type long, and position in an integer number.
- Offset specifies the number of positions (bytes) to be moved from the location specified bt the position.
- The position can take the 3 values.
Value Meaning
0 Beginning of the file
1 Current position
2 End of the file.


No comments:
Post a Comment