Professional Documents
Culture Documents
Magas File
Magas File
//-----------------------------------------------------------------------
// 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);
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);
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);
//-----------------------------------------------------------------------
// 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);
//-----------------------------------------------------------------------
// 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");
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;
}