Search This Blog

Translate

c program for file operations


c program for file operations,c programs on file handling, c programs for file handling, c program on file handling, file handling in c programs, file handling using c, c program for creating a file,what is header file in c programming

 

 

 

C programming Explain Various File Related Function in c

Or  Any Two Input Output Functions  For files, 

c programming file function fputc( ) ,  fgetc() , getc() , putc() , fputs( ), fgets() with example ,

Explain Various File Related Function

Any Two Input Output Functions For files C programming  Various File Related Function definition with example

Different File Related Functions as Follows   fputc( ) ,  fgetc() , getc() , putc() , fputs( ),fgets()

fputc():  this function writes a character to a specified file at the current file position and then increment the file position pointer .The putc() macro acts essentially identically to fputc(), but is a macro  that expands in-line.  It may evaluate stream more than once, so arguments given to putc() should not be expressions with potential side effects.

 

Syntax:     intfputc(int c, FILE *fp);

Return:    The fputc function returns an integer representing  the character written. If a write error occurs the error indicator is set and fputc returns EOF. .
/* program to  understand the use of fputc( ) function */
#include
void main()
{
FILE *p;
char ch;
if((p==fopen("myfile.txt","w"))==NULL)
{
printf("this file does not exist\n");
exit()
}
else
{
printf("enter the text")
while((ch==getchar())!=EOF)
fputc(ch,p);
}
fclose(p);
}
 

fgetc() Function :   fgetc() is a character oriented function.this function reads the single character  from a given file and increment the file pointer position. On success it return the character after converting it. it to an int without sign extension. on error it return EOF .

Syntax:  intfgetc(FILE *stream );
Return:  If successful, it  return the next requested object from the stream. Character values are returned as

anunsigned char converted to an int. If the stream is at end-of-file or a read error occurs, then it  returnEOF.
/* program to understand the use of fgetc( ) function */.
#include

void main()
{
int c; /* Character read from the file.           */
  FILE *ptr;/* Pointer to the file. FILE is a structure  defined in */

                                                
ptr = fopen("Ritu.txt","r");
/* Read one character at a time, checking
for the End of File. EOF is defined in   as -1 */
while ((c = fgetc(ptr)) != EOF)
  {
printf("%c",c);     /* O/P the character to the screen   */
  }

fclose(ptr); /* Close the file.                                   */
} 

getc( ) function and putc( ) function  The getc( ) and putc( )  are exactly similar to that of fgetc( ) and fputc( ).The putc function writes the character contained in character variable c to the file associated with the pointer fp1.

Syntax:  intputc (intch, FILE *file)
 ex ..     putc(c,fp1);
Syntax:       intgetc (FILE *file) .
The getc() function takes the file pointer as its argument, reads in the next character from the file and returns that character as a integer or EOF when it reaches the end of the file. The integer returned can easily type cast into a character variable for your use.Thegetc function acts essentially identically to fgetc, but is a macro that expands in-line.

/ Program to Read/Write using getc( ) and putc( ) */
 #include
void main()
{
file *fp1;
char c;
fp1=fopen("myfile.txt","w")
while((c=getchar())!=EOF)
{
putc(c,fp1);
}
fclose(fp1);
fp1=fopen("myfile.txt","r")
while((c=getc(fp1))!=EOF)
{
printf("%c",c)
}
fclose(fp1);

}

fputs( ) function :

Declaration:          char * fputs(const char *str , FILE *stream);
The fputs( ) function writes the content of the string pointed to by str to the specified stream. The null terminator is not written. The fputs( ) function returns the last character written on success and EOF on failure.
/* program to understand the use of fputs( ) */
#include
void main()
{
FILE *fp1;
charstr[100];
fp1=fopen("rajesh.txt","w");
printf("enter the text\n")
if((gets(str)!=NULL))
fputs(str,fp1);
fclose(fp1);}

Declarations of fgets() Function:  char  *fgets(char *str, int n, FILE *fptr);

Here str is the base address where the string is to be stored, n is the number of characters to be stored and fptr is a file pointer of type FILE structure which specifies the name of the file to which the input has to be read .The fgets() function reads characters from the current file pointer position upto and including the first new line character ‘\n’, upto the end of the stream, or until the number of characters read is equal to (n-1), whichever comes first. Now look at the second argument of fgets() function more carefully. This second argument is used to prevent fgets() from reading a too long string and overflowing the array.
 

fgets( ) function  This function is used to read character from a file  and these characters are store in the string pointed to by str . Characters are read until either a newline or an EOF is received or until the specified limit is reached. After the character has been read, a null is stored in the array immediately after the last character is read. A newline character will be retained and will be a part of the array pointed to by str.

/* program to understand the use of fgets( ) */
#include
#include
void main()
{
            FILE *FP1;
            char name[100];
            fp1=fopen("rohit.txt","r");
            printf("enter the data")
                        while(fgets(name,80,fp1)!=NULL)
                        puts(name);
            fclose(fp1);
}
OUTPUT:master ofcomputer application

c program for file operations,c programs on file handling, c programs for file handling, c program on file handling, file handling in c programs, file handling using c, c program for creating a file,what is header file in c programming

C Program example List

Google+ Badge