Vous êtes sur la page 1sur 21

File handling functions

Description

fopen () fopen () function creates a new file or opens an existing file.

fclose () fclose () function closes an opened file.

getw () getw () function reads an integer from file.

putw () putw () functions writes an integer to file.

fgetc () fgetc () function reads a character from file.

fputc () fputc () functions write a character to file.

gets () gets () function reads line from keyboard.

puts () puts () function writes line to o/p screen.

fgets () fgets () function reads string from a file, one line at a time.

fputs () fputs () function writes string to a file.

feof () feof () function finds end of file.

fgetchar () fgetchar () function reads a character from keyboard.

fprintf () fprintf () function writes formatted data to a file.

fscanf ()fscanf () function reads formatted data from a file.

fputchar () fputchar () function writes a character onto the output screen from keyboard input.

fseek () fseek () function moves file pointer position to given location.

SEEK_SET SEEK_SET moves file pointer position to the beginning of the file.

SEEK_CUR SEEK_CUR moves file pointer position to given location.

SEEK_END SEEK_END moves file pointer position to the end of file.

ftell () ftell () function gives current position of file pointer.

rewind () rewind () function moves file pointer position to the beginning of the file.

getc () getc () function reads character from file.

getch () getch () function reads character from keyboard.


getche () getche () function reads character from keyboard and echoes to o/p screen.

getchar () getchar () function reads character from keyboard.

putc () putc () function writes a character to file.

putchar () putchar () function writes a character to screen.

printf () printf () function writes formatted data to screen.

sprinf () sprinf () function writes formatted output to string.

scanf () scanf () function reads formatted data from keyboard.

sscanf () sscanf () function Reads formatted input from a string.

remove () remove () function deletes a file.

fflush () fflush () function flushes a File.

File operation

Declaration & Description

putw() Declaration: int putw(int number, FILE *fp);

putw function is used to write an integer into a file. In a C program, we can write integer value in a file as
below.

putw(i, fp);

where

i – integer value

fp – file pointer

getw() Declaration: int getw(FILE *fp);

getw function reads an integer value from a file pointed by fp. In a C program, we can read integer value
from a file as below.
getw(fp);

EXAMPLE PROGRAM FOR PUTW(), GETW() FUNCTION IN C PROGRAMMING LANGUAGE:

This file handling C program illustrates how to write into a file using putw() function and how to read the
same file using getw() function.

#include <stdio.h>

int main ()

FILE *fp;

int i=1, j=2, k=3, num;

fp = fopen ("test.c","w");

putw(i,fp);

putw(j,fp);

putw(k,fp);

fclose(fp);

fp = fopen ("test.c","r");

while(getw(fp)!=EOF)

num= getw(fp);
printf(“Data in test.c file is %d \n”, num);

fclose(fp);

return 0;

10

11

12

13

14

15

16

17

18

19

#include <stdio.h>
int main ()

FILE *fp;

int i=1, j=2, k=3, num;

fp = fopen ("test.c","w");

putw(i,fp);

putw(j,fp);

putw(k,fp);

fclose(fp);

fp = fopen ("test.c","r");

while(getw(fp)!=EOF)

num= getw(fp);

printf(“Data in test.c file is %d \n”, num);

fclose(fp);

return 0;

File operation

Declaration & Description


fgetc() Declaration: int fgetc(FILE *fp)

fgetc functions is used to read a character from a file. It reads single character at a time. In a C program,
we use fgetc() function as below.

fgetc (fp);

where,

fp – file pointer

/* Open, Read and close a file: Reading char by char */

# include <stdio.h>

int main( )

FILE *fp ;

char c ;

printf( "Opening the file test.c in read mode" ) ;

fp = fopen ( "test.c", "r" ) ; // opening an existing file

if ( fp == NULL )

printf ( "Could not open file test.c" ) ;

return 1;

printf( "Reading the file test.c" ) ;

while ( 1 )

c = fgetc ( fp ) ; // reading the file


if ( c == EOF )

break ;

printf ( "%c", c ) ;

printf("Closing the file test.c") ;

fclose ( fp ) ; // Closing the file

return 0;

File operation

Declaration & Description

fputc() Declaration: int fputc(int char, FILE *fp)

fputc functions is used to write a character into a file. In a C program, we use fputc() function as below.

fputc(ch, fp);

where,

ch – character value

fp – file pointer

