Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

File Concepts Programs in C

Program to Create a file using fprintf()


\* Program to Create a file using fprintf()*/
#include<stdio.h>
Void main()
{
FILE *f;
f=fopen(“INPUT.TXT”, “w”);
fprintf(f,“Hai Welcome to AIML Class\n”);
fprintf(f,“SRM Madurai College for Engg and Technology\n”);
fclose(f);
}

Program to read from a file using fscanf()

#include<stdio.h>
Void main()
{
FILE *f;
Char s[25];
Int i=0;
f=fopen(“INPUT.TXT”, “r”);
while(fscanf(f,”%s”,s)!=EOF)
{
printf(“%s”,s);
i++;
}
Printf(“\n Number of words in this fie is %d”, i);
fclose(f);
}
Program to write a file using putc & fputc

#include<stdio.h>
void main()
{
FILE *f;
f=fopen(“INPUT.TXT”, “w”);
putc(‘H’,f);
fputc(‘\n’,f);
putc(‘A’,f);
fputc(‘\n’,f);
putc(‘I’,f);
fclose(f);
}
Create FIle
#include<stdio.h>
Void main()
{
FILE *f;
F=fopen(“INPUT.TXT”, “w”);
fputs(“Hello Welcome to File Concepts”, f);
fclose(f);
}

Read FIle
#include<stdio.h>
Void main()
{
FILE *f;
Char s[25];
f=fopen(“INPUT.TXT”, “r”);
fgets(s,25,f);
printf(“Contents are:\n%s”, s);
fclose(f);
}
Program to Create a file using fprintf()
\* Program to Create a file using fprintf()*/
#include<stdio.h>
Void main()
{
FILE *f;
f=fopen(“INPUT.TXT”, “w”);
fprintf(f,“Hai Welcome to AIML Class\n”);
fprintf(f,“SRM Madurai College for Engg and Technology\n”);
fclose(f);
}

Program to read from a file using fscanf()

#include<stdio.h>
Void main()
{
FILE *f;
Char s[25];
Int i=0;
f=fopen(“INPUT.TXT”, “r”);
while(fscanf(f,”%s”,s)!=EOF)
{
printf(“%s”,s);
i++;
}
Printf(“\n Number of words in this fie is %d”, i);
fclose(f);
}

You might also like