Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3401626
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:02:19+00:00 2026-05-18T05:02:19+00:00

I’m studying UNIX programming and was experimenting with read/write system calls. I have a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:02:20+00:00Added an answer on May 18, 2026 at 5:02 am

    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, 680×0) 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.