Answers for "opening files in c though code"

PHP
7

write a binary file c

FILE *write_ptr;

write_ptr = fopen("test.bin","wb");  // w for write, b for binary

fwrite(buffer,sizeof(buffer),1,write_ptr); // write 10 bytes from our buffer
Posted by: Guest on May-15-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

how to take inputs and give outputs from a file in c

#include<stdio.h>

int main()
{
    FILE *fp;
    char ch;
    fp = fopen("one.txt", "w");
    printf("Enter data...");
    while( (ch = getchar()) != EOF) {
        putc(ch, fp);
    }
    fclose(fp);
    fp = fopen("one.txt", "r");
 
    while( (ch = getc(fp)! = EOF)
    printf("%c",ch);
    
    // closing the file pointer
    fclose(fp);
    
    return 0;
}
Posted by: Guest on November-11-2020

Browse Popular Code Answers by Language