file processing


Di C, Anda dapat melakukan empat operasi besar pada file, baik teks atau biner:

Membuat file baru
Membuka file yang ada
Menutup file
Membaca dari dan menulis informasi ke file

untuk melakannya kita memerlukan beberapa command khusus (dalam bahasa Inggris):

File ModeMeaning of ModeDuring Inexistence of file
rOpen for reading.If the file does not exist, fopen() returns NULL.
rbOpen for reading in binary mode.If the file does not exist, fopen() returns NULL.
wOpen for writing.If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wbOpen for writing in binary mode.If the file exists, its contents are overwritten. If the file does not exist, it will be created.
aOpen for append. i.e, Data is added to end of file.If the file does not exists, it will be created.
abOpen for append in binary mode. i.e, Data is added to end of file.If the file does not exists, it will be created.
r+Open for both reading and writing.If the file does not exist, fopen() returns NULL.
rb+Open for both reading and writing in binary mode.If the file does not exist, fopen() returns NULL.
w+Open for both reading and writing.If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wb+Open for both reading and writing in binary mode.If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+Open for both reading and appending.If the file does not exists, it will be created.
ab+Open for both reading and appending in binary mode.If the file does not exists, it will be created.
                 https://www.programiz.com/c-programming/c-file-input-output

Contoh Kodingannya:
#include <stdio.h>
#include<stdlib.h>
int main()
{
   int num;
   FILE *fptr;
   fptr = fopen("C:\\program.txt","w");

   if(fptr == NULL)
   {
      printf("Error!");   
      exit(1);             
   }

   printf("Enter num: ");
   scanf("%d",&num);

   fprintf(fptr,"%d",num);
   fclose(fptr);

   return 0;
}(kalau didalam file processing kita membutuhkan yang namanya file untuk membukanya seperti .txt,.abc dll. jika tidak maka kodingan tersebut akan gagal).

Comments

Popular posts from this blog

Repition dan Looping