Initial commit
This commit is contained in:
32
kapitel12/lasa-fil-ex3.c
Normal file
32
kapitel12/lasa-fil-ex3.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
FILE *fp;
|
||||
off_t filesize;
|
||||
int fd, readSize;
|
||||
struct stat fileinfo;
|
||||
char* buffer;
|
||||
fp = fopen("test.txt", "r");
|
||||
|
||||
fd = fileno(fp); /* Hämta fildeskriptorn och spara i fd */
|
||||
fstat(fd, &fileinfo); /* Hämta all information till 'fileinfo' */
|
||||
filesize = fileinfo.st_size; /* Spara filens storlek i filesize */
|
||||
/* från 'fileinfo.st_size' */
|
||||
|
||||
buffer = calloc(sizeof(char), filesize+1);
|
||||
readSize = fread(buffer, sizeof(char), filesize, fp);
|
||||
buffer[filesize] = '\0';
|
||||
|
||||
printf("%s\n\n", buffer);
|
||||
printf("Fildeskriptor: %d\n", fd);
|
||||
printf("Filens storlek är %ld byte och vi har läst in %d byte\n",
|
||||
(long int)filesize, readSize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user