This article will show you File Input Output Sample Programs in C. This program uses following components:
1. #include<stdio.h>: include directive , which is including stdio.h header file in program.
2. main() starting function of c
3. FILE , type to declare pointer variable for files
4. fopen(): function to open file, it takes two parameters first file name, and other the mode in which you want to open file, like w for write mode, r for read moe, a for append mode and so on.
5. clrscr(): clears screen
6. fprintf(): function to write on a file, it takes two parameters first file pointer variables and second some string to write on file.
7. printf(): output function, to provide output on screen.
8. getch(): input function to take single invisible character from keyboard.
9. fscanf(): function to read from file
10. fgets(): function to read from file
11. fputc(): function to write a single character on file
12. fgetc(): function to read a single character fro file
13. Function for reading and writing on binary files
Program1: Writing to a text File
1. #include<stdio.h>: include directive , which is including stdio.h header file in program.
2. main() starting function of c
3. FILE , type to declare pointer variable for files
4. fopen(): function to open file, it takes two parameters first file name, and other the mode in which you want to open file, like w for write mode, r for read moe, a for append mode and so on.
5. clrscr(): clears screen
6. fprintf(): function to write on a file, it takes two parameters first file pointer variables and second some string to write on file.
7. printf(): output function, to provide output on screen.
8. getch(): input function to take single invisible character from keyboard.
9. fscanf(): function to read from file
10. fgets(): function to read from file
11. fputc(): function to write a single character on file
12. fgetc(): function to read a single character fro file
13. Function for reading and writing on binary files
Program1: Writing to a text File
#include<stdio.h>
void main()
{
FILE *fp;
clrscr();
fp=fopen("abc.txt","w");
fprintf(fp,"Testing");
printf("File Written, Press any key
exit\n");
getch();
}
Program2:
Reading from a text file
#include<stdio.h>
void main()
{
FILE *fp;
char arr[50];
clrscr();
fp=fopen("abc.txt","r");
fscanf(fp,"%s",arr);
printf("\n%s",arr);
printf("\nPress any key to
exit\n");
getch();
}
Program3:
Using gets for writing to file
#include<stdio.h>
void main()
{
FILE *fp;
char arr[50];
clrscr();
fp=fopen("abc.txt","w");
printf("\nEnter any sentence");
gets(arr);
fprintf(fp,arr);
printf("\nFile Written, Press any
key to exit\n");
getch();
}
Program4:
Using fgets to read from File
#include<stdio.h>
void main()
{
FILE *fp;
char arr[50];
clrscr();
fp=fopen("abc.txt","r");
fgets(arr,50,fp);
//printf("%s",arr); or use puts
puts(arr);
printf("\nPress any key to
exit\n");
getch();
}
Program5:
Writing a single character to a text file
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("abc.txt","w");
printf("Enter Single
Character\n");
ch=getche();
fputc(ch,fp);
printf("\nCharacter written, Press
any key to exit\n");
getch();
}
Program6:
Reading Single Character from a Text File
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("abc.txt","r");
ch=fgetc(fp);
printf("\n\nCharacter is\t%c",ch);
printf("\nPress any key to
exit\n");
getch();
}
Program7:
Writing numeric data to a Text File
#include<stdio.h>
void main()
{
int num;
FILE *fp;
char ch;
clrscr();
fp=fopen("abc.txt","w");
if(fp==NULL)
{
printf("Error");
exit(0);
}
printf("Enter Number\n");
scanf("%d",&num);
fprintf(fp,"%d",num);
fclose(fp);
printf("\nFile Created\n");
getch();
}
Program8:
Reading Numeric Data from Text File
#include<stdio.h>
void main()
{
int num;
FILE *fp;
clrscr();
fp=fopen("abc.txt","r");
fscanf(fp,"%d",&num);
printf("Number is \t%u",num);
getch();
}
Program9:
Writing to a Binary File
//Writing to a Binary File
#include<stdio.h>
struct threeNum
{
int n1,n2,n3;
};
void main()
{
struct threeNum num;
int n;
FILE *fp;
clrscr();
if(fp=fopen("prog.txt","wb")==NULL)
{
printf("Error! Opening File!\n");
exit(0);
}
for(n=1;n<5;++n)
{
num.n1=n;
num.n2=n+5;
num.n3=n+10;
fwrite(&num,sizeof(struct
threeNum),1,fp);
}
fclose(fp);
printf("File Created");
getch();
}
Program10:
Reading Data from a Binary File
//Reading from Binary File
#include<stdio.h>
struct threeNum
{
int n1,n2,n3;
};
void main()
{
struct threeNum num;
int n;
FILE *fp;
clrscr();
if(fp=fopen("prog.txt","rb")==NULL)
{
printf("Error! Opening File!\n");
exit(0);
}
for(n=1;n<5;n++)
{
fread(&num,sizeof(struct
threeNum),1,fp);
printf("n1:%d\n n2:%d\nn3:%d\n",num.n1,num.n2,num.n3);
}
fclose(fp);
getch();
}
Program11:
Opening a File for Writing and Reading:
#include<stdio.h>
int main()
{
FILE *file;
char sentence[50];
int i;
clrscr();
file=fopen("c:\\test.txt","w+");
//we create a file for reading and
writting
if(file==NULL)
{
printf("Error:Can't create
File.\n");
return 1;
}
else
printf("enter a sentence less than 50
characters:");
gets(sentence);
for(i=0;sentence[i];i++)
{
fputc(sentence[i],file);
}
rewind(file); //Reset the file pointer's position
printf("Contents of the file:
\n\n");
while(!feof(file))
{
printf("%c",fgetc(file));
}
printf("\n");
fclose(file);
getch();
}
12.
Write a C program to read name and marks of n number of students from user and
store them in a file.
#include
<stdio.h>
int
main()
{
char name[50];
int marks, i, num;
printf("Enter number of students:
");
scanf("%d", &num);
FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(i = 0; i < num; ++i)
{
printf("For student%d\nEnter name:
", i+1);
scanf("%s", name);
printf("Enter marks: ");
scanf("%d", &marks);
fprintf(fptr,"\nName: %s \nMarks=%d
\n", name, marks);
}
fclose(fptr);
return 0;
}
13. Write a C program to read name and marks
of n number of students from user and store them in a file. If the file
previously exits, add the information of n students.
#include
<stdio.h>
int
main()
{
char name[50];
int marks, i, num;
printf("Enter number of students:
");
scanf("%d", &num);
FILE *fptr;
fptr = (fopen("C:\\student.txt", "a"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(i = 0; i < num; ++i)
{
printf("For student%d\nEnter name:
", i+1);
scanf("%s", name);
printf("Enter marks: ");
scanf("%d", &marks);
fprintf(fptr,"\nName: %s \nMarks=%d
\n", name, marks);
}
fclose(fptr);
return 0;
}
14. Write a C program to write all the
members of an array of structures to a file using fwrite(). Read the array from
the file and display on the screen.
#include
<stdio.h>
struct
student
{
char name[50];
int height;
};
int
main(){
struct student stud1[5], stud2[5];
FILE *fptr;
int i;
fptr = fopen("file.txt","wb");
for(i = 0; i < 5; ++i)
{
fflush(stdin);
printf("Enter name: ");
gets(stud1[i].name);
printf("Enter height: ");
scanf("%d",
&stud1[i].height);
}
fwrite(stud1, sizeof(stud1), 1, fptr);
fclose(fptr);
fptr = fopen("file.txt", "rb");
fread(stud2, sizeof(stud2), 1, fptr);
for(i = 0; i < 5; ++i)
{
printf("Name: %s\nHeight: %d",
stud2[i].name, stud2[i].height);
}
fclose(fptr);
}
Program# 15: Using fgetc for reading a single character from file
#include<stdio.h>
#include<process.h>
void main() {
FILE
*fp1, *fp2;
char
a;
clrscr();
fp1
= fopen("test.txt", "r");
if (fp1
== NULL) {
puts("cannot
open this file");
exit(1);
}
fp2
= fopen("test1.txt", "w");
if (fp2
== NULL) {
puts("Not
able to open this file");
fclose(fp1);
exit(1);
}
do {
a
= fgetc(fp1);
fputc(a,
fp2);
} while
(a != EOF);
fcloseall();
getch();
}
|
0 Comments:
Post a Comment