Earn by twitter on twivert

Sign up for PayPal and start accepting credit card payments instantly.
Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Wednesday, September 29, 2010

WAP to Prints file contents on printer


/*WAP to Prints file contents on printer*/


#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp ;
 char  ch ; 

 fp = fopen ( "poem.txt", "r" ) ;

 if ( fp == NULL )
 {
  printf ( "Cannot open file" ) ;
  exit( ) ;
 }

 while ( ( ch = fgetc ( fp ) ) != EOF )
  fputc ( ch, stdprn ) ;
  
 fclose ( fp ) ;
}

Monday, September 27, 2010

WAP to File-copy which copies text, .com and .exe files


/*WAP to File-copy which copies text, .com and .exe files */
#include "fcntl.h"
#include "types.h"  /* if present in sys directory use 
          "c:tc\\include\\sys\\types.h" */
#include "stat.h"  /* if present in sys directory use 
         "c:\\tc\\include\\sys\\stat.h" */

main ( int  argc, char  *argv[ ] )
{
 char  buffer[ 512 ], source [ 128 ], target [ 128 ] ;
 int  inhandle, outhandle, bytes ;

 printf ( "\nEnter source file name" ) ;
 gets ( source ) ;

 inhandle = open ( source, O_RDONLY | O_BINARY ) ;
 if ( inhandle == -1 )
 {
  puts ( "Cannot open file" ) ;
  exit( ) ;
 }

 printf ( "\nEnter target file name" ) ;
 gets ( target ) ;

 outhandle = open ( target, O_CREAT | O_BINARY | O_WRONLY,       S_IWRITE ) ;
 if ( inhandle == -1 )
 {
  puts ( "Cannot open file" ) ;
  close ( inhandle ) ;
  exit( ) ;
 }

 while ( 1 )
 {
  bytes = read ( inhandle, buffer, 512 ) ;

  if ( bytes > 0 )
   write ( outhandle, buffer, bytes ) ;
  else 
   break ;
 }

 close ( inhandle ) ;
 close ( outhandle ) ;
}

WAP of A menu-driven program for elementary database management


/*WAP of A menu-driven program for elementary database management */

#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp, *ft ;
 char  another, choice ; 
 struct emp 
 {
  char  name[40] ;
  int  age ;
  float  bs ;
 } ;
 struct emp  e ;
 char  empname[40] ;
 long int  recsize ;

 fp = fopen ( "EMP.DAT", "rb+" ) ;

 if ( fp == NULL )
 {
  fp = fopen ( "EMP.DAT", "wb+" ) ;

  if ( fp == NULL )
  {
   puts ( "Cannot open file" ) ;
   exit( ) ;
  }
 }

 recsize = sizeof ( e ) ;

 while ( 1 )
 {
  clrscr( ) ;

  gotoxy ( 30, 10 ) ;
  printf ( "1. Add Records" ) ;
  gotoxy ( 30, 12 ) ;
  printf ( "2. List Records" ) ;
  gotoxy ( 30, 14 ) ;
  printf ( "3. Modify Records" ) ;
  gotoxy ( 30, 16 ) ;
  printf ( "4. Delete Records" ) ;
  gotoxy ( 30, 18 ) ;
  printf ( "0. Exit" ) ;
  gotoxy ( 30, 20 ) ;
  printf ( "Your choice" ) ;

  fflush ( stdin ) ;
  choice = getche( ) ;
  switch ( choice )
  {
   case '1' :

    fseek ( fp, 0 , SEEK_END ) ;
    another = 'Y' ;

    while ( another == 'Y' )
    {
   printf ( "\nEnter name, age and basic sal. " ) ;
    scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
     fwrite ( &e, recsize, 1, fp ) ; 
    printf ( "\nAdd another Record (Y/N) " ) ;
     fflush ( stdin ) ;
     another = getche( ) ;
    }

    break ;
   
   case '2' :

    rewind ( fp ) ;

    while ( fread ( &e, recsize, 1, fp ) == 1 )
   printf ( "\n%s %d %f", e.name, e.age, e.bs ) ; 

    break ;

   case '3' :

    another = 'Y' ;
    while ( another == 'Y' )
    {
   printf ( "\nEnter name of employee to modify " ) ;
     scanf ( "%s", empname ) ;

     rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
     {   
     if ( strcmp ( e.name, empname ) == 0 )
      {
    printf ( "\nEnter new name, age & bs" ) ;
    scanf ( "%s %d %f", e.name, &e.age,&e.bs ) ;
     fseek ( fp, - recsize, SEEK_CUR ) ; 
      fwrite ( &e, recsize, 1, fp ) ; 
       break ; 
      }
     }

    printf ( "\nModify another Record (Y/N) " ) ;
     fflush ( stdin ) ;
     another = getche( ) ;
    }

    break ;
   
   case '4' :

    another = 'Y' ;
    while ( another == 'Y' )
    {
   printf ( "\nEnter name of employee to delete " ) ;
     scanf ( "%s", empname ) ;

     ft = fopen ( "TEMP.DAT", "wb" ) ;

     rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
     {
     if ( strcmp ( e.name, empname ) != 0 )
      fwrite ( &e, recsize, 1, ft ) ; 
     }

     fclose ( fp ) ;
     fclose ( ft ) ;
     remove ( "EMP.DAT" ) ;
     rename ( "TEMP.DAT", "EMP.DAT" ) ;

     fp = fopen ( "EMP.DAT", "rb+" ) ;

    printf ( "Delete another Record(Y/N) " ) ;
     fflush ( stdin ) ;
     another = getche( ) ;
    }
    break ;

   case '0' :
    fclose ( fp ) ;
    exit( ) ;
  }
 }
 getch();
}