#include <stdio.h>

int main()

char ch;

FILE *fp1;

FILE *fp2;
/* Assume this test1.c file has some data.

For example “Hi, How are you?” */

if (fp1 = fopen("test1.c", "r"))

ch = getc(fp1);

// Assume this test2.c file is empty

fp2 = fopen("test2.c", "w+")

while (ch != EOF)

fputc(ch, fp2);

ch = getc(fp1);

fclose(fp1);

fclose(fp2);

return 0;

return 1;

File operation

Declaration & Description

fopen() Declaration: FILE *fopen (const char *filename, const char *mode)

fopen() function is used to open a file to perform operations such as reading, writing etc. In a C program,
we declare a file pointer and use fopen() as below. fopen() function creates a new file if the mentioned
file name does not exist.
FILE *fp;

fp=fopen (“filename”, ”‘mode”);

Where,

fp – file pointer to the data type “FILE”.

filename – the actual file name with full path of the file.

mode – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+ and a+. Please
refer below the description for these mode of operations.

fclose() Declaration: int fclose(FILE *fp);

fclose() function closes the file that is being pointed by file pointer fp. In a C program, we close a file as
below.

fclose (fp);

gets() Declaration: char *gets (char *string)

gets functions is used to read the string (sequence of characters) from keyboard input. In a C program,
we can read the string from standard input/keyboard as below.

gets (string);

fputs() Declaration: int fputs (const char *string, FILE *fp)

fputs function writes string into a file pointed by fp. In a C program, we write string into a file as below.

fputs (fp, “some data”);

/ * Open, write and close a file : */

# include <stdio.h>

# include <string.h>
int main( )

FILE *fp ;

char data[50];

// opening an existing file

printf( "Opening the file test.c in write mode" ) ;

fp = fopen("test.c", "w") ;

if ( fp == NULL )

printf( "Could not open file test.c" ) ;

return 1;

printf( "\n Enter some text from keyboard” \

“ to write in the file test.c" ) ;

// getting input from user

while ( strlen ( gets( data ) ) > 0 )

// writing in the file

fputs(data, fp) ;

fputs("\n", fp) ;

// closing the file

printf("Closing the file test.c") ;

fclose(fp) ;

return 0;
}

File operation

Declaration & Description

feof() Declaration: int feof(FILE *fp)

feof functions is used to find the end of a file. In a C program, we use feof() function as below.

feof(fp);

where,

fp – file pointer

/* Open, Read and close a file: Reading char by char */

# include <stdio.h>

int main( )

FILE *fp ;

char c ;

printf( "Opening the file test.c in read mode" ) ;

fp = fopen ( "test.c", "r" ) ; // opening an existing file

if ( fp == NULL )

printf ( "Could not open file test.c" ) ;

return 1;

}
printf( "Reading the file test.c" ) ;

while ( 1 )

c = fgetc ( fp ) ; // reading the file

if( feof(fp) )

break ;

printf ( "%c", c ) ;

printf("Closing the file test.c as end of file is reached.");

fclose ( fp ) ; // Closing the file

return 0;

ftell() Declaration: long int ftell(FILE *fp)

ftell function is used to get current position of the file pointer. In a C program, we use ftell() as below.

ftell(fp);

#include <stdio.h>

int main ()

char name [20];

int age, length;

FILE *fp;

fp = fopen ("test.txt","w");
fprintf (fp, "%s %d", “Fresh2refresh”, 5);

length = ftell(fp); // Cursor position is now at the end of file

/* You can use fseek(fp, 0, SEEK_END); also to move the

cursor to the end of the file */

rewind (fp); // It will move cursor position to the beginning of the file

fscanf (fp, "%d", &age);

fscanf (fp, "%s", name);

fclose (fp);

printf ("Name: %s \n Age: %d \n",name,age);

printf ("Total number of characters in file is %d", length);

return 0;

File operation

Declaration & Description

fseek() Declaration: int fseek(FILE *fp, long int offset, int whence)

fseek() function is used to move file pointer position to the given location.

where,

fp – file pointer

offset – Number of bytes/characters to be offset/moved from whence/the current file pointer position

whence – This is the current file pointer position from where offset is added. Below 3 constants are used
to specify this.

SEEK_SET SEEK_SET – It moves file pointer position to the beginning of the file.

SEEK_CUR SEEK_CUR – It moves file pointer position to given location.


