C – Writing String into File

Writing String into File:

fputs() is used to write a string on to the file.

void  fputs(char* str , FILE* stream);

#include<stdio.h>
int main()
{
            FILE *src, *dest;
            char str[10];
            src = fopen(“code.c”, “r”);
            if(src==NULL)
            {
                        printf(“No such file to open \n”);
            }
            else
            {
                        dest = fopen(“data.txt”, “w”);
                        while(fgets(str, 10, src))
                        {
                                    fputs(str, dest);
                        }
                        printf(“Contents copied…\n”); 
                        fclose(src);
                        fclose(dest);
            }
            return 0;
}
Scroll to Top