Tuesday 1 May 2012

Files in C

Files in C
A file is a collection of records stored on a secondary storage device.  The collection of bytes may be interpreted(translated), for example, as characters, words, lines, paragraphs and pages from a text document; fields and records belonging to a database; or pixels from a graphical image. 
types of files
1. Text files
2. Binary files
Text Files

A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time.
Binary Files
A binary file is no different to a text file. It is a collection of bytes. In C Programming Language a byte and a character are equivalent. No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed. C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer.


predefined functions in files

Function Name                     meaning
fopen()                              Creates a new file. Opens an existing file.
fclose                                Closes a file already opened file
getc()                                Reads each character from a file
putc()                                Writes each character into a file
fprintf()                              Writes the data into a file
fscanf()                             Reads data from a file
getw()                              Reads a integer from a file
putw()                             Writes an integer value into the file
fseek()                             Sets the position to a desired point in the file
ftell()                               Gives the current position in the file
rewind()                            Sets the position to the beginning of the file


Example
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char c;
myfile = fopen("raj.txt", "r");
if (fp == NULL) 

{
  printf("File doesn't exist\n");

else 
{
 do 

 {
  c = getc(fp);

putchar(c);

} while (c != EOF);

}
fclose(fp);
getch();

return(0);
}


//coying from one file to another file

#include<stdio.h>
#include<conio.h>
void main()
{
  FILE *p,*q;
  char f1[20],f2[20];
  char ch;
  printf("\nEnter the source file name to be copied:");
  gets(f1);
  p=fopen(f1,"r");
  if(p==NULL)
  {
      printf("cannot open %s",f1);
      exit(0);
  }
  printf("\nEnter the destination file name:");
  gets(f2);
  q=fopen(f2,"w");
  if(q==NULL){
      printf("cannot open %s",f2);
      exit(0);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);

}


1. Write a c program to open a file and write some text and close its.
2.  Write a c program to delete a file.
3. Write a c program to copy a file from one location to other location.
4. Write a c program to copy a data of file to other file.
5. Write a c program which display source code as a output.
6. Write a c program which writes string in the file.
7. Write a c program which reads string from file.
8. Write a c program which writes array in the file.
9. Write a c program which concatenate two file and write it third file.
10. Write a c program to find out size of any file.
11. Write a c program to know type of file.
12. Write a c program to know permission of any file.
13. Write a c program to know last date of modification of any file.
14. Write a c program to find size and drive of any file.
15. Big list of c program examples
pointers
Programs on files
c programs