SlideShare a Scribd company logo
File
A file represents a sequence of bytes, regardless of it being a text file or a binary file. When a
program is terminated, the entire data is lost. Storing in a file will preserve your data even if the
program terminates. It is easy to move the data from one computer to another without any
changes. When working with files, you need to declare a pointer of type file. This declaration is
needed for communication between the file and the program.
FILE *fp; // *fp – file pointer variable
Types of Files
There are two types of files
 Text files
 Binary files
1. Text files
Text files are the normal .txt files. You can easily create text files using any simple text editors
such as Notepad. When you open those files, you'll see all the contents within the file as plain
text. It is easy to edit or delete the contents. They take minimum effort to maintain, are easily
readable, and provide the least security and takes bigger storage space.
2. Binary files
Binary files are mostly the .bin files in the computer. Instead of storing data in plain text, they
store it in the binary form (0's and 1's). They can hold a higher amount of data, are not readable
easily, and provides better security than text files.
File Operations
1) Opening a file:
Opening a file is performed using the fopen() function defined in the stdio.h header file.
The syntax for opening a file in standard I/O is:
FILE *fp
fp = fopen("filename","mode");
File Opening Mode
Sl. No Mode Description
1 r Opens an existing text file for reading purpose.
2 w
Opens a text file for writing. If it does not exist, then a new file is
created. Here your program will start writing content from the
beginning of the file.
3 a
Opens a text file for writing in appending mode. If it does not exist,
then a new file is created. Here your program will start appending
content in the existing file content.
4 r+ Opens a text file for both reading and writing.
5 w+
Opens a text file for both reading and writing. It first truncates the
file to zero length if it exists, otherwise creates a file if it does not
exist.
6 a+
Opens a text file for both reading and writing. It creates the file if it
does not exist. The reading will start from the beginning but writing
can only be appended.
File Location
We can provide the relative address of the file location or absolute address of the file. Consider
your working directory is C:CPTest . Now you want to open a file hello.c in read mode. Two
ways to provide the file location are as given below:
fp =
fopen("hello.c","r"); OR
fp = fopen("C:CPTesthello.c","r")
2. Closing a file
The file (both text and binary) should be closed after reading/writing. Closing a file is performed
using the fclose() function.
fclose(fp);
Here, fp is a file pointer associated with the file to be closed.
3. Reading and writing to a file
Sl. No Function Name Description Syntax
1 fgetc To read a character from a file ch = fgetc(fp)
2 fputc To write a character to a file fputc(ch,fp)
3 fscanf To read numbers, string from a file fscanf(fp,"%d",&n)
4 fprintf To write numbers, strings to a file fprintf(fp,"%d",n)
5 fread To read binary content from a file. It
is used to read structure content.
Refer Note
6 fwrite To write as binary content to a file. Refer Note
Note:
1) The only difference is that fprint() and fscanf() expects a pointer to the structure FILE.
2) To write into a binary file, you need to use the fwrite() function. The functions take four
arguments:
 Address of data to be written in the disk
 Size of data to be written in the disk
 Number of such type of data
 Pointer to the file where you want to write.
