Professional Documents
Culture Documents
C Write A Sentence To A File and Read The First Line From A File
C Write A Sentence To A File and Read The First Line From A File
#include <stdio.h>
#include <stdlib.h> // For exit() function
int main() {
char c[1000];
FILE *fptr;
if ((fptr = fopen("program.txt", "r")) == NULL) {
printf("Error! File cannot be opened.");
// Program exits if the file pointer returns NULL.
exit(1);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
// exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}