#include <stdio.h>
int main() {
//59. Write a program to write a string in file
printf("\tWrite String In File\n");
printf("==========================================\n");
char str[100];
printf("\nEnter A String: ");
gets(str);
FILE *fp;
fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Error Opening File!\n");
return 1;
}
fprintf(fp, "%s", str);
fclose(fp);
printf("\n------------------------------------------\n");
printf("String Written To File Successfully!\n");
printf("------------------------------------------\n");
return 0;
}
main
function.printf
.str
with a size of 100.printf
and gets
, which reads a line of text from the standard input and stores it in the str
array.fp
and initializes it to NULL
.fopen
function and assigns the file pointer fp
to the opened file.fp
is NULL, indicating that the file failed to open. If it is NULL, it prints an error message and returns a value of 1 to indicate an error has occurred.str
array to the file using the fprintf
function.fclose
function.printf
and returns a value of 0 to indicate the program ran successfully.