fwrite(addressData, sizeData, numbersData, pointerToFile);
3) Function fread() also take 4 arguments similar to the fwrite() function as above.
fread(addressData, sizeData, numbersData, pointerToFile);
feof()
The C library function int feof(FILE *stream) tests the end-of-file indicator for the given
stream. This function returns a non-zero value when End-of-File indicator associated with the
stream is set, else zero is returned.
Random Access to a file
1) fseek()
If you have many records inside a file and need to access a record at a specific position, you need
to loop through all the records before it to get the record. This will waste a lot of memory and
operation time. An easier way to get to the required data can be achieved using fseek().
fseek(FILE * stream, long int offset, int pos);
The first parameter stream is the pointer to the file. The second parameter is the position of the
record to be found, and the third parameter specifies the location where the offset starts.
Different positions in fseek()
Position Meaning
SEEK_SET Starts the offset from the beginning of the file.
SEEK_END Starts the offset from the end of the file.
SEEK_CUR Starts the offset from the current location of the cursor in the file.
2) ftell()
ftell() in C is used to find out the position of file pointer in the file with respect to starting of the
file.
Syntax of ftell() is:
ftell(FILE *pointer)
Examples Programs
1) Write a program to display the content of a file.
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("test.txt","r");
while(feof(fp) == 0)
{
ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
}
Content of test.txt
Hello, Welcome to C Programming Lectures.
Output
Hello, Welcome to C Programming Lectures.
2) Write a program to count numbers of vowels in a given file.
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
int countV=0;
fp = fopen("test.txt","r");
while(feof(fp) == 0)
{
ch=fgetc(fp);
if(ch == 'a' || ch == 'A' || ch=='e'
ch=='E' || ch == 'I' || ch == 'i' ||
ch == 'O' || ch=='o' || ch == 'U' ||
ch == 'u')
{
countV++;
}
}
printf("Count of Vowels=%d",countV);
fclose(fp);
}
Content of test.txt
Hello, Welcome to C Programming Lectures.
Output
Count of Vowels=12
3) Write a program to copy the content of file to another.
#include<stdio.h>
void main()
{
FILE *f1,*f2;
char ch;
f1 = fopen("test.txt","r");
f2 = fopen("copy.txt","w");
while(feof(f1) == 0)
{
ch=fgetc(f1);
fputc(ch,f2);
}
printf("Successfully Copied");
fclose(f1);
fclose(f2);
}
Content of test.txt
Hello, Welcome to C Programming Lectures.
Output
Successfully Copied
Content of copy.txt
Hello, Welcome to C Programming Lectures.
4) Write a program to merge the content of two files.
#include<stdio.h>
void main()
{
FILE *f1,*f2,*f3;
char ch;
f1 = fopen("file1.txt","r");
f2 = fopen("file2.txt","r");
f3 = fopen("merge.txt","w");
while(feof(f1) == 0)
{
ch=fgetc(f1);
fputc(ch,f3);
}
while(feof(f2) == 0)
{
ch=fgetc(f2);
fputc(ch,f3);
}
printf("Successfully Merged");
}
Content of file1.txt
Hello, Welcome to C Programming Lectures.
Content of file2.txt
C is very easy to learn.
Output
Successfully Merged
Content of merge.txt
Hello, Welcome to C Programming Lectures. C is very easy to learn.
5) Write a program to read numbers from a file and display the largest number.
#include<stdio.h>
void main()
{
FILE *f1;
int large,num;
f1 = fopen("number.txt","r");
fscanf(f1,"%d",&large); // setting first element as largest element
while(feof(f1) == 0)
{
fscanf(f1,"%d",&num);
if(large<num)
{
large= num;
}
}
fclose(f1);
printf("Largest element = %d",large);
}
Content of number.txt
15 21 7 29 36 78 67 56 10
Output
Largest element = 78
6) Consider you are a content writer in Wikipedia. You are the person who write the known facts
about APJ Abdul Kalam. After his death, you need to change all is to was. Write a program to
replace all is’ to was’ to a new file.
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1,*f2;
char str[30];
f1 = fopen("apj.txt","r");
f2 = fopen("new.txt","w");
fscanf(f1,"%s",str);
while(feof(f1) == 0)
{
if(strcmp(str,"is")==0)
fprintf(f2,"was A");
else
fprintf(f2,"%s ",str);
fscanf(f1,"%s",str);
}
fclose(f1);
fclose(f2);
printf("Replaced String Sucessfullyn");
}
7) Write a program to reverse each content of file to another.
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1,*f2;
char str[30],rev[30];
int i,j;
f1 = fopen("test.txt","r");
f2 = fopen("new.txt","w");
while(feof(f1) == 0)
{
fscanf(f1,"%s",str);
j=0;
for(i=strlen(str)-1;i>=0;i--)
{
rev[j]=str[i];
j++;
}
rev[j]='0';
fprintf(f2,"%s ",rev);
}
fclose(f1);
fclose(f2);
}
Content of test.txt
Welcome to C programming
Content of new.txt after execution
emocleW ot C gnimmargorp
8) Write a program to copy the content of a file to another in reverse order.
#include<stdio.h>
void main()
{
FILE *f1,*f2;
int count,begin,end;
char ch,i;
f1 = fopen("test.txt","r");
f2 = fopen("new.txt","w");
// Code to find count of characters in a file.
begin = ftell(f1);
fseek(f1, -1, SEEK_END);
end = ftell(f1);
count = end - begin; // Count of characters.
printf("Count of characters=%d",count);
// Copy the content of file in reverse order
i=-1;
while (count != -1)
{
ch = fgetc(f1);
fputc(ch, f2);
i--;
fseek(f1, i, SEEK_END); // shifts the pointer to the previous character
count--;
}
fclose(f1);
fclose(f2);
}
9) Write a program to count number of words and lines in a file.
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1,*f2;
int countW=0,countL=0;
char ch;
f1 = fopen("test.txt","r");
while (feof(f1) == 0 )
{
ch = fgetc(f1);
if(ch == ' ')
countW++;
if(ch == 'n')
countL++;
}
printf("Count of words = %dn",countW);
printf("Count of Lines = %d",countL);
fclose(f1);
}
Content of test.txt
Welcome to C programming.
C is very easy to learn
Output
Count of words = 4
Count of Lines = 2
10) Write a program to append some data to already existing file.
#include<stdio.h>
void main()
{
FILE *f1;
char str[30];
f1 = fopen("test.txt","a");
printf("Enter the string:");
gets(str);
fprintf(f1,"%s",str);
fclose(f1);
}

