[go: up one dir, main page]

0% found this document useful (0 votes)
24 views36 pages

Files

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views36 pages

Files

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

1 Types of Files

2 File Operations

Files
3 File Handling functions

4 File Positions
Text File (or) ASCII File - The file that
contains ASCII codes of data like
File Types
digits, alphabets and symbols is
called text file (or) ASCII file.

Text or ASCII File Binary File

Binary File - The file that contains


data in the form of bytes (0's and 1's)
is called as binary file. Generally, the
binary files are compiled version of
text files.
1 Creating a file

2 Reading a file
Operations
on files 3 Writing a file

4 Closing a file
Creating / Opening
a file

We use the pre-defined method fopen()


To create a new file or open an to create a new file or to open an
existing file, we need to create a existing file.
file pointer of FILE type. There are different modes in which a file
can be opened.

Following is the sample code for


creating file pointer. FILE *f_ptr ;
f_ptr = fopen("abc.txt", "w") ;
File *f_ptr ;
File Modes
r Opens a text file in reading mode.

w Opens a text file in writing mode.

a Opens a text file in append mode.

r+ Opens a text file in both reading and writing mode.

Opens a text file in both reading and writing mode. It set the cursor position to
w+ the beginning of the file if it exists.

Opens a text file in both reading and writing mode. The reading operation is
a+ performed from begining and writing operation is performed at the end of the file.
Note

• The above modes are used with text files only. If we want to work with

binary files we use

• rb, wb, ab, rb+, wb+ and ab+.


1 getc()

2 getw()

Reading from
a file 3 fscanf()

4 fgets()

5 fread()
getc()

• getc( *file_pointer )
• This function is used to read a character from specified
file which is opened in reading mode.
• It reads from the current position of the cursor.
• After reading the character the cursor will be at next
character.
Code for getc()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("a.txt","r");
printf("Reading character from the file: %c\
n",getc(fp));
ch = getc(fp);
printf("ch = %c", ch);
fclose(fp);
getch();
return 0;
}
Output
getw()

• getw( *file_pointer )
• This function is used to read an integer value form the
specified file which is opened in reading mode.
• If the data in file is set of characters, then it reads ASCII
values of those characters.
Code for getw()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int i,j;
fp = fopen("a1.txt","w");
putw(65,fp); // inserts A
putw(97,fp); // inserts a
fclose(fp);
fp = fopen("a1.txt","r");
i = getw(fp); // reads 65 - ASCII value of A
j = getw(fp); // reads 97 - ASCII value of a
printf("SUM of the integer values stored in
file=%d",i+j);//65+97=162
fclose(fp);
return 0;
}
Output
fscanf()

• fscanf( *file_pointer, typeSpecifier, &variableName )


• This function is used to read multiple datatype values
from specified file which is opened in reading mode.
Code for fscanf()
#include<stdio.h>
#include<conio.h>
int main()
{
char str1[10], str2[10], str3[10];
int year;
FILE * fp;
fp = fopen ("file5.txt", "w+");
fputs("We are in 2023", fp);
rewind(fp); // moves the cursor to begining of the file
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
printf("Read String1 - %s\n", str1 );
printf("Read String2 - %s\n", str2 );
printf("Read String3 - %s\n", str3 );
printf("Read Integer - %d", year );
fclose(fp);
return 0;
}
Output
fgets()

• fgets(variableName, numberOfCharacters, *file_pointer )


• This method is used for reading a set of characters from
a file which is opened in reading mode starting from the
current cursor position.
• The fgets() function reading terminates with reading
NULL character.
Code for fgets()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char str[100];
fp = fopen ("file.txt", "r");
fgets(str,6,fp);
printf("str = %s", str);
fclose(fp);
getch();
return 0;
}
fread()

• fread(source, sizeofrElement, noOfChar, FILE *pointer )


• This function is used to read specific number of
sequence of characters from the specified file which is
opened in reading mode.
Code for fread()

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
FILE *fp;
char str[100];
fp = fopen("file.txt","r");
fread(str,sizeof(char),5,fp);
str[strlen(str)+1] = 0;
printf("str = %s", str);
fclose(fp);
return 0;
}
1 putc()

2 putw()

Writing into a
file 3 fprintf()

4 fputs()

5 fwrite()
putc()

• putc( char, *file_pointer )

• This function is used to write/insert a character to the


specified file when the file is opened in writing mode.
Code for putc()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("file1.txt","w");
putc('A',fp);
ch = 'B';
putc(ch,fp);
fclose(fp);
return 0;
}
putw()

• putw( int, *file_pointer )

• This function is used to writes/inserts an integer value to


the specified file when the file is opened in writing mode.
Code for putw()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int i;
fp = fopen("file2.txt","w");
putw(66,fp);
i = 100;
putw(i,fp);
fclose(fp);
return 0;
}
fprintf()

• fprintf( *file_pointer, "text" )

• This function is used to writes/inserts multiple lines of


text with mixed data types (char, int, float, double) into
specified file which is opened in writing mode.
Code for fprintf()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char *text = "\nthis is example text";
int i = 10;
fp = fopen("file3.txt","w");
fprintf(fp,"This is line1\nThis is line2\n%d",i);
fprintf(fp,text);
fclose(fp);
return 0;
}
fputs()

• fputs( "string", *file_pointer )

• This method is used to insert string data into specified


file which is opened in writing mode.
Code for fputs()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char *text = "\nthis is example text";
fp = fopen("file4.txt","w");
fputs("Hi!\nHow are you?",fp);
fclose(fp);
return 0;
}
fwrite()

• fwrite( “StringData”, sizeof(char), noOfChar, FILE *ptr )

• This function is used to insert specified number of


characters into a binary file which is opened in writing
mode.
Code for fwrite()

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char *text = "Welcome to C Language";
fp = fopen("file5.txt","w");
fwrite(text,sizeof(char),5,fp);
fclose(fp);
return 0;
}
Closing a file

• Closing a file is performed using a pre-defined method


fclose().
• fclose( *f_ptr )
• The method fclose() returns '0'on success of file close
otherwise it returns EOF (End Of File).
rewind( *file_pointer )
File
pointers • This function is used reset the cursor
position to the beginning of the file.

ftell() rewind() fseek()

fseek( *file_pointer, noOfChar, fromPosition)


• This function is used to set the cursor
ftell( *file_pointer )
position to the specific position.
• This function returns the
• beginning of the file (indicated with 0)
current position of the
• current cursor position (indicated with 1)
cursor in the file.
• ending of the file (indicated with 2)
Code for ftell()

#include<stdio.h>
#include<conio.h>
int main() Output:
{
FILE *fp; Cursor position = 0
int position; Cursor position = 5
fp = fopen ("file4.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
return 0;
}
Code for rewind()

#include<stdio.h>
#include<conio.h>
int main()
Output:
{
Cursor position = 0
FILE *fp;
Cursor position = 5
int position;
Cursor position = 0
fp = fopen ("file4.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d\n", position);
rewind(fp);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
return 0;
}
Code for fseek()

#include<stdio.h>
#include<conio.h>
int main()
Output:
{
Cursor position = 0
FILE *fp;
Cursor position = 5
int position;
Cursor position = 12
fp = fopen ("file4.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d\n", position);
fseek(fp, -5, 2);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
return 0;
}

You might also like