ACP Unit-4 Programs


ACP----Unit 4 Programs

Note: 7 programs done..............!

1. Write a program to display contents of file on the screen. The
program should ask for file name. Display the contents in capital
case.
ANS
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char letter,file_name[100];
clrscr();
printf("Enter file name(with extention) = ");
gets(file_name);
fp=fopen(file_name,"r");
if(fp == NULL)
{
printf("\nFile doesn't exits...!");
getch();
exit(0);
}
printf("\n");
while(!feof(fp))
{
letter=getc(fp);
if(letter>='a' && letter<='z')
{
printf("%c",letter-32);
}
else
{
printf("%c",letter);
}
}
fclose(fp);
getch();

}


2. Write a program to find size of the file.
ANS
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char letter,file_name[100];
int count=0;
clrscr();
printf("Enter file name to find size = ");
gets(file_name);
fp=fopen(file_name,"rw");
if(fp==NULL)
{
printf("\nFile does not exits.......!");
getch();
exit(0);
}
while(!feof(fp))
{
letter=fgetc(fp);
count++;
}
printf("\nFile size in bytes = %d \n",count);
fclose(fp);
getch();
}

3. Write a program to combine contents of two files in a third file.
Add line number at the beginning of each line.
ANS
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file1,*file2,*file3;
char file_name[20],a;
int k=1;
clrscr();

file1=fopen("file1.txt","r");   //enter your file name
file2=fopen("file2.txt","r"); //enter your file name
printf("Enter New file name = ");
gets(file_name);
file3=fopen(file_name,"wa");
if(file1 == NULL || file2 == NULL || file3 == NULL)
{
printf("File does not exits...!");
getch();
exit(0);
}
while(!feof(file1))
{
if(a == '\n')
{ fprintf(file3,"%d",k); k++;}
a=fgetc(file1);
fprintf(file3,"%c",a);
}
k=1;
while(!feof(file2))
{
if(a == '\n')
{ fprintf(file3,"%d",k); k++;}
a=fgetc(file2);
fprintf(file3,"%c",a);
}

printf("\nDone....! files created");
fcloseall();
getch();
}

4. Write a program to display number 1 to 100. Redirect the output
of the program to text file.
ANS
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file;
char file_name[20];
int num=1;
clrscr();
printf("Enter New file name = ");
gets(file_name);
file=fopen(file_name,"w");
if(file == NULL)
{
printf("File does not exits...!");
getch();
exit(0);
}
for(num=1;num<=100;num++)
{
fprintf(file,"%d \n",num);
printf("%d\t",num);
}
fclose(file);
getch();
}



5. Write a program to write contents of one file in reverse into
another file.
ANS
//later


6. Write a program to count number of lines, words and characters
in a file.
ANS
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file;
char file_name[20],a;
int word=0,ch=0,line=0;
clrscr();
printf("Enter New file name = ");
gets(file_name);
file=fopen(file_name,"r");
if(file == NULL)
{
printf("File doesn't exits...!");
getch();
exit(0);
}
printf("Content of %s = \n",file_name);
while(!feof(file))
{
a=fgetc(file);
if(a == '\n')
{line++;}
if(a==' ')
{ word++; }
ch++;
printf("%c",a);

}
printf("\ncharacters = %d\nwords = %d\nlines = %d",ch,word,line+1);
getch();
}

7. Write a program to create a file called dictionary.dat that contains the information such as Name, Surname, City and Phone number. Write a program to accept a City from user and list details of persons having the given city.
ANS)

#include<stdio.h>
#include<conio.h>
struct detail
{
char name[10],city[10];
int no,mobile;
}person[3];
void main()
{
FILE *fp;
char file_name[20],search[20];
int i=0;
clrscr();
printf("Enter file name =  \n");
gets(file_name);

fp=fopen(file_name,"wr");
if(fp==NULL)
{
printf("\nFile doesn't exits......!");
}
for(i=0;i<2;i++)
{
printf("Enter no %d = ",i+1);
scanf("%d",&person[i].no);   flushall();
printf("Enter name %d = ",i+1);
gets(person[i].name);        flushall();
printf("Enter mobile %d = ",i+1);
scanf("%d",&person[i].mobile); flushall();
printf("Enter city %d = ",i+1);
gets(person[i].city);
}
printf("\nEnter which city u want search =");
gets(search);
printf("No\t Name\t City\t mobile \n");
for(i=0;i<3;i++)
{
if(strcmp(search,person[i].city) == 0)
{
printf("%d\t %s\t %s\t %d\n",person[i].no,person[i].name,person[i].city,person[i].mobile);
}

}
fclose(fp);
getch();
}

8. Write a program to copy one file to another. While doing so, all extra spaces in a file should be squeezed to one. For eg. If a file contains line “I am learning C”, it should be converted to “I am learning C”.
ANS)

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*tp;
char file_name[10],ch;
int space;

clrscr();

fp=fopen("any_file.txt","r");
printf("Enter file name for copy = ");
gets(file_name);
tp=fopen(file_name,"w");
if(fp == NULL || tp == NULL)
{
printf("File does not exits");
getch();
exit(0);
}
while(!feof(fp))
{
ch=fgetc(fp);
if(ch == ' ')
{
space++;
}
else
{
if(space>0)
{
fprintf(tp," ");
space=0;
}
fprintf(tp,"%c",ch);
}
}
printf("Done......!!");
fcloseall();
getch();
}

Comments

Popular posts from this blog

C++ Practice Program

Cloud Computing Important Question-Answer for University Exam

Software Testing Gujarat University Important Questions For Exam