More Related Content

PPTX
File handling in C
PPT
file handling1
DOCX
Satz1
PPTX
File handling in c
PPTX
File management
PPT
File in c
PPT
Unit5
PPSX
C programming file handling
File handling in C
file handling1
Satz1
File handling in c
File management
File in c
Unit5
C programming file handling

What's hot (19)

PPT
File handling in 'C'
PPT
File handling in c
PPTX
C Programming Unit-5
DOCX
Understanding c file handling functions with examples
PPT
File in C Programming
PDF
14. fiile io
PPT
file
PPTX
File handling in c
PDF
Python-files
PPTX
File handling in C
PDF
Python - File operations & Data parsing
PPTX
Data Structure Using C - FILES
PPTX
File handling in c language
DOCX
PDF
Files in C
DOCX
Php files
File handling in 'C'
File handling in c
C Programming Unit-5
Understanding c file handling functions with examples
File in C Programming
14. fiile io
file
File handling in c
Python-files
File handling in C
Python - File operations & Data parsing
Data Structure Using C - FILES
File handling in c language
Files in C
Php files
Ad

Similar to Module 5 file cp (20)

PDF
Chapter 13.1.10
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PDF
FILES IN C
PPTX
Programming C- File Handling , File Operation
PPTX
want to learn files,then just use this ppt to learn
PPT
file_handling_in_c.ppt
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
PPTX
PPS-II UNIT-5 PPT.pptx
DOCX
Unit 5 dwqb ans
PPTX
File Handling ppt.pptx shjd dbkd z bdjdb d
PPTX
Unit-VI.pptx
PPTX
PPS PPT 2.pptx
PDF
4 text file
PPTX
File Organization
PPT
PPTX
MODULE 8-File and preprocessor.pptx for c program learners easy learning
PPTX
File management
PPTX
INput output stream in ccP Full Detail.pptx
PPT
Mesics lecture files in 'c'
PDF
VIT351 Software Development VI Unit5
Chapter 13.1.10
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
FILES IN C
Programming C- File Handling , File Operation
want to learn files,then just use this ppt to learn
file_handling_in_c.ppt
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
PPS-II UNIT-5 PPT.pptx
Unit 5 dwqb ans
File Handling ppt.pptx shjd dbkd z bdjdb d
Unit-VI.pptx
PPS PPT 2.pptx
4 text file
File Organization
MODULE 8-File and preprocessor.pptx for c program learners easy learning
File management
INput output stream in ccP Full Detail.pptx
Mesics lecture files in 'c'
VIT351 Software Development VI Unit5
Ad

More from Amarjith C K (20)

PPTX
E paper technologies
PDF
Module 2
PDF
module 1
PDF
Module 2
PDF
Module-4
PDF
New doc 2020 03-28 09.51.49
PDF
Mat102 module iv
PDF
Mat102 module 5(text )
PDF
Mat 102 module iv
PDF
New doc 2020 03-19 12.05.31
PDF
New doc 2020 03-23 14.09.06
PDF
New doc 2020 03-23 16.20.34
PDF
New doc 2020 03-23 17.08.57
PDF
Unit 03 esl
PDF
Module-5
PDF
Module-4
PPTX
Module 3
PDF
Mat102 module v
PDF
cp Module4(1)
E paper technologies
Module 2
module 1
Module 2
Module-4
New doc 2020 03-28 09.51.49
Mat102 module iv
Mat102 module 5(text )
Mat 102 module iv
New doc 2020 03-19 12.05.31
New doc 2020 03-23 14.09.06
New doc 2020 03-23 16.20.34
New doc 2020 03-23 17.08.57
Unit 03 esl
Module-5
Module-4
Module 3
Mat102 module v
cp Module4(1)

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Sustainable Sites - Green Building Construction
DOCX
573137875-Attendance-Management-System-original
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Welding lecture in detail for understanding
PPTX
additive manufacturing of ss316l using mig welding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Lecture Notes Electrical Wiring System Components
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Well-logging-methods_new................
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
Project quality management in manufacturing
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
composite construction of structures.pdf
bas. eng. economics group 4 presentation 1.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
OOP with Java - Java Introduction (Basics)
Sustainable Sites - Green Building Construction
573137875-Attendance-Management-System-original
Operating System & Kernel Study Guide-1 - converted.pdf
Welding lecture in detail for understanding
additive manufacturing of ss316l using mig welding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Lecture Notes Electrical Wiring System Components
R24 SURVEYING LAB MANUAL for civil enggi
Internet of Things (IOT) - A guide to understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Well-logging-methods_new................
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Project quality management in manufacturing
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Embodied AI: Ushering in the Next Era of Intelligent Systems

