forked from sudikrt/assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_6_readfile.c
50 lines (45 loc) · 1.21 KB
/
2_6_readfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
char* read_from_file (const char* file_name, size_t length)
{
char* buffer;
int fd;
ssize_t bytes_read;
/* Allocate the buffer. */
buffer = (char*) malloc (length);
if (buffer == NULL)
return NULL;
/* Open the file. */
fd = open (file_name, O_RDONLY);
/* Check for the failure of opening file. */
if (fd == -1)
{
free (buffer);
return NULL;
}
/* Read the data */
bytes_read = read (fd, buffer, length);
/* Again check for the bytes read */
if (bytes_read != length)
{
free (buffer);
close (fd);
return NULL;
}
/* if Everything goes fine then. */
close (fd);
return buffer;
}
int main ()
{
const char* filename = "2_1_arglist.c";
int fd,len=100;
fd = open (filename, O_RDONLY);
printf ("Read from file is :\n ");
printf ("%s", read_from_file (filename, len));
return 0;
}