Dosyaya yazma ve okuma işlemlerinde kullanılan fputc(), fgetc(), fputs(), fgets(), fprintf(), fscanf(), fread() ve fwrite() fonksiyonlarının kullanımını gerekli hata kontrol işlemleri ile birlikte tek bir örnek üzerinde incelemektedir.Bu proje C programlama dilinde yazılmıştır.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
  FILE *fp;
  int id, csize;
  char cdizi[][40]= { "Bilgisayar", "Programlama", "Sayısal", "Sistem" };
  char gdizi[40];
  unsigned char cd;
  double dd;
  // fputc() ve fgetc() fonksiyonları
  if ((fp = fopen ("dosya.txt", "w+")) == NULL) {
      printf("Dosya açma hatası!");
      exit(1);
  }
  for (id=0; id<strlen(cdizi[0]); id++) {
       if(fputc (cdizi[0][id], fp)==EOF) {
          printf("fputc() fonksiyonu dosyaya yazma hatası!");
          fclose(fp);
          exit(1);
       }
  }
  rewind(fp);
  while (!feof(fp)) {
     id = fgetc(fp);
     if (ferror(fp)) {
         printf("Kaynak dosyadan okuma hatası!");
         fclose(fp);
         exit(1);
     }
     if (id!=EOF) printf("%c", id);
  }
  fclose(fp);
  printf("\n");
  // fputs() ve fgets() fonksiyonları
  if ((fp = fopen ("dosya.txt", "w+")) == NULL) {
      printf("Dosya açma hatası!");
      exit(1);
  }
  csize = sizeof(cdizi)/sizeof(cdizi[0]);
  for (id=0; id<csize; id++) {
       // Karakter dizisi sonundaki '\0' karakteri dosyaya yazılmaz.
       if (fputs(cdizi[id], fp)==EOF) {
           printf("fputs() fonksiyonu dosyaya yazma hatası!");
           fclose(fp);
           exit(1);
       }
  }
  rewind(fp);
  for (id=0; id<csize; id++) {
       // Okunan karakterler karakter dizisine atandıktan sonra, dizi sonuna otomatik olarak NULL bir karakter ('\0') eklenir.
       fgets(gdizi, strlen(cdizi[id])+1, fp);
       if (ferror(fp)) {
           printf("fgets() fonksiyonu dosyadan okuma hatası!");
           fclose(fp);
           exit(1);
       }
       printf("%s", gdizi);
  }
  fclose(fp);
  printf("\n");
  // fprintf() ve fscanf() fonksiyonları
  if ((fp = fopen ("dosya.txt", "w+")) == NULL) {
      printf("Dosya açma hatası!");
      exit(1);
  }
  if (fprintf (fp,"%.3f %d %s %c", 654.123, 9852, "Bilgisayar", 'A')<0) {
      printf("fprintf() fonksiyonu dosyaya yazma hatası!");
      fclose(fp);
      exit(1);
  }
  rewind(fp);
  if (fscanf (fp,"%lf%d%s %c", &dd, &id, gdizi, &cd)==EOF) {
      printf("fscanf() fonksiyonu dosyadan okuma hatası!");
      fclose(fp);
      exit(1);
  }
  printf("%.3f %d %s %c\n", dd, id, gdizi, cd);
  fclose (fp);
  // fread() ve fwrite() fonksiyonları
  if ((fp = fopen ("dosya.txt", "w+")) == NULL) {
      printf("Dosya açma hatası!");
      exit(1);
  }
  if (fwrite(cdizi[0], strlen(cdizi[0]), 1, fp) != 1) {
      printf("Dosyaya yazma hatası!\n");
      exit(1);
  }
  rewind(fp);
  // Okunan karakterler karakter dizisine atandıktan sonra, dizi sonuna otomatik olarak NULL bir karakter ('\0') eklenmez.
  if (fread(gdizi, strlen(cdizi[0]), 1, fp) != 1) {
      printf("Dosyadan okuma hatası!\n");
      exit(1);
  }
  // Okunan karakter dizisi sonuna boş karakter ekleme
  gdizi[strlen(cdizi[0])] = '\0';
  printf("%s", gdizi);
  fclose (fp);
  return 0;
}
Daha fazla C programı için tıklayınız.
  
  
  
  
  

0 Yorumlar
Bizimle fikirlerinizi paylaşabilirsiniz.