Computer Programming C: UNIT V: File Operations

getw, putw, fprintf and fscanf C Programming functions

These are integer‒oriented functions. They are similar to getc and putc functions and are used to read and write integer values.

THE getw AND putw FUNCTIONS

These are integer‒oriented functions. They are similar to getc and putc functions and are used to read and write integer values. These functions would be useful when we deal with only integer data.

Syntax:

putw(integer,fp);

getw(fp);

/*Example program for using getw and putw functions*/

#include <stdio.h>

#include <conio.h>

main()

{

FILE *fl, *f2, *f3;

int n, i;

clrscr();

printf("Contents of the data file\n\n");

fl=fopen("DATA", "w");

for(i=1;i < = 10;i++)

{

         scanf("%d", &n);

         if(n ==‒1)  break;

         putw (n,fl);

}

fclose(fl);

fl=fopen("DATA", "r");

f2=fopen("ODD", "w");

f3=fopen("EVEN", "w");

while((n=getw (f1))!= EOF) * Read from data file*!

{

         if(n%2==0) putw(n,f3); /*Write to even file*/

         else putw (n,f2);  /*write to odd File*/

}

fclose(fl);

fclose(f2);

fclose(f3);

f2=fopen("ODD", "r");

f3=fopen("EVEN", "r");

printf("\n\n Contents of the odd file\n\n");

while((n=getw (f2))!= EOF) printf("%d\t",n);

printf("\n\nContents of the even file\n");

while((n=getw (f3))!= EOF) printf("%d\t",n);

fclose(f2);

fclose(f3);

}

OUTPUT

Contents of the data file

1 2 3 4 5 6 7 8 9 10

Contents of the odd file

1 3 5 7 9

Contents of the even file

2 4 6 8 10

 

THE fprintf & fscanf FUNCTIONS

The fprintf and fscanf functions are similar to printf and scanf functions except that they work on files. The first argument of theses functions is a file pointer which specifies the file to be used.

Syntax :

fprintf(fp, "control string", list);

Description:

Fp is a file pointer associated with a file that has been opened for writing.

control string is file output specifications list may include variable, constant and string.

Example:

fprintf(fl,"%s %d", name, &age);

Where name is an array variable of type char and age is an int variable.

Syntax:

fscanf(fp, "control string", list);

Description:

fp is a file pointer associated with a file that has been opened for writing.

control string is file output specifications list may include variable, constant and string.

Example:

fscanf(f2,"%d %d", &item, & qty");

Where item is an int variable and qty is an int variable

This statement would cause the reading of data items from the file in the control string. Like scanf, fscanf also returns the number of items that are successfully read.

/*Program to handle mixed data types */

#include <stdio.h>

#include <conio.h>

main()

{

FILE *fp;

int num, qty, i;

float price, value;

char filename [10];

printf("Input filename....");

scanf("%s", filename);

fp=fopen(filename, "w");

printf("\nInput inventory data");

printf("Item number price quantity \n");

for(i=1;i<= 2;i++)

{

        fscanf(stdin, "%d %f%d", &num, & price, &qty);

        fprintf(fp, "%d %f%d", num, price, qty);

}

fclose (fp);

fp=fopen(filename, "r");

printf("\nItem number price quantity value\n");

fflush(stdin);

fflush(stdout);

for(i=1;i<= 2;i++)

{

        fscanf(fp, "%d %f%d", &num, & price, & qty);

        value=price*qty;

        fprintf(stdout, "%d\t%f\t%d\t%f\t\n", num, price, qty, value);

}

fclose(fp);

}

OUTPUT

Input filename...: vrb

Input inventory data Item number price quantity

1 10 10

2 20 20

Item number price quantity value

1                  10      10          100

2                  20      20          200

 

Computer Programming C: UNIT V: File Operations : Tag: Computer Science : - getw, putw, fprintf and fscanf C Programming functions


Computer Programming C: UNIT V: File Operations



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation