File Dealing with
File Dealing with in C
File Dealing with idea in C language is used for retailer a knowledge completely in laptop. Utilizing this idea we are able to retailer our information in Secondary reminiscence (Exhausting disk). All recordsdata associated perform can be found in stdio.h header file.
Learn how to obtain File Dealing with in C
For reaching file dealing with in C we’d like comply with following steps
- Naming a file
- Opening a file
- Studying information from file
- Writing information into file
- Closing a file
Capabilities use in File Dealing with in C
S.No | Perform | Operation |
---|---|---|
1 | fopen() | To create a file |
2 | fclose() | To shut an present file |
3 | getc() | Learn a personality from a file |
4 | putc() | Write a personality in file |
5 | fprintf() | To write down set of information in file |
6 | fscanf() | To learn set of information from file. |
5 | getw() | To learn an integer from a file |
6 | putw() | To write down an integer in file |
Defining and Opening a File
Information construction of file is outlined as FILE in the usual I/O perform. So all recordsdata needs to be declared as kind FILE.
Earlier than opening any file we have to specify for which goal we open file, for instance file open for write or learn goal.
Syntax
1 |
<span class="pln">FILE </span><span class="pun">*</span><span class="pln">fp</span><span class="pun">;</span><span class="pln">pf</span><span class="pun">=</span><span class="pln">fopen</span><span class="pun">(</span><span class="str">"filename"</span><span class="pun">,</span> <span class="str">"mode"</span><span class="pun">);</span> |
Right here fp declare a variable as a pointer to the info kind FILE.
Closing a File
A file have to be shut after completion of all operation associated to file. For closing file we’d like fclose() perform.
Syntax
1 |
<span class="pln">fclose</span><span class="pun">(</span><span class="typ">Filepointer</span><span class="pun">);</span> |
File Opening mode
S.No | Mode | That means | Function |
---|---|---|---|
1 | r | Studying | Open the file for studying solely. |
2 | w | Writing | Open the file for writing solely. |
3 | a | Appending | Open the file for appending (or including) information to it. |
4 | r+ | Studying + Writing | New information is written at first override present information. |
5 | w+ | Writing + Studying | Override present information. |
6 | a+ | Studying + Appending | To new information is appended on the finish of file. |
Enter/Output Operation on recordsdata
To carry out Enter/Output Operation on recordsdata we’d like under capabilities.
S.No | Perform | Operation | Syntax | |
---|---|---|---|---|
1 | getc() | Learn a personality from a file | getc( fp) | |
2 | putc() | Write a personality in file | putc(c, fp) | |
3 | fprintf() | To write down set of information in file | fprintf(fp, “management string”, checklist) | |
4 | fscanf() | To learn set of information from file. | fscanf(fp, “management string”, checklist) | |
5 | getw() | To learn an integer from a file. | getw(fp) | |
6 | putw() | To write down an integer in file. | putw(integer, fp) |
Write information in File
Instance
1 |
<span class="com">#embody</span><span class="str"><stdio.h></stdio.h></span><span class="com">#embody</span><span class="str"><conio.h></conio.h></span><span class="kwd">void</span><span class="pln"> principal</span><span class="pun">()</span><span class="pun">{</span><span class="pln">FILE </span><span class="pun">*</span><span class="pln">fp</span><span class="pun">;</span><span class="kwd">char</span><span class="pln"> ch</span><span class="pun">[</span><span class="lit">20</span><span class="pun">];</span><span class="pln">clrscr</span><span class="pun">();</span><span class="pln">fp</span><span class="pun">=</span><span class="pln">fopen</span><span class="pun">(</span><span class="str">"hh.txt"</span><span class="pun">,</span> <span class="str">"w"</span><span class="pun">);</span><span class="pln">printf</span><span class="pun">(</span><span class="str">"Enter any Textual content: "</span><span class="pun">);</span><span class="pln">scanf</span><span class="pun">(</span><span class="str">"%s"</span><span class="pun">,&</span><span class="pln">ch</span><span class="pun">);</span> <span class="com">// Learn information from keyboard</span><span class="pln">fprintf</span><span class="pun">(</span><span class="pln">fp</span><span class="pun">,</span><span class="str">"%s"</span><span class="pun">,</span><span class="pln">ch</span><span class="pun">);</span> <span class="com">// Write information in file</span><span class="pln">fclose</span><span class="pun">(</span><span class="pln">fp</span><span class="pun">);</span><span class="pln">getch</span><span class="pun">();</span><span class="pun">}</span> |