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 knowledge in Secondary reminiscence (Onerous disk). All information associated perform can be found in stdio.h header file.
Learn how to obtain File Dealing with in C
For attaining file dealing with in C we’d like comply with following steps
- Naming a file
- Opening a file
- Studying knowledge from file
- Writing knowledge into file
- Closing a file
Features use in File Dealing with in C
S.No | Operate | Operation |
---|---|---|
1 | fopen() | To create a file |
2 | fclose() | To shut an current 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
Knowledge construction of file is outlined as FILE in the usual I/O perform. So all information 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 information 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 | Which means | Objective |
---|---|---|---|
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) knowledge to it. |
4 | r+ | Studying + Writing | New knowledge is written in the beginning override current knowledge. |
5 | w+ | Writing + Studying | Override current knowledge. |
6 | a+ | Studying + Appending | To new knowledge is appended on the finish of file. |
Enter/Output Operation on information
To carry out Enter/Output Operation on information we’d like under capabilities.
S.No | Operate | 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 knowledge in File
Instance
1 |
<span class="com">#embrace</span><span class="str"><stdio.h></stdio.h></span><span class="com">#embrace</span><span class="str"><conio.h></conio.h></span><span class="kwd">void</span><span class="pln"> foremost</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 knowledge 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 knowledge 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> |