We regularly utilize files for putting away data which can be handled by our projects. In this c programming tutorialwe are using file structure in our c programs. With a specific end goal to store data forever and recover it we have to utilize files.
Files in C programming are not only used for data reading but also for data writing, manipulation and many more option thanks to C programming built in functions. Our C programs are also stored in files.
Note : Stdio.h library should be added in order to use file handling functions such as basic input output functions in C programming language.
Following are the file handling function that could be used in C programming language
Functions Used In C Programming with Description
fopen() --- New file creation or open an existing file in c programming.
fclose() --- Close a file in c programming.
getc() ---- Read character from a file.
putc() ---- Writes a character to a file.
fscanf() --- Reads specific set of data from a file.
fprintf() -- Writing specific set of data to a file.
getw() --- Reads an integer from a file.
putw() --- Writes an integer to a file.
fseek() --- Setting the location to required point.
ftell() ---- returns current location in the file.
rewind --- Set the location to the beginning of point in c programming.
fopen() --- New file creation or open an existing file in c programming.
fclose() --- Close a file in c programming.
getc() ---- Read character from a file.
putc() ---- Writes a character to a file.
fscanf() --- Reads specific set of data from a file.
fprintf() -- Writing specific set of data to a file.
getw() --- Reads an integer from a file.
putw() --- Writes an integer to a file.
fseek() --- Setting the location to required point.
ftell() ---- returns current location in the file.
rewind --- Set the location to the beginning of point in c programming.
Modes of operation in c:
Reading (r)
This is used when a file is used for writing
#include <stdio.h> // library
void main()
{
FILE *fopen(), *fp;
int c;
fp = fopen("prog.c","r");
c = getc(fp) ;
while (c!= EOF)
{
putchar(c);
c = getc(fp);
}
fclose(fp);
}
#include <stdio.h> // library
void main()
{
FILE *fopen(), *fp;
int c;
fp = fopen("prog.c","r");
c = getc(fp) ;
while (c!= EOF)
{
putchar(c);
c = getc(fp);
}
fclose(fp);
}
Writing (w)
This is used when a file is opened for writing
FILE *fp;
file = fopen("file.txt","w"); /*Create a file and add text*/
FILE *fp;
file = fopen("file.txt","w"); /*Create a file and add text*/
Appending (a)
This is used when a file is opened for appending
FILE *fp
file = fopen("file.txt","a");
fprintf(fp,"%s","This is just an example :)"); /*append some text*/
This is used when a file is opened for appending
FILE *fp
file = fopen("file.txt","a");
fprintf(fp,"%s","This is just an example :)"); /*append some text*/
0 comments:
Post a Comment