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

Friday, October 16, 2020

, ,

C program for ferror() function

 ferror() function in C Programming language is used to identify the error in given c file. 

C program for ferror() function
C program for ferror()

 

In this tutorial you will learn about how to use the ferror() function and programing syntax.

 Prototype :-  

 int ferror(FILE *filename);

Parameters :-

FILE *filename

C program for ferror() function

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

int main()
{
	FILE *fr;
	char str[100];

	if((f=fopen("help.txt","r"))==NULL){
		printf("Cannot open the file...");
		//file is not exist program is terminated
		exit(1);
	}

	// Check if here is some error in the file
	if(ferror(f))
		printf("print the Error to read the file\n");
	else	
		printf("No error\n");

	printf("File content is--\n");
	//print the strings until EOF is encountered
	while(!feof(fr)){
		fgets(str,100,fr);
		//print the string
		printf("%s",str);
	}

	//remember to close the opened file
	fclose(f);

	return 0;
}

Wednesday, October 14, 2020

,

System() function program in c

 In System library function in c use the command int system(const char *command) name or program name to the host environment and also executed command processor and returns the command after complete the task that is given in the c program.

 Parameters For System() function program in c :

Command:-  This string in c program containing the name of requested variable.  
 
Return the status of the command at the success and  returned is -1 on error.
 
Example:-

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main () { char command[50]; strcpy( command, "-l" ); system(command); return(0); }


Example:-

This function list down the dir file with the help of c program

#include <stdio.h>
#include <string.h>

int main () {
   char command[50];

   strcpy( command, "dir" );
   system(command);

   return(0);
} 


system program in c
system program in c

Sunday, October 11, 2020

, ,

Example Program For Lab() Function In C Programming

 Lab() Function in c programming gives the absolute value of the integer and it return the absolute value of the long integer argument.

Showing program will tell how you can use the LAB function in c programming.

Example Program For  Lab() Function In C Programming
Example Program For  Lab() Function In C Programming

 

Parameters:-

Y − is a integral value. 

Return Value:-

This lab function returns the absolute value of y. 

LAB Function Program Example :-

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

int main () {
   long int e,d;

   e = labs(64887L);
   printf("Value for e = %ld\n", a);

   d = labs(-1003080L);
   printf("Value for d = %ld\n", b);
   
   return(0);
}

OUTPUT:-

Value for e = 64887
Value for d = 1003080