0% found this document useful (0 votes)
3 views3 pages

C File Handling: Reading & Writing Data

The document provides an introduction to files, explaining that a file is a collection of data stored on secondary storage devices like hard disks. It outlines the process of working with files in C, including declaring file pointers, opening files in various modes, and closing them, along with functions used for reading and writing data. Additionally, it includes a sample C program demonstrating how to copy data from one text file to another.

Uploaded by

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

C File Handling: Reading & Writing Data

The document provides an introduction to files, explaining that a file is a collection of data stored on secondary storage devices like hard disks. It outlines the process of working with files in C, including declaring file pointers, opening files in various modes, and closing them, along with functions used for reading and writing data. Additionally, it includes a sample C program demonstrating how to copy data from one text file to another.

Uploaded by

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

chap-16 Files

16.1 Introduction to files

file:A file stored


device
is a collection ofdata on a
secondary storage
like hard disk.

stored be
-data in the file will
permanently stored.

and writing files performed through


Reading into are streams.

streams:stream is a
logical interface to the devices thatare
connected the computer.
to Streams are channels between
afile and program.
- streams acts as buffers to transfer data between program and
secondary storage (Disk/File).
-
3 standard streams in

standard output stream


-
--
stdout terminall
keyboard program
- >

Stain t screen
↓ Stdeur
standard input Statandard error stream.
stream

16.2 Files in
Using C

following steps are


required operate files:
to

1. Declaration of file pointer variable

FILE *Ip;
Pointer to a file.

2.
Opening a file.
-
before data is read/written into file, file must be
opened
in
appropriate mode (read, write, opend) and assigned
a pointer to it.

FILE *p=fopen("filename", model;


Ex: FILE *A=fopen("input ext";'r'); //opens the file in
read model assigns
Ip-Girish mantha
Pointer p to
starting char

page:ol
[Link]
file in write
FILE
*p=topen ("[Link]", 'w'; opens the
mode all contents
evasing
if already exists;

- creates a new file.

out file. txt

FILE *p=fopen ("input. ext";a); //opens file in


the
write mode, sets to
givish mantha to the end ofexistiting
up -> contents.

[Link]
the file.
3.
Closing
-
disconnects the the pointer from the file,
file the pointer can
be used to access the different file.

fclose (fp);

from files data files


16.3
Rending data 16.4 writing to

I includes.
#


-

i
main)

>? >

[Link] program. C out [Link]


program program
reads the writes the
data from data into
file file
functions used Functions used
Ascanfl> Aprintf()
fgets fputs()
fgetc() Aputcl)
fread() twrite()

⑪ write a c program to copy a text file another, read


to both the

input file nause file name.


and target

include
# (staio.h)
include (string.
# 4)

int main
E
FILE *fp1, *fp2, *p3;
buffer (1000];
char
inputfile (20], outputfile (20],
puts ("Enter input file name with xt extension");
gets (input file); Page:02
puts ("Enter outputfile name with xt extension");
gets (output file);

↑1= topen (input file, "Will; I open file


for ");
printf ("Enter the contents inputfile:
gets (buffer);

Aputs (buffer, pl); waite data file


Aclose (p1); 1) close the file

↑pI=fopen (inputfile, "w"); open inputfile in read mode


fpz topen (output file, "wi);
=
I/open output file in write mode

I file-to-file copy
while)! eof (p) 1 while not end of file fp-> inputfile
E
Agets (buffer, 100, PI); Ireads 100 bytes from plI
stores in buffer
-puts (buffer, fp2); // writes buffer to
2 (outputfile)
3

printf (" In open the


directory I check the outputfiles",
outputfile);
fclose (AD);
Aclose (fp2);
3
output:
Enter input file name with the extension
a. txt I
with it extension
Enter output file name
b. txt
Enter the contents for inputfile: JNNCE Shivamogga Kartala

and [Link]
Open the
directory check the outputfile:
contents of files
a. At b. txt

JNNCE Shivaloyga
JNNCE Shivaloyga
copied to
-- Karnataka
Karnataka

inputfile outputfile.

page:03

You might also like