t.c
#include <stdio.h>
#include <errno.h>
int main(void)
{
FILE *fp;
char ch;
if(!(fp = fopen(__FILE__, "w+")))
{
fprintf(stderr, "%s\n", strerror(errno));
return(1);
}
fprintf(fp, "Hello World\n");
rewind(fp);
while((ch = getc(fp)) != EOF)
putchar(ch);
return(0);
}
OUTPUT:
Hello World
This should work even on codepad.org...
this is becase the file that you post is named 't.c'
and you have permission to write and read in this file
but can not create new files.. try out!
If you not understand yet.. try this;;
#include <stdio.h>
#include <errno.h>
int
main(void)
{
FILE *fp;
char ch;
if(!(fp = fopen("t.c", "r"))){
fprintf(stderr, "%s\n", strerror(errno));
return(1);
}
printf("I'm the file, my name is %s!\n", __FILE__);
printf("and my content is:\n");
while((ch = getc(fp)) != EOF)
putchar(ch);
return(0);
}
cya