0% found this document useful (0 votes)
23 views

C Write A Sentence To A File and Read The First Line From A File

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

C Write A Sentence To A File and Read The First Line From A File

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Program to 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);
}

// reads text until newline is encountered


fscanf(fptr, "%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(fptr);

return 0;
}

// Write a Sentence to a File

#include <stdio.h>
#include <stdlib.h>

int main() {
char sentence[1000];

// creating file pointer to work with files


FILE *fptr;

// opening file in writing mode


fptr = fopen("program.txt", "w");

// 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;
}

You might also like