22 March, 2015

NWP-Practical-5

                                                            Practical#5
AIM: WAP for file handling by using the concept of fscanf() and fprintf().
Theory: RHS
#include "stdio.h"
#include "io.h"
#include "stdlib.h"
#include "conio.h"

int main()
{
  FILE *fp;
  char s[80];
  int t;
  clrscr();
  if((fp=fopen("test", "w")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  printf("Enter a string and a number: ");
  fscanf(stdin, "%s%d", s, &t); /* read from keyboard */

  fprintf(fp, "%s %d", s, t); /* write to file */
  fclose(fp);

  if((fp=fopen("test","r")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  fscanf(fp, "%s%d", s, &t); /* read from file */
  fprintf(stdout, "%s %d", s, t); /* print on screen */
  getch();
  return 0;
}

Output: LHS

No comments:

Post a Comment