How to implement the function fputs() from stdio.h that write characters (string) to a file in C

1 Answer

0 votes
#include <stdio.h>
 
int my_fputs(char *s, FILE *fp);
  
int main(void)
{
    char buf[100] = "Write string to file";
    FILE *fp;
     
    fp = fopen("e:/test.txt", "w");
    my_fputs(buf, fp);
    fclose(fp);
    
    return 0;
}

int my_fputs(char *s, FILE *fp)
{
    int c;
    
    while ( (c = *s++) )
        putc(c, fp);

    return ferror(fp) ? EOF : 0;
}
  
/*
run:
    
check for the file: e:/test.txt
 
*/

 



answered Dec 22, 2015 by avibootz
edited Dec 22, 2015 by avibootz
...