Anda di halaman 1dari 7

Assignment 9 FILE MANAGEMENT Program 1: Character-oriented read/write operation on a file using getc() and putc() #include<stdio.

h> void main() { FILE *f1; char c; f1 = fopen("input.txt","wb"); if(f1 == NULL) { printf("\nCannot open file...."); } while((c = getchar()) != EOF) putc(c,f1); fclose(f1); clrscr(); printf("\nCOntents of the file...\n"); f1 = fopen("input","rb"); while((c = getc(f1)) != EOF) printf("%c ",c); fclose(f1); getch(); }

Program 2: A file named DATA contains a series of integer numbers. Code a program to read these numbers and then write all odd numbers to a file called ODD and all even numbers to a file called EVEN Using getw() and putw() #include<stdio.h> void main() { FILE *f1,*f2,*f3; int number, i; printf("\nType the numbers to be saved in the file DATA"); f1 = fopen("DATA.txt","wb"); if(f1 == NULL) { printf("\nCannot open file...."); } printf("\nTYpe -1 to STOP...\n"); for(i = 1 ; i<= 30; i++) { scanf("%d",&number); if(number == -1) break; putw(number,f1); } fclose(f1); f1 = fopen("DATA.txt","rb"); f2 = fopen("DATA.txt","wb"); f3 = fopen("DATA.txt","wb"); //Read from the DATA file while((number = getw(f1)) != EOF) { if(number%2 == 0) { putw(number,f2);

} else { putw(number,f3); }

} fclose(f1); fclose(f2); fclose(f3); f2 = fopen("ODD.txt","rb"); printf("\nContents of the ODD file\n"); while(number = getw(f2) != EOF) printf("%d",number); getch(); }

Program 3: Program to open a file named INVENTIORY and store in it the following data Item Name, Number, Price, Quantity Using fscanf() and fprintf() #include<stdio.h> void main() { FILE *fp; int number, quantity, i; float price, value; char item[80],filename[80]; clrscr(); printf("\nInput the File Name"); gets(filename); fp = fopen(filename,"w"); if(fp == NULL) { printf("\nCannot open file...."); } printf("\nInput the THREE Inventory Data...\n"); for(i = 1 ; i <= 3; i++) { fscanf(stdin,"%s %d %f %d",item,&number,&price,&quantity); fprintf(fp,"%s %d %f %d",item,number,price,quantity); } fclose(fp); fprintf(stdout,"\n\n"); fp = fopen(filename,"r"); //Read from the ile printf("ITEM NAME NUmber Price Quantity Value\n"); for(i = 1 ; i <= 3; i++) { fscanf(fp,"%s %d %f %d",item,&number,&price,&quantity); value = price * quantity;

fprintf(stdout,"\n%-8s %7d %8.2f %8d %11.2f", item, number, price, quantity, value); } fclose(fp); getch(); }

Program 4: Program to demonstrate the use of fread() and fwrite() functions /* parametes in the fread and fwrite A memory address Number of bytes to read per block Number of blocks to read A file variable */ #include<stdio.h> /* Our structure */ struct rec { int x,y,z; }; int main() { int counter; FILE *ptr_myfile; struct rec my_record; ptr_myfile=fopen("test.bin","wb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } for ( counter=1; counter <= 10; counter++) { my_record.x= counter; fwrite(&my_record, sizeof(struct rec), 1, ptr_myfile); } fclose(ptr_myfile); clrscr(); printf("\nReading the contents....\n"); ptr_myfile=fopen("test.bin","rb"); if (!ptr_myfile)

{ printf("Unable to open file!"); return 1; } for ( counter=1; counter <= 10; counter++) { fread(&my_record,sizeof(struct rec),1,ptr_myfile); printf("%d\n",my_record.x); } fclose(ptr_myfile); return 0; }