Programming for Problem Solving

GTU Practical 59

59. Write a program to write a string in the file
#include<stdio.h>  ( this applies in the given code )

#include

int main() {
// Declare a file pointer
FILE *file;

// Open the file in write mode (‘w’). If the file doesn’t exist, it will be created.
file = fopen(“example.txt”, “w”);

// Check if the file is opened successfully
if (file == NULL) {
// If the file could not be opened, print an error message with more details
perror(“File aapko khud ke pc me banani padegi”);
return 1;
}

// Write a string to the file
fprintf(file, “This is a string that will be written to the file.”);

// Close the file after writing
fclose(file);

printf(“String written to file successfully!\n”);

return 0;
}

OUTPUT

File aapko khud ke pc me banani padegi: Permission denied

GTU STUDY