This blog is under construction

Saturday 29 June 2013

C program to write a string into a file

Write a C program to write a string in a file.


  #include <stdio.h>
  int main() {
        FILE *fp;
        char str[100];
        /* open a new file "file1.txt" in write mode */
        fp = fopen("file1.txt", "w");

        /* get the input from the user */
        printf("Enter your input:");
        fgets(str, 100, stdin);

        /* print the input string into the file */
        fprintf(fp,"%s", str);

        /* close the file */
        fclose(fp);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:Hello world
  jp@jp-VirtualBox:~/$ cat file1.txt 
  Hello world






SEE ALSO

    No comments:

    Post a Comment