3 Replies - 305 Views - Last Post: 03 November 2011 - 07:00 PM Rate Topic: -----

#1 pantera  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-June 11

problem i face is that putchar() not working

Posted 03 November 2011 - 04:31 PM

#include<stdio.h>
#include<ctype.h>
#define null 0
main()
 {
  char ch;
  FILE *fp;
  FILE *p;
  clrscr();
  fp=fopen("alpha.txt","w");
  if(fp==null)
   {
    exit();
   }
  else
   {
    printf("Enter th data and press ^z : ");
    while((ch=getchar())!=EOF)
     {
      putc(ch,fp);
     }
     fclose(fp);
   }
   fp=fopen("alpha.txt","r");
   p=fopen("beta.txt","w");
   while((ch=getc(fp))!=EOF)
    {
     if(islower(ch))
      {
       putc(toupper(ch),p);
      }
     else
      {
       putc(tolower(ch),p);
      }
    }
   fclose(fp);
   fclose(p);
   fp=fopen("beta.txt","r");
   if(fp==null)
    {
     printf(":(/>");
     exit();
    }
   else
   {
    while((ch=getc(p))!=EOF)
     {
      putchar(ch);//not working
     }
   fclose(p);
  }
   getch();
 }



Is This A Good Question/Topic? 0
  • +

Replies To: problem i face is that putchar() not working

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: problem i face is that putchar() not working

Posted 03 November 2011 - 05:37 PM

putchar(char) writes to stdout, so if your expecting data to be put in the file it won't be there. Also, look at this code:

fclose(p); //HERE!!
   fp=fopen("beta.txt","r");
   if(fp==null)
    {
     printf(":(/>");
     exit();
    }
   else
   {
    while((ch=getc(p))!=EOF) //HERE!!
     {
      putchar(ch);//not working
     }
   fclose(p);
  }



Your calling getc(p) on a closed file.

This post has been edited by blackcompe: 03 November 2011 - 05:39 PM

Was This Post Helpful? 1
  • +
  • -

#3 pantera  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-June 11

Re: problem i face is that putchar() not working

Posted 03 November 2011 - 06:00 PM

View Postblackcompe, on 03 November 2011 - 05:37 PM, said:

putchar(char) writes to stdout, so if your expecting data to be put in the file it won't be there. Also, look at this code:

fclose(p); //HERE!!
   fp=fopen("beta.txt","r");
   if(fp==null)
    {
     printf(":(/>");
     exit();
    }
   else
   {
    while((ch=getc(p))!=EOF) //HERE!!
     {
      putchar(ch);//not working
     }
   fclose(p);
  }



Your calling getc(p) on a closed file.

what is stdout?
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: problem i face is that putchar() not working

Posted 03 November 2011 - 07:00 PM

stdout is the output stream that writes to the console. stdin is the input stream where keyboard input goes.

This post has been edited by blackcompe: 03 November 2011 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1