SEEK_END SEEK_END – It moves file pointer position to the end of file.

#include <stdio.h>

int main ()

FILE *fp;

char data[60];

fp = fopen ("test.c","w");

fputs("Fresh2refresh.com is a programming tutorial website", fp);

fgets ( data, 60, fp );

printf(Before fseek - %s", data);

// To set file pointet to 21th byte/character in the file

fseek(fp, 21, SEEK_SET);

fflush(data);

fgets ( data, 60, fp );

printf("After SEEK_SET to 21 - %s", data);

// To find backward 10 bytes from current position

fseek(fp, -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(fp, -7, SEEK_END);


fflush(data);

fgets ( data, 60, fp );

printf("After SEEK_END to -7 - %s", data);

// To set file pointer to the beginning of the file

fseek(fp, 0, SEEK_SET); // We can use rewind(fp); also

fclose(fp);

return 0;

File operation

Declaration & Description

putw() Declaration: int putw(int number, FILE *fp);

putw function is used to write an integer into a file. In a C program, we can write integer value in a file as
below.

putw(i, fp);

where

i – integer value

fp – file pointer

getw() Declaration: int getw(FILE *fp);

getw function reads an integer value from a file pointed by fp. In a C program, we can read integer value
from a file as below.

getw(fp);
#include <stdio.h>

int main ()

FILE *fp;

int i=1, j=2, k=3, num;

fp = fopen ("test.c","w");

putw(i,fp);

putw(j,fp);

putw(k,fp);

fclose(fp);

fp = fopen ("test.c","r");

while(getw(fp)!=EOF)

num= getw(fp);

printf(“Data in test.c file is %d \n”, num);

fclose(fp);

return 0;

}
File operation

Declaration & Description

fflush() Declaration: int fflush(FILE *fp)

fflush() function is used to flush/clean the file or buffer. In a C program, we can use fflush() function as
below.

fflush(buffer);

where, buffer is a temporary variable or pointer which loads/points the data.

#include <stdio.h>

int main()

char buf[50];

FILE *fp;

fp = fopen("test.txt", "r+");

if (fp)

fputs("Test data by Fresh2refres", fp);

fflush(buf); // flushes the buffer to load the data from file

fgets(buf, 20, fp); // It loads 1st 20 characters from file to buffer

puts(buf); // It displays buffer data in output screen

fclose(fp);

return 0;

return 1;

}
File operation

Declaration & Description

sscanf() Declaration: int sscanf(const char *string, const char *format, …)

sscanf() function is used to read formatted input from a string. In a C program, we can use sscanf()
function as below.

sscanf (buffer,”%s %d”,name,&age);

where,

buffer – buffer which has formatted data. Above is just for example only. Buffer can have any type of
formatted data within it.

/* sscanf example */

#include <stdio.h>

int main ()

char buffer[30]="Fresh2refresh 5 ";

char name [20];

int age;

sscanf (buffer,"%s %d",name,&age);

printf ("Name : %s \n Age : %d \n",name,age);

return 0;

File operation

Declaration & Description


sprintf()Declaration: int sprintf(char *string, const char *format, …)

sprintf function is used to write formatted output to the string, In a C program, we use fgets function as
below.

sprintf ( string, “%d %c %f”, value, c, flt ) ;

where,

string – buffer to put the data in.

value – int variable, c – char variable and flt – float variable. There are for example only. You can use any
specifiers.

#include <stdio.h>

#include <string.h>

int main( )

int value = 50 ;

float flt = 7.25 ;

char c = 'Z' ;

char string[40] = {‘\0’} ;

printf ( "int value = %d \n char value = %c \n " \

"float value = %f", value, c, flt ) ;

/*Now, all the above values are redirected to string

instead of stdout using sprint*/

printf(“\n Before using sprint, data in string is %s”, string);

sprintf ( string, "%d %c %f", value, c, flt );


printf(“\n After using sprint, data in string is %s”, string);

return 0;

File operation

Declaration & Description

fputchar() Declaration: int fputchar(int c)

fputchar() function is used to write a character on standard output/screen. In a C program, we can use
fputchar function as below.

fputchar(char);

where, char is a character variable/value.

#include <stdio.h>

int main ()

char ch='a';

while(ch <= 'z')

fputchar(c);

ch++;

return 0;

Vous aimerez peut-être aussi