Powered By Blogger

Friday, April 27, 2012

Program : 18


PROGRAM - 18

Write a program to print the output in the following format by giving the customer_ID as an input.

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
     FILE *fp;
     char line[200],ch,id[10],*f,fname[20];
     int i=0;
     clrscr();
     printf("enter source file name:");
     scanf("%s",&fname);
     fp=fopen(fname,"r");
     if(fp==NULL)
     {
           printf("file can't be opened");
           exit(0);
     }
     while((ch=fgetc(fp))!=EOF)
     {
           line[i]=ch;
           i++;
     }
     line[i]='\0';
     printf("content in file is:\n");
     printf("................................");
     printf("\nid name price");
     printf("\n...............................\n");
     printf("%s",line);
     printf("\nenter id:");
     scanf("%s",&id);
     f=strstr(line,id);
     i=(f-line);
     if(i<=strlen(line))
     {
          printf("...........\nid name price\n............\n");
           for( ;line[i]!='\n'&&line[i]!='\0';i++)
           {
                printf("%c",line[i]);
           }
     }
     else
     printf("invalid id");
     getch();
     fclose(fp);
}
/*
INPUT:enter source file name:item.txt
content in file is:
.... .......  ......
id    name   price
.... .......  ......
1.   c01     10
2.   c02     50
3.   c03     20
4.   c04     30
OUTPUT:
enter id:c02
..   .....   ....
id   item    price
..   .....   ....
c02    i2     50
*/

No comments:

Post a Comment