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
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.
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));
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;
}
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);
Related
So I'm using mmap to then write to another file. But the weird thing is, when my code hits mmap, what it does is clears the file. So I have a file that's populated with random characters (AB, HAA, JAK, etc...). What it's supposed to do is use mmap as read basically and then write that file to the new file. So that first if (argc == 3) is the normal read and write, the second if (argc ==4) is supposed to use mmap. Does anyone have any idea why on Earth this is happening?
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc, char const *argv[])
{
int nbyte = 512;
char buffer[nbyte];
unsigned char *f;
int bytesRead = 0;
int size;
int totalBuffer;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDONLY);
int i = 0;
char c;
int fileInput = open(argv[1], O_RDONLY);
int fileOutPut = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
fstat(fileInput, &s);
size = s.st_size;
printf("%d\n", size);
if (argc == 3)
{
printf("size: %d\n", size);
printf("nbyte: %d\n", nbyte);
while (size - bytesRead >= nbyte)
{
read(fileInput, buffer, nbyte);
bytesRead += nbyte;
write(fileOutPut, buffer, nbyte);
}
read(fileInput, buffer, size - bytesRead);
write(fileOutPut, buffer, size - bytesRead);
}
else if (argc == 4)
{
int i = 0;
printf("4 arg\n");
f = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fileInput, 0);
/* This is where it is being wipped */
}
close(fileInput);
close(fileOutPut);
int who = RUSAGE_SELF;
struct rusage usage;
int ret;
/* Get the status of the file and print some. Easy to do what "ls" does with fstat system call... */
int status = fstat (fd, & s);
printf("File Size: %d bytes\n",s.st_size);
printf("Number of Links: %d\n",s.st_nlink);
return 0;
}
EDIT: I wanted to mention that the first read and write works perfectly, it is only when you try to do it through the mmap.
If you mean it's clearing your destination file, then yes, that's exactly what your code will do.
It opens the destination with truncation and then, in your argc==4 section, you map the input file but do absolutely nothing to transfer the data to the output file.
You'll need a while loop of some description, similar to the one in the argc==3 case, but which writes the bytes in mapped memory to the fileOutput descriptor.
I have a very small C program which sends and receives newline-terminated ASCII strings to and from a serial device. It's plugged into my computer with a USB adapter, on /dev/ttyUSB0.
Most of the time it sends the commands just find, but occasionally it will capitalize all the lower-case letters to upper-case. It leaves all special characters alone.
The string I am sending is /home\n. About 1 out of every five times I run the program (by simply running ./a.out without recompiling), the sent message understood by the device is /HOME\n.
Here is my source code:
#include <stdio.h>
#include <stdlib.h>
#include "zserial.h"
int main() {
char buf[256];
int fd = connect("/dev/ttyUSB0");
char *cmd = "/home\n";
send(fd, cmd);
receive(fd, buf, 256);
puts(buf);
exit(0);
}
And zserial.c:
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zserial.h"
int send(int fd, char *buf) {
int len = strlen(buf);
int nb = write(fd, buf, len);
if (len != nb || nb < 1)
perror("Error: wrote no bytes!");
tcdrain(fd);
return nb;
}
int receive(int fd, char *dst, int nbytes) {
int i;
char c;
for(i = 0; i < nbytes;) {
int r = read(fd, &c, 1);
/* printf("Read %d bytes\n", r); */
if (r > 0) {
dst[i++] = c;
if (c == '\n') break;
}
}
dst[i] = 0; /* null-terminate the string */
return i;
}
int connect(char *portname) {
int fd;
struct termios tio;
fd = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
tio.c_cflag = CS8|CREAD|CLOCAL;
if ((cfsetospeed(&tio, B115200) & cfsetispeed(&tio, B115200)) < 0) {
perror("invalid baud rate");
exit(-1);
}
tcsetattr(fd, TCSANOW, &tio);
return fd;
}
What am I doing wrong? Is there some termios flag which modifies the output on a serial port?
c_oflag & OLCUC turns on the mapping of lowercase to uppercase on output. Since you never initialized tio, it's not surprising you got some random flags set.
You have two choices:
tcgetattr the current settings into a termios struct to initialize it, then modify the ones you're interested in, then write them back with tcsetattr
initialize all the termios fields to known values, not just c_cflag and the speed fields.
code:
#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
void write_zero(char * file, unsigned long bytes)
{
printf("Zeroing %s\n", file);
unsigned int wrote = 0, total = 0;
int fd, i, buf;
char obj = 0x00;
fd = open(file, O_RDWR, DEFFILEMODE);
lseek(fd, 0, SEEK_SET);
write(fd, &obj, bytes);
}
int main(int argc, char * * argv)
{
int fd;
unsigned long blocks = 0;
char check = 0x0;
fd = open(argv[1], O_RDONLY);
ioctl(fd, BLKGETSIZE, &blocks);
close(fd);
printf("Blocks: %lu\tBytes: %lu\tGB: %.2f\n",
blocks, blocks * 512, (double)blocks * 512.0 / (1024 * 1024 * 1024));
do
{
printf("Write 0x0 to %s? [y/N] ", argv[1]);
fflush(stdout);
}
while (scanf("%c", &check) < 1);
if (check == 'y')
{
write_zero(argv[1], blocks * 512);
}
}
I get nothing actually written to the device.. I copied my open line from the 'dd' source code, thinking maybe it was not opened right. dd can zero the device, but this program does not. Any ideas?
It seems like this has been beaten to death but
char obj = 0x00;
fd = open(file, O_RDWR, DEFFILEMODE);
lseek(fd, 0, SEEK_SET);
write(fd, &obj, bytes);
Is not going to write zeros. It's going to write garbage from the stack.
I am trying to write a very simple echo Linux driver.
The driver takes a maximum of 250 characters from command-line and just writes it into a dummy device 'mydev'. This is again read back from the device. The front end and driver code is pasted below for reference.
The issue is I am able to write but not read. There is no error on compilation or segmentation fault. But none of the messages in printk in the driver's read are printed. I am puzzled as to what is happening. Can I get some clue here?
I am just sharing the code copy for better clarity:
mydriver.c :
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
static int major;
static char kbuf[250];
static int dv_open(struct inode *inode, struct file *filp)
{
return 0;
}
static int dv_close(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t dv_read(struct file *filp, char __user *buf,
size_t sz, loff_t *fpos)
{
int r;
int L;
printk("READ:Entering\n");
L = strlen(kbuf);
r = copy_to_user(buf, kbuf, L);
printk("READ:Ends\n");
return L;
}
static ssize_t dv_write(struct file *filp, const char __user *buf,
size_t sz, loff_t *fpos)
{
int r, wr_sz;
printk("WRITE:Entering\n");
memset(kbuf,'\0', 250);
if ( sz <= 250 ) {
wr_sz = sz;
} else {
wr_sz = 250;
}
r = copy_from_user(kbuf, buf, wr_sz);
printk("WRITE:Rx buf = %s\n", kbuf);
return 0;
}
static struct file_operations dv_fops = {
.open = dv_open,
.release = dv_close,
.read = dv_read,
.write = dv_write,
.owner = THIS_MODULE,
};
int init_module(void)
{
major = register_chrdev(0, "dvdev", &dv_fops);
if ( major < 0 ) {
printk("Error in registering driver\n");
return -1;
}
else printk("Success. major = %d\n", major);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(major, "dvdev");
}
myuserapp.c
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
static char buf[250];
static char * wbuf;
int main(int argc, char **argv)
{
int fd;
int option;
int nbr = 0, len;
if ( argc != 2 ) {
printf("usage: front <devName>\n");
return -1;
}
fd = open("mydev", O_RDONLY | O_WRONLY);
if ( fd < 0 ) {
printf("Error opening file. %s does not exist\n", argv[1]);
return -2;
}
wbuf = argv[1];
len = strlen(wbuf);
nbr = write(fd, wbuf, len);
printf("USR: Buf written = %s, nbr = %d\n", wbuf ,nbr);
nbr = read(fd, buf, 250);
printf("USR RD: %s", buf);
close(fd);
return 0;
}
Your code has at least one error:
fd = open("mydev", O_RDONLY | O_WRONLY);
That is an improper open() call.
The man page for open() specifies that:
Applications shall specify exactly one of the first three values (file access modes) below in the value of oflag:
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
Instead of specifying only one, you have an expression of two values.
I guess O_RDONLY and O_WRONLY are bits individually, so it would be something like O_RDONLY | O_WRONLY = 10|01 = 11 . Both the bits of Read and write are set.
The bit values are irrelevant, since combining these values is not allowed.
You seem to be ignoring the exclusionary suffix "ONLY".
RDONLY means "allow read and disallow write".
WRONLY means "allow write and disallow read".
"O_RDONLY | O_WRONLY" is a logical contradiction.
If you want to allow both reading and writing, then you have to specify O_RDWR.
And Mark Stevens has provided the correct values and Boolean arithmetic to prove that your inproper expression is not equivalent to O_RDWR.
sawdust gave the right answer, but there's another problem in your code.
If you write 250 bytes to your device, the buffer is not null-terminated. Then, when reading, strlen will read beyond it, leading to an unexpected result.
Each calls to your write function will result in calling memset on the buffer.This is the root cause you are not able to retrieve the message.
I am writing a simple program to flip all the bits in a file, but right now it only does the first 1000 bytes until I get that much working. Why does my call to read() ignore \r characters? When I run this code on a file that only contains \r\n\r\n, the read call returns 2 and the buffer contains \n\n. The \r characters are completely ignored. I'm running this on Windows (this wouldn't even be an issue on Linux machines)
Why does read(2) skip over the \r character when it finds it? Or is that what is happening?
EDIT: Conclusion is that windows defaults to opening files in "text" mode as opposed to "binary" mode. For this reason, when calling open, we must specify O_BINARY as the mode.
Thanks, code below.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
void invertBytes(size_t amount, char* buffer);
int main(int argv, char** argc)
{
int fileCount = 1;
char* fileName;
int fd = 0;
size_t bufSize = 1000;
size_t amountRead = 0;
char* text;
int offset = 0;
if(argv <= 1)
{
printf("Usages: encode [filenames...]\n");
return 0;
}
text = (char *)malloc(sizeof(char) * bufSize);
for(fileCount = 1; fileCount < argv; fileCount++)
{
fileName = argc[fileCount];
fd = open(fileName, O_RDWR);
printf("fd: %d\n", fd);
amountRead = read(fd, (void *)text, bufSize);
printf("Amount read: %d\n", amountRead);
invertBytes(amountRead, text);
offset = (int)lseek(fd, 0, SEEK_SET);
printf("Lseek to %d\n", offset);
offset = write(fd, text, amountRead);
printf("write returned %d\n", offset);
close(fd);
}
return 0;
}
void invertBytes(size_t amount, char* buffer)
{
int byteCount = 0;
printf("amount: %d\n", amount);
for(byteCount = 0; byteCount < amount; byteCount++)
{
printf("%x, ", buffer[byteCount]);
buffer[byteCount] = ~buffer[byteCount];
printf("%x\r\n", buffer[byteCount]);
}
printf("byteCount: %d\n", byteCount);
}
fd = open(fileName, O_RDWR);
should be
fd = open(fileName, O_RDWR | O_BINARY);
See read() only reads a few bytes from file for details.
Try opening with O_BINARY to use binary mode, text mode may be default and may ignore \r.
open(fileName, O_RDWR|O_BINARY);