#include <stdio.h>
#include <stdlib.h>
int main()
{
  FILE *fp;
  char ch;
  
  fp=fopen("exp.txt","w");

  while( (ch=getchar()) !=EOF)
  {
          
    putc(ch,fp);
     
  }
  
  fclose(fp);
  return 0;
}

I am using gcc compiler in linux mint
This code is not working only exp.txt is created which is empty.

exp.txt is created which is empty.

Since "ch" has no value you don't write anything in the file

Since "ch" has no value you don't write anything in the file

using getchar() we are taking input from the keyboard from the user

till it reaches the eof


but problem is how to take eof from the keyboard

What value do you get when you "take eof from the keyboard"?

#include <stdio.h>
#include <stdlib.h>
int main()
{
  FILE *fp;
  char ch;
  
  fp=fopen("exp.txt","w");

  while( (ch=getchar()) !=EOF)
  {
          
    putc(ch,fp);
     
  }
  
  fclose(fp);
  return 0;
}

I am using gcc compiler in linux mint
This code is not working only exp.txt is created which is empty.

your program is correct , just press CTRL+D at the end of your input.

however need to check whether file open is success.
fp = fopen("exp.txt","w");
if ( fp ) {
do file operations
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.