C programming: C program for appending a line to a text file.

#include<stdio.h>
#include<conio.h>

//NOTE:if using TurboC append.txt should be stored in C:\TC\BIN
void main()
{
FILE *fp;
char c;
clrscr();

fp=fopen("append.txt","a"); //opens append.txt in append mode

printf("Enter a string ending with character $\n");
while((c=getchar())!='$')
  putc(c,fp);  //append in the file append.txt

fclose(fp); //close the file pointed by fp
getch();
}

 

C programming : C program to copy one file to another.

#include<stdio.h>
#include<conio.h>

//NOTE: if using TurboC both file.txt and copy.txt are in C:\TC\BIN

void main()
{
FILE *fp1,*fp2;
char c;
clrscr();

fp1=fopen("file.txt","r"); //opens file.txt in read mode
fp2=fopen("copy.txt","w"); //creates and opens copy.txt in write mode

if(fp1==0)
  printf("File does not exist\n");
else
{
  while(!feof(fp1))
  {
    c=getc(fp1); //get every character of file.txt
    putc(c,fp2); //put every character of file.txt in copy.txt for copying
  }
fclose(fp1); //close file pointed by fp1
fclose(fp2); //close file pointed by fp2
}
getch();

}

 

Write a program to count the number of lines and characters in a text file.

#include<stdio.h>
#include<conio.h>

//NOTE:if using TurboC count.txt is in C:\TC\BIN
void main()
{
FILE *fp;
char c;
int ccount=0,lcount=0;
clrscr();

fp=fopen("count.txt","r"); //open count.txt file in read mode

if(fp==0)
  printf("File does not exist\n");
else
{
  //count no. of lines and no. of characters
  while(!feof(fp))
  {
    c=getc(fp); //get every character of file count.txt
    putchar(c); //print every character of file count.txt

    if(c=='\n')
      lcount++;
    else
      ccount++;
  }
printf("\nNumber of lines=%d\n",lcount);
printf("\nNumber of characters=%d\n",ccount);

}
getch();
fclose(fp); //close the file pointed by fp
}

 

Write a program that reads data from a text file and prints them. The program should also write the count of vowels, consonants, digits and other characters from the read text into another text file.

#include<stdio.h>
#include<conio.h>

//NOTE: if using TurboC char.txt file should be stored in C:\TC\BIN

