Tuesday, October 27, 2020

,

Example program for tmpfile() function in c programming

  •  tmpfile() function in c programming is used to created the temporary file in program and this temporary file will be deleted after  termination of c program.
  • wb+ mode is used to opens file in binary update 
Example program for tmpfile() in c
Example program for tmpfile() function in c programming

 

Syntax :- 

FILE *tmpfile(void) 
  • this function return the file after creating the temporary file.

Program example for tmpfile() function in c


#include <stdio.h>
int main() 
{ 
    char str[] = "Hello function in c"; 
    int i = 0; 
  
    FILE* tmp = tmpfile(); 
    if (tmp == NULL) 
    { 
        puts("Unable to create temp file"); 
        return 0; 
    } 
  
    puts("Temporary file is created\n"); 
    while (str[i] != '\0') 
    { 
        fputc(str[i], tmp); 
        i++; 
    } 
  
    // rewind() function sets the file pointer 
    // at the beginning of the stream. 
    rewind(tmp); 
  
    while (!feof(tmp)) 
        putchar(fgetc(tmp)); 
} 
OUTPUT : -

Temporary file is created
Hello function in c

Monday, October 26, 2020

, ,

Example Program for setbuf Function in c

 setbuf() function in c programming is used to define how stream should be buffered and this function should be call when a file associated with the stream and a already open file but any input or output shouldn't take place.

Example Program for setbuf Function in c
Example Program for setbuf Function in c

 

Parameter for setbuf() function program

steam is an pointer in this program and a allocated buffer is used.

This function doesn't return any thing.

Example program for setbuf() :-

#include <stdio.h>

int main () {
   char buf[BUFSIZ];

   setbuf(stdout, buf);
   puts("This is functioinc.in");

   fflush(stdout);
   return(0);
}

OUTPUT

This is functioinc.in

Sunday, October 25, 2020

,

Example program for ftell function in c

 ftell() function in c is used to get the total size of file after move the file pointer to the end of the file and it return the current position in long type.

File can have more than 32767 bytes of data

Syntax : -

long int ftell(FILE *stream)


Let's Check out the Program for ftell() function in c programming.

 

#include <stdio.h>
#include <conio.h>
void main () {
   FILE *f;
   int len;
   f = fopen("one.txt", "r");
   if(f == NULL) {
      perror(“Error opening file”);
      return(-1);
   }
   fseek(f, 0, SEEK_END);
   len = ftell(f);
   fclose(f);
   printf("Size of file: %d bytes", len);
   getch();
}


OUTPUT For ftell() function program

ftell() function
ftell() in c

Saturday, October 24, 2020

, ,

Example program for fsetpos() function in C programming

 In c handling, fsetpos() function will be used to set the position of input file with the help of fsetpos() access that point.

If you use to fix the file indicator position at any time in the given file then we need to use the fsetpos() function.

 Return Value :- fsetpos() function return zero at the time of success or non zero at failure. 

Example program for fsetpos()
Example program for fsetpos() function in C programming

 

Example program for fsetpos() function

#include <stdio.h>
#include <stdlib.h>

int main(){
	
	FILE *f;
	
	char ch[100];
	
	fpos_t pos;

	
	f=fopen("functioninc.txt","w+");
	
	fgetpos(f,&pos);
	printf("Enter any five strings\n");
	for(int i=0;i<4;i++){
		
		scanf("%[^\n]",&ch);
		//write back to the file
		fputs(ch,f);
		
		fputs("\n",f);
		
		fflush(stdin);
	}
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);
	
	fsetpos(f,&pos);
	printf("\n...............print the strings..............\n\n");
	while(!feof(f)){
		
		fgets(ch,100,f);
		
		printf("%s",ch);
	}

	//close the file
	fclose(f);

	return 0;
}<4 0="" 100="" a="" after="" and="" array="" back="" buffer="" ch="" character="" clear="" close="" don="" entry.otherwise="" entry="" every="" except="" f="" fclose="" feof="" fflush="" fgets="" file="" first="" for="" fputs="" from="" fsetpos="" i="" if="" in="" indicator="" initial="" is="" last="" line="" n...............print="" n="" new="" of="" or="" pos="" position="" pre="" print="" printf="" return="" s="" scanf="" set="" space="" stdin="" stream="" string="" strings..............="" strings="" t="" take="" takes="" taking="" the="" then="" this="" time="" to="" twice="" users="" waiting="" we="" while="" white="" write="">



