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

1.Write a program to copy the contents of one file into another.

Program: void main(int arg,char *arr[]) { FILE *fs,*ft; char ch; clrscr(); if(arg!=3) { printf("Argument Missing ! Press key to exit."); getch(); exit(0); } fs = fopen(arr[1],"r"); if(fs==NULL) { printf("Cannot open source file ! Press key to exit."); getch(); exit(0); } ft = fopen(arr[2],"w"); if(ft==NULL) { printf("Cannot copy file ! Press key to exit."); fclose(fs); getch(); exit(0); } while(1)

{ ch = getc(fs); if(ch==EOF) { break; } else putc(ch,ft); } printf("File copied succesfully!"); fclose(fs); fclose(ft); } 2. Write a C program to read the input file from command prompt, using command line arguments. Program: void main(int arg,char *arr[]) { FILE *fs; Char ch; fs = fopen(arr[1],"r"); if(fs==NULL) { printf("Cannot open source file ! Press key to exit."); getch(); exit(0); } While(ch=getc(fs)!=EOF)

Printf(%c,ch); }

3) Write a program to read an input file and count the number of characters in the input file. Program: #include < stdio.h > #include < conio.h > #include < process.h > void main() { FILE *fs; int c=0; char c1; clrscr(); fs=fopen(t.txt,"r"); while(!feof(fs)) { c1=fgetc(fs); c++; }f close(fs); printf(\n total character count=%d,c); getch(); }

4) Write a C program to read data from the keyboard, write it to a file called INPUT, again read the same data from the INPUT file, and display it on the screen. Program: main() { FILE *f1; char c; printf("Data Input\n\n"); f1 = fopen("INPUT", "w"); while((c=getchar()) != EOF) putc(c,f1); fclose(f1); printf("\nData Output\n\n"); f1 = fopen("INPUT","r"); while((c=getc(f1)) != EOF) printf("%c",c); fclose(f1); }

5)Write a program to read a C program file and count the following in the complete C program Total number of statements, Total number of opening brackets.

Program: #include < stdio.h > #include < conio.h > #include < process.h > void main() { FILE *fs; int ob=0,smc=o; char c1; clrscr(); fs=fopen(t.txt,"r"); while(!feof(fs)) { c1=fgetc(fs); if(c1==;) smc++; if(c1=={) ob++; }f close(fs); printf(\n total no of semicolons =%d,smc); printf(\n total no of opening braces =%dob); getch(); }

6) Write a program to open a pre-existing file and add information at the end of file.Display the contents of the file before and after appending. Program: #include<stdio.h> #include<conio.h> main() { File *fp; char c; clrscr(); fp=fopen("data.txt","r"); if(fp==NULL) { printf("the file is not opening"); exit(1); } printf("\n the contents of the file before appending is:"); while(!feof(fp)) { c=fgetc(fp); printf("%c",c); } fp=fopen("data.txt","a"); printf("enter the string:"); while(c!='.') { c=getche();

fputc(c,fp); } fclose(fp); printf("\n contents of file after appending:"); fp=fopen("data.txt","r"); while(!feof(fp)) { c=fgetc(fp); printf("%c",c); } } 7) Write a C program to read a text file and convert the file contents in capital (upper case) and also calculate the total characters and lines. Program: #include<stdio.h> #include<conio.h> #include<ctype.h> #include<process.h> main() { FILE *fp; int l=0,c=0,ch; fp=fopen("data.txt:,"r"); clrscr(); printf("\n contents of 'c' program file in capital letters \n"); printf("==================================\n"); while((ch=fgetc(fp))!=EOF)

{ c++; if(ch=='\n') l++; printf("%c",toupper(ch)); } printf("\n total characters:%d",c); printf("\n total lines:%d",l); getch(); } 8) Write a program to copy up to 100 characters from a file to an output array. Program: #include<stdio.h> #include<conio.h> #include<process.h> #define size 100 void main() {File *f,*f2; static char c,ch[size]; int a=0,x=0; clrscr(); f=fopen("poem.txt","r"); f2=fopen("alpha.txt","w"); if(f==NULL || f2=NULL) { printf("cannot open the file"); exit(1);

} clrscr(); while(!feof(f)) { c=fgetc(f); x++; } if(x==99) { fcloseall(); exit(1); } else ch[s++]=c; } fclose(f); for(s=0;s<=100;s++) fputc(ch[s],f2); fclose(f2); } 9) Write a C program to read last n characters of the file using appropriate file function. Program: #include<stdio.h> #include<conio.h> #include<dos.h> void main() {

