File Management

  • Uploaded by: Derrick Morris
  • 0
  • 0
  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View File Management as PDF for free.

More details

  • Words: 612
  • Pages: 3
FILE MANAGEMENT What is afile? File ia a collection of data or set of characters may be a text or a program. 1. Sequential file. 2. Ramndom acess file. Opening and closing of files. fopen()-ment for opening a file. fclose()-ment for closing a file. What are the modes in case of file management? “w”-means write to the file, if the file mane exist already then it is deleted and a new file will be created. “r”-means read from a file. If the file does not exist, error is returned, i.e., NULL is returned as the value of fopen. “a”-means append a file. New data are added to the end of the file. “r+”-means open an existing file for updating. “w+”-means create new files for reading and writing. “a+”-means open for append, create if does not already exist. Write a program to copy the content of a file to append the content into another file. #include<stdio.h> main() { int alpha; file *fr,*fw,*fa; fr=fopen(“ss.doc”, “r”); /* existing file to be copied to another file*/ fw=fopen(“ws.doc”, “w”); /*copy the content of ss.doc to ws.doc*/ fa=fopen(“as.doc”, “a”); /*append the content of ss.doc to as.doc*/ do { alpha=fgetc(fr); fputc(alpha,fw); fputc(alpha,fa); putchar(alpha); } while(alpha!=eof); fclose(fr); fclose(fw);

fclose(fa); } Write a program to delete a file. #include<stdio.h> #include main() { file *in_file char s[20]; printf(“file name?\n”); scanf(“%s”, s); if((in_file=fopen(s, “r”))==null) { printf(“no such a file exist \n”); exist(); } else unlink(s); } Write short notes on fread() and fwrite(). 1.The fread() function is used to read a block of binary data from a given file. It is also used to read one or more stuctures from a given file and copies them to given memory location. The general format of the fread function is as follows: fread(ptr, size, nitems, fptr) Where ptr is a pointer to the location to receive the structure, size is the size (in bytes) of each structure to be read, nitems is the number of structures to be read fptr is a file pointer to the file to be read. The ptr may be a pointer to a variable of any type (from a single character to a structure). The size, an integer, should give the numbers of bytes in each item. The following program segment illustrates the fread() function #include<stdio.h> main() { file *out_file; struct database { char name[20]; int age; }; /*the structure can be defined within or outside of the main function*/

struct database person; …………………………… ………………………………… fread(&person,sizeof(person),2,out_file); ………………………………………. ……………………………………….. fclose(out_file); } Fread() function does not explicity indicate errors. So that feof() and ferror() functions should be used to detect end of the files or errors. 2.The fwrite() function is used to write a binary data to a given file. It is also used to write one or more structures to a given file. The general format of fwrite() function is as follows: fwrite(ptr,size,nitems,fptr) Where ptr is a pointer is the first structure to be written, size is the size(in bytes) of each structures, nitems is the number of structures to be written, fptr is the file pointer to the file. The ptr may be a pointer to a variable of any types(from a single character to a structure). The size should give the number of bytes in each item to be written. The fwrite() function always returns the number of items actually written to the file whether a not of end of the file or an error is encountered. The following program segment illustrates the fwrite(); #include<stdio.h> { file *out_file; struct database { char mane[20]; int age; }; /* the structure can be defined within or outside of the main function*/ struct database person; ………………………………….. ………………………………….. fwrite(&person,sizeof(person),2,out_file); ………………………………….. ………………………………….. fclose(out_file); }

Related Documents

File Management
October 2019 22
File Management
May 2020 18
File Management
July 2020 7
Module 3 File Management
October 2019 22
File Management In C
June 2020 10

More Documents from ""