void main()
{
FILE *fp;
char c;
int vcount=0,ccount=0,dcount=0,ocount=0;
clrscr();

fp=fopen("char.txt","r");  //opens char.txt in read mode

if(fp==0)
  printf("File does not exist\n");
else
{
  while(!feof(fp))
  {
    c=getc(fp);
    putchar(c);

    if(c>='A'&& c<='Z' || c>='a'&& c<='z') //check if the char is an alphabet
    {
      if(c=='A'|| c=='E'|| c=='I'|| c=='O'|| c=='U'||
   c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u')
       //check if the char is a vowel
    vcount++;

      else
      //if char is an alphabet but not a vowel then it is a consonant
    ccount++;
    }
    else //if not an alphabet
    {
      if(c>='0' && c<='9') //check if char is a digit
    dcount++;
      else   //if char is not a digit then it is an other character
    ocount++;
    }
  }
printf("\nNumber of vowels=%d\n",vcount);
printf("\nNumber of consonants=%d\n",ccount);
printf("\nNumber of digits=%d\n",dcount);
printf("\nNumber of other characters=%d\n",ocount);

}
getch();
fclose(fp); //close the file pointed by fp
}

 

Write a program that reads student name and marks scored in IA1, IA2, TW separately stored in a file and stores data along with total marks in another file.

#include<stdio.h>
#include<conio.h>
#include<string.h>

/* NOTE: if using TurboC student1.txt and student2.txt are stored and created
respectively in C:\TC\BIN
Sample of student1.txt file is
Rita
20 20 24
Shyam
18 18 22
Geeta
15 15 21
Raj
12 12 19 */

struct student
{
 char name[50];
 int ia1,ia2,tw,total;
};

void main()
{
FILE *fp1,*fp2; //fp1 and fp2 are file pointers
struct student s;
clrscr();

fp1=fopen("student1.txt","r"); //opens student1.txt in read mode
fp2=fopen("student2.txt","w"); //opens student2.txt in write mode

if(fp1==0)
  printf("File does not exist\n");
else
{
  //read information from student1.txt and write to student2.txt
  while(1)
  {
    fscanf(fp1," %[^\n]",s.name);
    fscanf(fp1," %d",&s.ia1);
    fscanf(fp1,"%d",&s.ia2);
    fscanf(fp1,"%d",&s.tw);

    if(feof(fp1))
      break;

    fprintf(fp2,"%s",s.name);
    fprintf(fp2," %d",s.ia1);
    fprintf(fp2," %d",s.ia2);
    fprintf(fp2," %d",s.tw);
    fprintf(fp2," %d\n",s.ia1+s.ia2+s.tw);

  }
fclose(fp1); //close file pointed by fp1
fclose(fp2); //close file pointed by fp2
}
getch();

}

 

Write a program that reads integer numbers from a text file and prints the largest and smallest on the output device.

#include<stdio.h>
#include<conio.h>

/* NOTE:if using TurboC this file should be stored in C:\TC\BIN
Here the file int.txt contains numbers 1,2,3,4,16,-5,0,12,90,-10 */

void main()
{
FILE *fp; //fp is a file pointer
int max,min,count=0,n;
clrscr();

fp=fopen("int.txt","r");//opens int.txt file in read mode
if(fp==0)
  printf("File does not exist\n");
else
{
  while(1)
  {
    fscanf(fp,"%d",&n);
    if(feof(fp)) //feof() checks if it is end of file
      break;

      /* find maximum and minimum number from the file */
    if(count==0)
    {
      max=min=n;
      count++;
    }
    else
    {
      if(n>max)
  max=n;
      else
  if(n<min)
    min=n;
    }
  }
  printf("Highest number=%d\n",max);
  printf("Lowest number=%d\n",min);
}
getch();
fclose(fp); //close the file pointed by fp
}

 

Write a program that reads a line of text and writes its uppercase version into a text file.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

//NOTE: if using TurboC this file is created at C:\TC\BIN
void main()
{
FILE *fp;
char c;
clrscr();

//fp is a file pointer
fp=fopen("uppercase.txt","w"); //creates and writes in uppercase.txt file
printf("Enter string\n");
do
{
  c=getchar();
  putc(toupper(c),fp); //converts every character of uppercase.txt file to upper case
}
while(c!='\n');
getch();
fclose(fp); //close the file pointed by fp
}

 

A hospital needs to maintain details of patients – First Name, Middle Name, Surname, Date of Birth, Disease. Write a program that will print list of all patients with a given disease.

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct patient //structure for a patient
{
  char fname[20],mname[20],surname[20],dob[10],disease[15];
};

void main()
{
struct patient p[20];
int i,n,flag=0;
char search_disease[15];
clrscr();

printf("Enter no. of patient records\n");
scanf("%d",&n);
printf("Enter patient records\n");
//take input for patient records
for(i=0;i<n;i++)
{
  printf("\nFirst Name=");
  scanf("%s", p[i].fname);

  printf("\nMiddle name=");
  scanf("%s",p[i].mname);

  printf("\nSurname=");
  scanf("%s",p[i].surname);

  printf("\nDate of birth=");
  scanf("%s",p[i].dob);

  printf("\nDisease=");
  scanf("%s",p[i].disease);
}

printf("Enter disease to be seached\n");
//take input of disease to be searched
scanf("%s",search_disease);

for(i=0;i<n;i++)
{
//search the disease by comparing with each disease in array
  if(strcmp(p[i].disease,search_disease)==0)
  {
    flag++;
    printf("%s \t %s \t %s \t %s \n",p[i].fname,p[i].mname,p[i].surname,p[i].dob);
  }
}

if(flag==0)
  printf("No record of patient with given disease found\n");

getch();
}

 

Define a structure cricket which consists of following members:- i)player name ii)country name iii)batting average Input 20 player information of test playing country. Write a program that will display detailed information of player with given player name.

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct cricket  //structure for cricket
{
  int average;
  char name[40],country[20];
};

void main()
{
struct cricket c[20]; //array of type structure cricket
int i;
char player_name[20];
clrscr();

printf("Enter player records\n");
//take input of player records
for(i=0;i<20;i++)
{
  printf("\nName=");
  scanf("%s", c[i].name);

  printf("\nCountry=");
  scanf("%s",c[i].country);

  printf("\nAverage=");
  scanf("%d",&c[i].average);
}

printf("Enter player name whose information is needed\n");
scanf("%s",player_name);
//take input of player name whose information is needed
for(i=0;i<20;i++)
{
  if(strcmp(c[i].name,player_name)==0)
  //compare player name from array with the player's name whose information is needed
    break;

}
if(i<20) //is required player is found print his information
  printf("%s \t %s \t %d \n",c[i].name,c[i].country,c[i].average);
else  //else print no record found
  printf("No record of given player found\n");


getch();
}

 

A sports club of cricket needs to maintain data about players – player’s name, age, number of matches played, number of runs, average. Make a structure for the same.

#include<stdio.h>
#include<conio.h>

struct cricket //structure for cricket
{
  int age,no_of_matches,runs,average;
  char name[40];
};

void main()
{
struct cricket c[20];  //array of type structure cricket
int i,n;
clrscr();

printf("Enter number of player records to be read\n");
scanf("%d",&n);
printf("Enter player records\n");
//take input of player records
for(i=0;i<n;i++)
{
  printf("\nName=");
  scanf("%s", c[i].name);

  printf("\nAge=");
  scanf("%d",&c[i].age);

  printf("\nNo. of matches played=");
  scanf("%d",&c[i].no_of_matches);

  printf("\nNo. of runs=");
  scanf("%d",&c[i].runs);

  printf("\nAverage=");
  scanf("%d",&c[i].average);
}
//print player records
printf("\nName \t Age \t No. of matches played \t Runs \t Average\n");
for(i=0;i<n;i++)
{
  printf("%s \t %d \t %d \t\t\t %d \t %d \n",c[i].name,c[i].age,c[i].no_of_matches,c[i].runs,c[i].average);
}

getch();
}