,

Example Program for FSEEK() function in c programming

 fseek() function in c programming is used to write data into desired file with the help of pointer which help to find the location into the given file.

Example Program for FSEEK()
Example Program for FSEEK() function in c programming

 

Syntax :-

int fseek(FILE *stream, long int offset, int whence)  

There are three constants are used in fseeks() function in c  SEEK_SET, SEEK_CUR and SEEK_END


 #include <stdio.h>
void main(){  
   FILE *fp;  
  
   fp = fopen("myfile.txt","w+");  
   fputs("This is functioinc", fp);  
    
   fseek( fp, 7, SEEK_SET );  
   fputs("Adam stiffman", fp);  
   fclose(fp);  
} 

Now let see the Example Program for FSEEK() function in c

#include <stdio.h>
int main ()
{
   FILE *f;
   char data[60];
   fp = fopen ("test.c","w");
   fputs("functioinc.in is a programming tutorial website", f);
   fgets ( data, 60, fp );
   printf(Before fseek - %s", data); 
 
   // To set file pointet to 21th byte/character into the file
   fseek(f, 21, SEEK_SET);
   fflush(data);
   fgets ( data, 60, fp );
   printf("After SEEK_SET to 21 - %s", data);
 
   // To find backward 10 bytes 
   fseek(f, -10, SEEK_CUR);
   fflush(data);
   fgets ( data, 60, fp );
   printf("After SEEK_CUR to -10 - %s", data);
 
   // To find 7th byte before the end of file
   fseek(f, -7, SEEK_END); 
   fflush(data);
   fgets ( data, 60, f );
   printf("After SEEK_END to -7 - %s", data);
 
   // now set file pointer to the start of the file
   fseek(f, 0, SEEK_SET); 
 
   fclose(f);
   return 0;
}

Friday, October 23, 2020

, , ,

Example program for fprintf() function in c

 

  •  The fprintf() function in c is used to write the formatted data into the file.
  • This didn't print the data on the console.
  • All the arguments of function fprintf() is same as the printf() but fprintf() has an extra arguments which is pointer that is used to find the location where the formatted data is written in a file.

 Return value for fprintf() function:- This function return the total number of characters written to the file.

On the other hand it return the EOF when any error occurred. 

Program for fprintf() function in c : -  


#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f;
    char Name[50];
    int roll_num, chars, i, n;
    float mark;

    fp = fopen("records.txt", "w");

    if(f == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Testing fprintf() function: \n\n");

    printf("Enter the number of records you want to enter: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++)
    {
        fflush(stdin);
        printf("\nEnter the details of student %d \n\n", i +1);

        printf("Enter name of the student: ");
        gets(name);

        printf("Enter roll no: ");
        scanf("%d", &roll_num);

        printf("Enter marks: ");
        scanf("%f", &mark);

        chars = fprintf(f, "Name: %s\t\tRoll no: %d\t\tMarks: %.2f\n",
            Name, roll_num, mark);
       printf("\n%d characters successfully written to the file\n\n", chars);
    }

    fclose(f);
    return 0;
}

 Output fprintf() program: -

 

Example Program For fprintf() function in c
Example Program For fprintf() function in c

Wednesday, October 21, 2020

, ,

List of function program in c programming language

 Well as all of you can see in previous tutorial of function in c programming we try to cover all in-build function in c not just only c but also other programming language like python, javascript, SQL, and other languages.

function program in c programming language
List of function program in c programming language

 

 

In this tutorial you can find all the function program in c and also learn about HOW TO USE IN-BUILD FUNCTION IN C with program example with explanation for these programs.

Check out our list of function program in c