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 

Tuesday, October 20, 2020

, ,

How to use the fflush in c programming with example

 fflush() function in c programming is use to  flushes the contents of output to given file.

Understand the fflush function:-

To discuses the common issue due to the output buffering after the execute the c program.

#include <stdio.h>
 
int main() {
    fprintf(stdout, "This is to stdout. ");
    fprintf(stderr, "This is to stderr. ");
    fprintf(stdout, "This is also to stdout. ");
}
Out for this code snippet comes in different order.

fflush function program in c
fflush function program in c


The reason behind for this random order is buffered of any file in computer.

Well by default stdout is newline-buffered and on the other hand stderr is unbuffered

For stdout the content will be stored inside the temporary buffer so the output will be displayed when counters a newline or end of the file.

 On the other hand  stderr is not buffered the out will be written immediately.

That's why the stderr will be displayed first in upper program.

But if you want to displayed the standout output first the you have to flush the output with the fflush program in c.

#include <stdio.h>
 
int main() {
    fprintf(stdout, "This is to stdout. ");
    // output stream immediately
    fflush(stdout);
    fprintf(stderr, "This is to stderr. ");
   
    fprintf(stdout, "This is also to stdout. ");
    // output stream immediately
    fflush(stdout);
}

Output Of fflush program in c


Output of fflush program
Output

Monday, October 19, 2020

, , ,

Example program for fscanf function in c

 fscanf() function in c programming is used to read input from a file. 

Well this function works same as the scanf() function in c programming but instead of read the given input in c program fscanf() function read the data from file.

Most of the arguments of fscanf() function in c are same as scanf() function.

  •  Return Value For fscanf() function program in c:-

This function return the number of value and complete the read operation or return the end of file EOF and -1 on error occurred.

 

Example program for fscanf() function in c


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

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

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

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

    printf("Testing fscanf() function: \n\n");
    printf("Name:\t\tRoll\t\tMarks\n");

    while( fscanf(f, "Name: %s\t\tRoll num: %d\t\tMarks: %f\n"
                    , Name, &roll_num, &mark) != EOF )
    {
        printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
    }

    fclose(f);
    return 0;
}


Output of c program
Output of c program