FILE *fp; int n,ch; clrscr(); fp=fopen("text.txt","r"); printf("\ncontents of file\n"); while((ch=fgetc(fp))!=EOF) printf("%c",ch); printf("\n how many characters including spaces would you like to skip?:"); scanf("%d",&n); fseek(fp,n-2,SEEK_END); printf("\n last %d characters of a file \n",n); while((ch=fgetc(fp))!=EOF) printf("%c",ch); fclose(fp); } 10) Write a C program to replace every 5th character of data file, using fseek( ) command. Program: #Iinclude<stdio.h> main() { FILE * fp; long number; char ch; fp=fopen(filename,w); number=5L;

while(feof(fp)==0) { fseek(fp,number,0); putc(A,fp); number=number+5L; }f close(fp); } 11.write a program for indexed sequential file for the employee database for the following operation: a) Add record b) Delete Record c) Search Record based on the department. Program: Typedef struct { Unsigned int empno; Char name[50]; Int salary; }st_rec; Void main() { Int addrecord(); Int removerecord(); Iint seachrecord(); Int ch; St_rec rec; FILE *fp; Fp=fopen(employee.rec,wb); Printf(\n enter the empno,name and salary);

/* read from key board write to a file, press ctrl+z to stop */ While(scanf(%u%c%d,rec.empno,rec.name,&rec.salary)!=EO F) Fwrite(&rec,sizeof(rec),1,fp); Fclose(fp); Printf(\n); /* read from file and write to a screen*/ Fp=fopen(employee.rec,rb); While(fread(&rec,sizeof(rec),1,fp) /* while loop terminates when fread returns 0 */ Printf(%5u%-10c%3d\n,rec.empno,rec.name,rec.salary); Printf( a) Add record b) Delete Record c) Search Record based on the department ); Printf(enter ur choice); Scanf(%d,&ch); Switch(ch) { Case a: int addrecord() { Printf(\n enter the empno,name and salary); /* read from key board write to a file, press ctrl+z to stop*/ While(scanf(%u%c%d,rec.empno,rec.name,&rec.salary)!=EO F) Fwrite(&rec,sizeof(rec),1,fp); } Fclose(fp); Break; Case b: int removerecord(const char * employeerecord.index); Break; Case c: int searchrecord() /* read from file and write to a screen*/

Fp=fopen(employee.rec,rb); While(fread(&rec,sizeof(rec),1,fp) /* while loop terminates when fread returns 0 */ Printf(%5u%10c%3d\n,rec.empno,rec.name,rec.salary); Fclose(fp); } } Fclose(fp); }

Q12. write a program to read the following data, to find the value of each item and display the contents of the file. Item code price quantity Pen 101 RS.20 5 Pencil 103 Rs.3 100. Program: #include<stdio.h> void main() { FILE *fp; int code,price,quantity,I; char item[10],filename[10]; float value; printf(\n enter file name); scanf(%s,filename); fp=fopen(filename, w); printf( enter data into file);

printf(item code price quantity); for(i=1;i<=2;i++) { fscanf(stdin,%s %d %d %d,item,&code,&price,&quantity); fprintf(fp,%s %d %2d %d , item,code,price,quantity); } fclose(fp); fprintf(stdout,\n\n); fp=fopen(filename,r); printf(item number price quantity value\n); for(i=1;i<=2;i++) { fscanf(fp,%s %d %d %d,item,&code,&price,&quantity); value=price*quantity; fprintf(stdout,%s %d %2d %d , item,code,price,quantity); } Fclose(fp); }

Q13. write a program to read last n characters from a file? Program: void main() { FILE *fp; Int n,ch; Fp=fopen(ram.txt,r); Printf(\n contents of file); While(ch=fgetc(fp)!=EOF)

Printf(\n contents of file); Printf(how many characters u want to skip); Scanf(%d,&n); Fseek(fp,n,SEEK_END); Printf(last %d characters of a file are,n); While(ch=fgetc(fp)!=EOF) Printf(\n contents of file); Fclose(fp); } 14) write a program to count no of wards, characters, lines in a file. Program: #include<stdio.h> int main() { FILE *fs,*ft; int c=0,w=1,s=0; char ch; clrscr(); fs=fopen("a.txt","r"); ft=fopen("b.txt","w"); while(!feof(fs)) { ch=fgetc(fs); if(ch>64&&ch<92||ch>97&&ch<123) c++; else if(ch==32||ch>46&&ch<57) w++;

else if(ch==46||ch==44||ch==63) s++; } fprintf(ft,"The File a.txt contains %d Characters\nIt contains %d words.And %d Sentences",c,w,s); fcloseall(); getch(); return 0; }

You might also like