Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

Thursday, July 21, 2011

Program to display contents of a file

/*Serial No.110     [swami87.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char ch;
clrscr();
FILE *fp1;
fp1=fopen("swami1.dat","r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
printf("Contents of file are:\n\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
    break;
    else
    printf("%c",ch);
    }
getch();
}

Program to copy two files into single another file

/*Serial No.109     [swami86.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i;
char ch,name[10];
clrscr();
FILE *fp1,*fp2,*fp3;
fp1=fopen("swami1.dat","r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
fp3=fopen("swami3.dat","w");
if(fp3==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp3);
    exit;
    }
printf("file is being copied.\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp3);
    }
fclose(fp1);
fclose(fp3);
fp2=fopen("swami2.dat","r");
if(fp2==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp2);
    exit;
    }
fp3=fopen("swami3.dat","a+");
if(fp3==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp3);
    exit;
    }
printf("second file is being copied.\n");
while(1)
    {
    ch=fgetc(fp2);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp3);
    }
fclose(fp2);
fclose(fp3);
getch();
}

Program to count no of characters, blank spaces, lines in a file

/*Serial No.104     [swami81.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i;
char ch,c=0,nl=1,name[20],bs=0;
clrscr();
FILE *fp1;
printf("Enter the name of file=%s\n",name);
gets(name);       //name is FILE NAME.
fp1=fopen(name,"r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
printf("file data are as follows:\n\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
       break;
    else
       c++;          // c is no. of characters.
       if(ch=='\n')
         {
         nl++;       // nl is no. of lines.
         }
       if(ch==' ')
         {
         bs++;       // bs is no. of blank spaces.
         }
    }
printf("No of characters in file=%d\n\n",c);
printf("No of blank spaces in file=%d\n\n",bs-1);
printf("No of characters in file includes blank spaces,tabs,new line characters,\nevery charater\n\n");
printf("No of lines in file=%d\n",nl);
fclose(fp1);
getch();
}

Wednesday, July 20, 2011

Program to copy one file to another file

/*Serial No.90     [swami65.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i;
char ch,name[10];
clrscr();
FILE *fp1,*fp2;
fp1=fopen("swami1.dat","r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
fp2=fopen("swami2.dat","w");
if(fp2==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp2);
    exit;
    }

printf("file is being copied.\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp2);
    }
fclose(fp1);
fclose(fp2);
getch();
}

Program to write data in a file and search data from file

/*Serial No.75     [swami54.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int r,found;
struct stud
    {
    char name[30];
    int age;
    int roll_no;
    }st;
FILE *fp;
fp=fopen("test.dat","r+t");
        if(fp==NULL)
            {
            printf("\nCannot open file\n");
            exit(1);
            }

printf("enter role no:");
found=0;
scanf("%d",&r);

while((!feof(fp))&&(found))
    {
    fread(&st,sizeof(stud),1,fp);
    if(st.roll_no==r)
    {
    fseek(fp,-sizeof(stud),SEEK_CUR);
    printf("enter new name\n");
    scanf("%s",st.name);
    fwrite(fp,sizeof(stud),1,fp);
    found=1;
    }
    }
    if(!found)
    printf("record not present");
    fclose(fp);
}

Program to store students record in a text file

/*Serial No.73     [swami45.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    FILE *fp;
    char another='y';
    int marks1,marks2,marks3,marks4,marks5,role;
    char name[20];
    clrscr();

    fp=fopen("Students.txt","w");
    if(fp==NULL)
    {
        puts("Cannot open file");
        exit(1);
    }
       fprintf(fp,"Role No.\tNAME\t  PHYSICS  CHEMISTRY  MATHS  S.S.T  ENGLISH\n");
    while(another=='y')
    {
        printf("\nEnter name :");
        scanf("%s",&name);
        printf("Enter your Role No.:");
        scanf("%d",&role);
        printf("Enter marks in Physics:");
        scanf("%d",&marks1);
        printf("Enter marks in Chemistry:");
        scanf("%d",&marks2);
        printf("Enter marks in Mathematics:");
        scanf("%d",&marks3);
        printf("Enter marks in Social Science:");
        scanf("%d",&marks4);
        printf("Enter marks in English:");
        scanf("%d",&marks5);
        fprintf(fp,"%d\t%s\t%d\t%d\t%d\t%d\t%d\n",role, name, marks1,marks2,marks3,marks4,marks5);
        printf("Add another record (y/n): ");
        fflush(stdin);
        another=getche();
    }
    fclose(fp);
}