WAP to read a file and write(copy) data in another file


/*WAP to read a file and write(copy) data in another file*/
#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fs, *ft ;
 char  ch ;

 fs = fopen ( "pr1.c", "r" ) ;
 if ( fs == NULL )
 {
  puts ( "Cannot open source file" ) ;
  exit( ) ;
 }

 ft = fopen ( "pr2.c", "w" ) ;
 if ( ft == NULL )
 {
  puts ( "Cannot open target file" ) ;
  fclose ( fs ) ;
  exit( ) ;
 }

 while ( 1 )
 {
  ch = fgetc ( fs ) ;

  if ( ch == EOF )
   break ;
  else
   fputc ( ch, ft ) ;
 }

 fclose ( fs ) ;
 fclose ( ft ) ;
       getch();
}

WAP to Count chars, spaces, tabs and newlines in a file


/*WAP to Count chars, spaces, tabs and newlines in a file*/
#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp ;
 char  ch ;
 int  nol = 0, not = 0, nob = 0, noc = 0 ;
 
 fp = fopen ( "PR1.C", "r" ) ;

 while ( 1 )
 {
  ch = fgetc ( fp ) ;

  if ( ch == EOF )
   break ;

  noc++ ;

  if ( ch == '  ' )
   nob++ ;

  if ( ch == '\n' )
   nol++ ;

  if ( ch == '\t' )
   not++ ;
 }

 fclose ( fp ) ;
 printf ( "\nNumber of characters = %d", noc ) ;
 printf ( "\nNumber of blanks = %d", nob ) ;
 printf ( "\nNumber of tabs = %d", not ) ;
 printf ( "\nNumber of lines = %d", nol ) ;
       getch();
}


WAP to check existing file empty or not


/*WAP to check existing file empty or not*/
#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp ;

 fp = fopen ( "PR1.C", "r" ) ;
 if ( fp == NULL )
 {
  puts ( "cannot open file" ) ;
  exit( ) ;
 }
       getch();
}

WAP to Display contents of a file on screen


/*WAP to Display contents of a file on screen*/
#include<stdio.h>
#include<conio.h>

main( )
{
 FILE  *fp ;
 char  ch ;
 
 fp = fopen ( "PR1.C", "r" ) ;

 while ( 1 )
 {
  ch = fgetc ( fp ) ;

  if ( ch == EOF )
   break ;

  printf ( "%c", ch ) ;
 }

 fclose ( fp ) ;
  getch();
}

ADS

How to earn online

How to earn online:
Step - 1 :
signup for PayPal, to recieve your online earning
click here:
https://www.paypal.com/in/mrb/pal=CZ7224TZBMCBL
step - 2 : Singup these 4 sites & earn & enjoy! Read site to know how to earn.
1. trekpay
Earn up to $0.02 (2 cents) per click.
http://www.trekpay.com/?ref=34690
2. neobux
Earn up to $0.02 (2 cents) per click.
Click here:
http://www.neobux.com/?r=Moneyearner786

AddMe

Bookmark and Share