Reputation: 2581
I'm studying UNIX programming and was experimenting with read/write system calls. I have a file with a pair of integer:
4 5
and I wrote this code to read the numbers:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct prova {
int first;
int second;
} prova_t;
int main(void) {
int fd;
prova_t origin;
prova_t result;
ssize_t bytes_read;
size_t nbytes;
fd = open("file.bin", O_WRONLY | O_CREAT);
origin.first = 24;
origin.second = 3;
write(fd, &origin, sizeof(prova_t));
close(fd);
fd = open("file.bin", O_RDONLY);
nbytes = sizeof(prova_t);
/* 1.BAD */
bytes_read = read(fd, &result, nbytes);
write(STDOUT_FILENO, &(result.first), sizeof(int));
write(STDOUT_FILENO, &(result.second), sizeof(int));
close(fd);
/* 2.GOOD */
nbytes = sizeof(int);
bytes_read = read(fd, &(result.first), nbytes);
write(STDOUT_FILENO, &(result.first), bytes_read);
bytes_read = read(fd, &(result.second), nbytes);
write(STDOUT_FILENO, &(result.second), bytes_read);
return 0;
}
In my first attempt I tried to read the whole struct from file and write its members to stdout. In this way, along with the numbers, I get some weird characters
4 5
E�^�
In my second attempt I read the numbers one by one and there were no problems in the output.
Is there any way to read and write the struct using the first method?
Edit: I updated the code to reflect suggestion from other users but still getting strange characters instead of numbers
Upvotes: 2
Views: 18628
Reputation: 432
Include flatbuffers/util.h, there are save and load functions sepeartely
SaveFile(const char *name, const char *buf, size_t len,
bool binary);
LoadFile(const char *name, bool binary, std::string *buf);
Upvotes: 0
Reputation: 5169
First, let's do a hex dump to see what is really stored in the file.
hexdump -C b.txt
or od -t x2 -t c b.txt
are two examples (od is for octal dump, more common, less pretty output in my opinion)
00000000 34 20 35 0a |4 5.|
00000004
That's is what the file looks like if it was a created as an ASCII text file (such as using a text editor like vi). You can use man ascii
to double check the hexadecimal values.
Now if you had a binary file that only contains two 8-bit bytes, in the system's native byte ordering (e.g. little-endian for x86, big endian for MIPS, PA-RISC, 680x0) then the hexdump would look like:
00000000 04 05 |..|
00000004
Here is the code to both create (open & write) a binary file, and read it back.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h> /* uint32_t */
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
/* User has read & write perms, group and others have read permission */
const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
typedef struct prova {
uint32_t first;
uint32_t second;
} prova_t;
#define FILENAME "file.b"
/* 'Safe' write */
int safewrite( int fd, const void *p, size_t want) {
int ret;
errno = 0;
while (want) {
ret = write(fd, (uint8_t *)p, want);
if (ret <= 0) {
if (errno != EINTR && errno != EAGAIN) {
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
}
return 0;
}
int saferead(int fd, const void *p, size_t want) {
int ret;
errno = 0;
while (want) {
ret = read(fd, (uint8_t*)p, want);
if( ret == 0 )
return -1; /* EOF */
if (ret <= 0) {
if( errno != EINTR && errno != EAGAIN ) {
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
}
return 0;
}
int main(int argc, char **argv) {
int fd;
prova_t result;
size_t nbytes;
/* Create file */
fd = creat(FILENAME, mode);
if (fd < 0) {
fprintf(stderr, "Unable to open " FILENAME ": %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
nbytes = sizeof(prova_t);
result.first = 4;
result.second = 5;
if (0 != safewrite(fd, &result, nbytes)) {
fprintf(stderr, "Unable to write to " FILENAME ": %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
fd = -1;
/* Reopen and read from binary file */
fd = open(FILENAME, O_RDONLY);
nbytes = sizeof(prova_t);
if (0 != saferead(fd, &result, nbytes)) {
fprintf(stderr, "Unable to read file \"" FILENAME "\": %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
printf( "Read: %d %d (%#.02x%.02x)\n",
result.first, result.second,
result.first, result.second);
return EXIT_SUCCESS;
}
Now the data file contents look like:
00000000 04 00 00 00 05 00 00 00 |........|
00000008
Because the integers were specified as 32-bit integers (32-bits / 8 bits per byte = 4 bytes). I'm using a 64-bit system (little endian, x86), so I wanted to be explicit so the your results should match, assuming little-endian.
Upvotes: 5
Reputation: 55752
From the name of your file, I assume that you are trying to read a text file. read from unistd.h reads from binary files. If you are indeed trying to read from a text file, you should use fscanf or in ifstream
To read a struct in binary:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct prova {
int first;
int second;
} prova_t;
int main(void) {
int fd;
prova_t result;
ssize_t bytes_read;
size_t nbytes;
prova_t initial;
// create a binary file
fd = open("file.bin", O_WRONLY | O_CREAT);
initial.first = 4;
initial.second = 5;
write(fd, &initial, sizeof(prova_t));
close(fp);
// read it back
fd = open("file.bin", O_RDONLY);
nbytes = sizeof(prova_t);
bytes_read = read(fd, &result, nbytes);
write(STDOUT_FILENO, &(result.first), sizeof(int));
write(STDOUT_FILENO, &(result.second), sizeof(int));
close(fp);
return 0;
}
Upvotes: 0
Reputation: 146910
You tried to read to a struct containing two ints, by passing a pointer to some data and telling read that you had one int's worth of storage. The first should be
bytes_read = read(fd, &result, sizeof(prova_t));
Upvotes: 0