EmbLogic's Blog

Use of fopen(), fgetc() and fputc().

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
FILE *fd1, *fd2;
char ch;int i;
fd1=fopen(argv[1],”r”);//the read file is to be read from command line arguments.(as this is the high level programming because in this we are using the fopen instead of open as fopen is the function which works on pointer and the return type of this function is address of the buffer. The syntax and the declaration will be check using the man fopen command).
fd2=fopen(“file2″,”w”);//another file into which we have to write

//for(;ch != EOF;)//we can use this also instead of do while loop.
do
{
printf(“%d\n”,i);

ch=fgetc(fd1);
fputc(ch,fd2);
i++;
}while(ch!=EOF);

fclose(fd1);
fclose(fd2);
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>