Answers for "c write to file"

C
13

write in file in c

#include<stdio.h>
int main(){
	FILE *out=fopen("name_of_file.txt","w");
	fputs("Hello File",out);
	fclose(out);
	return 0;
}
Posted by: Guest on April-30-2020
6

c write to 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;
}
Posted by: Guest on May-31-2020
3

c program to read and write to a file

/*Program to read from file using getc() function*/
#include <stdio.h>
int main() {
 FILE *fp;
 char ch;
 /*Open file in read mode*/
 fp= fopen ('example.txt', 'r');
 while( (ch = getc(fp)) != EOF) {
  /*getc() function reads a character and its value is stored in variable 'ch' until EOF is encountered*/
  printf('%ch', ch);
 }
  fclose(fp);
  return 0;
}
Posted by: Guest on March-17-2020
0

write to file in c programming

[...]
  printf("Enter name: \n");
  if (fgets(name, sizeof name, stdin)) {
    fputs(name,fileptr);
    fclose(fileptr);
    printf("File write was successful\n");
  } else {
    printf("Read error.\n");
  }
Posted by: Guest on May-09-2021

Code answers related to "C"

Browse Popular Code Answers by Language