-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.c
47 lines (36 loc) · 1 KB
/
fileio.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
#include "general.h"
#include "fileio.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int FileIODrv_echo_to_file(char* file, char* buff)
{
// Open direction file
int file_desc = open(file, O_WRONLY);
if (file_desc < 0) {
printf("FILEIODRV ERROR: Unable to open file for write: %s\n", file);
perror("Error is:");
exit(-1);
}
int bytes_written = write(file_desc, buff, strlen(buff));
die_on_failed("Unable to write to file for echo.");
// Close
close(file_desc);
return bytes_written;
}
// Returns number of bytes read into buff (null-terminated string)
int FileIODrv_read_str(char* file_name, char* buff, int max_length)
{
int file_desc = open(file_name, 0);
if (file_desc < 0) {
printf("FILEIODRV ERROR: Unable to open file for read: %s\n", file_name);
exit(-1);
}
// Read max-1 bytes so room for a null.
int bytes_read = read(file_desc, buff, max_length-1);
// Close
close(file_desc);
// Null terminate "string" being returned
buff[bytes_read] = 0;
return bytes_read;
}