You are on page 1of 5

// ALACSONY és MAGAS file-kezelés összehasonlítása

//-----------------------------------------------------------------------
// 1. példa: nyitás - csukás
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

int main()
{
// ALACSONYszintű file azonosító és file nyitás és zárás:
int fileaz=-10;
fileaz=open("num1.dat",
O_CREAT | O_RDWR | O_TRUNC | O_BINARY, // O_BINARY kell !!!
S_IREAD | S_IWRITE);
close (fileaz);

// NEM KELLENEK a ZÖLDEK (include-ok)


// MAGASszintű FILE típusú pointer és file nyitás és zárás:
FILE *fp=NULL; // FILE: egy előredefiniált típus (magyarázat később)
fp = fopen("num2.dat", "wb"); //buffer-be írunk, // OP.R. megoldja
fclose(fp); // 'b' kell !!!

printf("VEGE\n");
return 0;
}

//-----------------------------------------------------------------------
// 2. példa: írás

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

int main()
{
// ALACSONYszintű file azonosító és file nyitás és zárás, ÍRÁS:
int fileaz=-10, i=0x00112233;
fileaz=open("num1.dat",
O_CREAT | O_RDWR | O_TRUNC | O_BINARY, // O_BINARY kell !!!
S_IREAD | S_IWRITE);
write(fileaz,&i,1*sizeof(i));
close (fileaz);

// NEM KELLENEK a ZÖLDEK (include-ok)


// MAGASszintű FILE típusú pointer és file nyitás és zárás, ÍRÁS:
FILE *fp=NULL;
int y=0x55667788;
fp = fopen("num2.dat", "wb"); // 'b' kell !!!
fwrite(&y,1*sizeof(y),1,fp);
fclose(fp);

printf("VEGE\n");
return 0;
}

//-----------------------------------------------------------------------
// 3. példa: olvasás, visszaolvasás

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

int main()
{
// ALACSONYszintű file azonosító és file nyitás és zárás, OLVASÁS:
int fileaz=-10, i=0;
fileaz=open("num1.dat", O_RDONLY|O_BINARY); // O_BINARY kell !!!
read(fileaz,&i,1*sizeof(i));
close (fileaz);
printf("0x %x\n",i);

// NEM KELLENEK a ZÖLDEK (include-ok)


// MAGASszintű FILE típusú pointer és file nyitás és zárás, OLVASÁS:
FILE *fp=NULL;
int y=0;
fp = fopen("num2.dat", "rb"); // 'b' kell !!!
fread(&y,1*sizeof(y),1,fp);
fclose(fp);
printf("0x %x\n",y);
printf("VEGE\n");
return 0;
}

//-----------------------------------------------------------------------
// 4. példa: MOZGÁS A FILE-ban

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

int main()
{
FILE *fp=NULL; // text2.txt file létrehozása
char c=0;
fp = fopen("text2.txt", "wb"); // 'b' kell !!!
for (c='A';c<='Z';c++)
{
fwrite(&c,1*sizeof(c),1,fp);
}
fclose(fp);

// ALACSONYszintű file-ban MOZGÁS:


int fileaz=-10;
char x=0;
fileaz=open("text2.txt", O_RDONLY|O_BINARY); // O_BINARY kell !!!
lseek(fileaz, +3,SEEK_SET);
read(fileaz,&x,1*sizeof(x));
close (fileaz);
printf("%c\n",x);

// MAGASszintű FILE típusú pointer és file nyitás és zárás:


char y=0;
fp = fopen("text2.txt", "rb"); // 'b' kell !!!
fseek(fp,+3,SEEK_SET);
fread(&y,1*sizeof(y),1,fp);
fclose(fp);
printf("%c\n",y);
printf("VEGE\n");
return 0;
}

//-----------------------------------------------------------------------
// 5. példa: FORMÁTUMOZOTT MAGASszintű filekezelés

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
int main()
{
FILE *fp=NULL; // text2.txt file létrehozása
char c=0;
printf("NAGYBETUK es ASCII kodjaik\n");
printf("Kar.\tDEC.\tHEX.\tOCT.\n");

fp = fopen("text2.txt", "w"); // FONTOS!!! ITT TILOS A 'b' !!!


fprintf(fp,"NAGYBETUK es ASCII kodjaik\n");
fprintf(fp,"Kar.\tDEC.\tHEX.\tOCT.\n");

for (c='A';c<='Z';c++)
{
fprintf(stdout,"%c\t%d\t%x\t%o\n", c,c,c,c);
fprintf(fp ,"%c\t%d\t%x\t%o\n", c,c,c,c);
}
fclose(fp);
printf("VEGE\n");
return 0;
}

You might also like