Module 5 file cp

  • 1. File A file represents a sequence of bytes, regardless of it being a text file or a binary file. When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates. It is easy to move the data from one computer to another without any changes. When working with files, you need to declare a pointer of type file. This declaration is needed for communication between the file and the program. FILE *fp; // *fp – file pointer variable Types of Files There are two types of files  Text files  Binary files 1. Text files Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad. When you open those files, you'll see all the contents within the file as plain text. It is easy to edit or delete the contents. They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage space. 2. Binary files Binary files are mostly the .bin files in the computer. Instead of storing data in plain text, they store it in the binary form (0's and 1's). They can hold a higher amount of data, are not readable easily, and provides better security than text files. File Operations 1) Opening a file: Opening a file is performed using the fopen() function defined in the stdio.h header file. The syntax for opening a file in standard I/O is: FILE *fp fp = fopen("filename","mode");
  • 2. File Opening Mode Sl. No Mode Description 1 r Opens an existing text file for reading purpose. 2 w Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file. 3 a Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content. 4 r+ Opens a text file for both reading and writing. 5 w+ Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. 6 a+ Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended. File Location We can provide the relative address of the file location or absolute address of the file. Consider your working directory is C:CPTest . Now you want to open a file hello.c in read mode. Two ways to provide the file location are as given below: fp = fopen("hello.c","r"); OR fp = fopen("C:CPTesthello.c","r") 2. Closing a file The file (both text and binary) should be closed after reading/writing. Closing a file is performed using the fclose() function.
  • 3. fclose(fp); Here, fp is a file pointer associated with the file to be closed. 3. Reading and writing to a file Sl. No Function Name Description Syntax 1 fgetc To read a character from a file ch = fgetc(fp) 2 fputc To write a character to a file fputc(ch,fp) 3 fscanf To read numbers, string from a file fscanf(fp,"%d",&n) 4 fprintf To write numbers, strings to a file fprintf(fp,"%d",n) 5 fread To read binary content from a file. It is used to read structure content. Refer Note 6 fwrite To write as binary content to a file. Refer Note Note: 1) The only difference is that fprint() and fscanf() expects a pointer to the structure FILE. 2) To write into a binary file, you need to use the fwrite() function. The functions take four arguments:  Address of data to be written in the disk  Size of data to be written in the disk  Number of such type of data  Pointer to the file where you want to write. fwrite(addressData, sizeData, numbersData, pointerToFile); 3) Function fread() also take 4 arguments similar to the fwrite() function as above. fread(addressData, sizeData, numbersData, pointerToFile); feof() The C library function int feof(FILE *stream) tests the end-of-file indicator for the given stream. This function returns a non-zero value when End-of-File indicator associated with the stream is set, else zero is returned.
  • 4. Random Access to a file 1) fseek() If you have many records inside a file and need to access a record at a specific position, you need to loop through all the records before it to get the record. This will waste a lot of memory and operation time. An easier way to get to the required data can be achieved using fseek(). fseek(FILE * stream, long int offset, int pos); The first parameter stream is the pointer to the file. The second parameter is the position of the record to be found, and the third parameter specifies the location where the offset starts. Different positions in fseek() Position Meaning SEEK_SET Starts the offset from the beginning of the file. SEEK_END Starts the offset from the end of the file. SEEK_CUR Starts the offset from the current location of the cursor in the file. 2) ftell() ftell() in C is used to find out the position of file pointer in the file with respect to starting of the file. Syntax of ftell() is: ftell(FILE *pointer) Examples Programs 1) Write a program to display the content of a file. #include<stdio.h> void main() { FILE *fp; char ch; fp = fopen("test.txt","r"); while(feof(fp) == 0)
  • 5. { ch=fgetc(fp); printf("%c",ch); } fclose(fp); } Content of test.txt Hello, Welcome to C Programming Lectures. Output Hello, Welcome to C Programming Lectures. 2) Write a program to count numbers of vowels in a given file. #include<stdio.h> void main() { FILE *fp; char ch; int countV=0; fp = fopen("test.txt","r"); while(feof(fp) == 0) { ch=fgetc(fp); if(ch == 'a' || ch == 'A' || ch=='e' ch=='E' || ch == 'I' || ch == 'i' || ch == 'O' || ch=='o' || ch == 'U' || ch == 'u') { countV++; } } printf("Count of Vowels=%d",countV); fclose(fp); } Content of test.txt Hello, Welcome to C Programming Lectures. Output Count of Vowels=12
  • 6. 3) Write a program to copy the content of file to another.
  • 7. #include<stdio.h> void main() { FILE *f1,*f2; char ch; f1 = fopen("test.txt","r"); f2 = fopen("copy.txt","w"); while(feof(f1) == 0) { ch=fgetc(f1); fputc(ch,f2); } printf("Successfully Copied"); fclose(f1); fclose(f2); } Content of test.txt Hello, Welcome to C Programming Lectures. Output Successfully Copied Content of copy.txt Hello, Welcome to C Programming Lectures. 4) Write a program to merge the content of two files. #include<stdio.h> void main() { FILE *f1,*f2,*f3; char ch; f1 = fopen("file1.txt","r"); f2 = fopen("file2.txt","r"); f3 = fopen("merge.txt","w"); while(feof(f1) == 0) { ch=fgetc(f1); fputc(ch,f3); } while(feof(f2) == 0) {
  • 9. printf("Successfully Merged"); } Content of file1.txt Hello, Welcome to C Programming Lectures. Content of file2.txt C is very easy to learn. Output Successfully Merged Content of merge.txt Hello, Welcome to C Programming Lectures. C is very easy to learn. 5) Write a program to read numbers from a file and display the largest number. #include<stdio.h> void main() { FILE *f1; int large,num; f1 = fopen("number.txt","r"); fscanf(f1,"%d",&large); // setting first element as largest element while(feof(f1) == 0) { fscanf(f1,"%d",&num); if(large<num) { large= num; } } fclose(f1); printf("Largest element = %d",large); } Content of number.txt 15 21 7 29 36 78 67 56 10 Output Largest element = 78
  • 10. 6) Consider you are a content writer in Wikipedia. You are the person who write the known facts about APJ Abdul Kalam. After his death, you need to change all is to was. Write a program to replace all is’ to was’ to a new file. #include<stdio.h> #include<string.h> void main() { FILE *f1,*f2; char str[30]; f1 = fopen("apj.txt","r"); f2 = fopen("new.txt","w"); fscanf(f1,"%s",str); while(feof(f1) == 0) { if(strcmp(str,"is")==0) fprintf(f2,"was A"); else fprintf(f2,"%s ",str); fscanf(f1,"%s",str); } fclose(f1); fclose(f2); printf("Replaced String Sucessfullyn"); } 7) Write a program to reverse each content of file to another. #include<stdio.h> #include<string.h> void main() { FILE *f1,*f2; char str[30],rev[30]; int i,j; f1 = fopen("test.txt","r"); f2 = fopen("new.txt","w"); while(feof(f1) == 0)
  • 12. for(i=strlen(str)-1;i>=0;i--) { rev[j]=str[i]; j++; } rev[j]='0'; fprintf(f2,"%s ",rev); } fclose(f1); fclose(f2); } Content of test.txt Welcome to C programming Content of new.txt after execution emocleW ot C gnimmargorp 8) Write a program to copy the content of a file to another in reverse order. #include<stdio.h> void main() { FILE *f1,*f2; int count,begin,end; char ch,i; f1 = fopen("test.txt","r"); f2 = fopen("new.txt","w"); // Code to find count of characters in a file. begin = ftell(f1); fseek(f1, -1, SEEK_END); end = ftell(f1); count = end - begin; // Count of characters. printf("Count of characters=%d",count); // Copy the content of file in reverse order i=-1; while (count != -1) { ch = fgetc(f1); fputc(ch, f2); i--;
  • 13. fseek(f1, i, SEEK_END); // shifts the pointer to the previous character count--;
  • 14. } fclose(f1); fclose(f2); } 9) Write a program to count number of words and lines in a file. #include<stdio.h> #include<string.h> void main() { FILE *f1,*f2; int countW=0,countL=0; char ch; f1 = fopen("test.txt","r"); while (feof(f1) == 0 ) { ch = fgetc(f1); if(ch == ' ') countW++; if(ch == 'n') countL++; } printf("Count of words = %dn",countW); printf("Count of Lines = %d",countL); fclose(f1); } Content of test.txt Welcome to C programming. C is very easy to learn Output Count of words = 4 Count of Lines = 2 10) Write a program to append some data to already existing file. #include<stdio.h> void main() {
  • 15. FILE *f1; char str[30]; f1 = fopen("test.txt","a");