0% found this document useful (0 votes)
2 views

c assign part 2

Uploaded by

alfredweizmann
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)
2 views

c assign part 2

Uploaded by

alfredweizmann
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
You are on page 1/ 5

Examples: -exe (uindows) , no extension(inus)

DisCuss various ile-handlng functions in C wrth


their syntax and examples each.

-Opening a ile fopen):


Tbis func tion opens a file and returns a file
Pointer OY NULL if there's an erOr opening
the fle
Syntax:
F\LE * open((onst char filename const char
* mode);
Lt takes two arquments : the file name and the
mode in which the fle is to be opened like
open for Teading ile must exist)
w": Open for writing (creates a new file or oveY LJY0 tes
existi ng)
open for appending(cveates a new file ov appends
to existing)
Exarnple
FlLE *P fopen"my-file .txt","w);
lopen for writing
2-Closing a filefclosec) :
Tt is used to close a file that has be en
opene d
using fopenc ). Tt takes the fle pointer as
as its
arqument and returns o SUccess and EOF
failure
Syntar:
int fclose (FILE *P);
EXample:
fclose(P); lcloses the file
3-Witing to a file:
a) f printy ():
"Tbis unction is used to ite formatted data to
a file It woYks similarls to 'printt(), but instead
of printing to console, it writes fomatted output
to the specified file
Syntax:
int fprintt (FILE *4P, const char *format,...): |
Exame
fprintt(tr."The value of pi is: .24\n, 314 159);
b) f putsc0:
This func tion write s a string to afile
Syntan:
int f puts(const-char *str, FILE *{P):
Example:
tputs ("Hello, world!",tp):
y-Rea din9 from a file:
o) fscang0):
fscanft0' 1eads foimatted data fvom a file J+
is similar to 's caft )', but reads input from a
file instead of the console
Syntax:
int fscanfFILE *fP, ConSt char *format,...) ;
Examçl
int eagei
:
fscanf4p. "Aqe: hd", age);
b) fgetslO:
This func tion ve ads a line pom the specitie d file
and styo ves it into aa string. IH 1eads unil a
newline character is found the speified numr
0f Characters is Yead
Syntax:
Char *fqets (char *Str,int size, F\lE *jP);
Exarmçle:
char line [100):
tqets(ine, sizeof (line),fP):
)49etc0:
Reads aa single chavacter fom a file
Syntax:
|int fqetc (F ILE xstream);
Example
FILE *4P= topen("'my-file.txt", "a);
char ch fqetcGP), IRead the first
5fputeO: charactev
rites asingle character to a file
Syntax:
int fputc(int ch, FlLE *Stream);
Example:
fputc(A, tP); I|JTite the characteT A' to the
file)'
GTewind():
Tt sets the file position indicator for the file
Stre am to the beginning of the file. It is typically
use d to rewind aa fle to the start tor
re-reading
Syntax:
Void re wind (FILE #Stream);

Examplere wind (P); l60 back to the beginning ot


the file
7feof):
Tests for end- of-fle
Synt aX:
int feof (EILE *Stve am);
Example:
while (1feot 4P))
Loop until the end of file is re ached
ch= fgetcGP) ;

84seek():
This function is used to move the file pointer to a
Specified lbcation within the fle. It takes three
avqumets: the file pointer, the offset (num ber
bytes to move), and the oriqin.
Syntax:
int fseek(FlLE *Stream,long ofset, int s0uTCe)
Example
tseek(P,10, StEk-sET); |/Move to the ioth byte
fom the beginning
4ftellO:
It netuns the cuent position of the file pointer
in the specified file stre am.Tt is often used to
detemine the cuTYent position within a file.
file
Syntax:
long ftell(FILE * stream);
EXample:
tong pos -ftell(4P) ;
/ Ghet the cuent position.

You might also like