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 6866371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:10:30+00:00 2026-05-27T03:10:30+00:00

I am having a weird issue when i do a pwrite a struct to

  • 0

I am having a weird issue when i do a pwrite a struct to a file. It adds a single byte with next to a char entry in the struct. When i tried to write the char alone to a file it correctly wrote a single byte. Could some one tell me why the single byte got added??

   int main(){

   typedef struct pcap_hdr_s {
   guint32 magic_number;   /* magic number */
   guint16 version_major;  /* major version number */
   guint16 version_minor;  /* minor version number */
   gint32  thiszone;       /* GMT to local correction */
   guint32 sigfigs;        /* accuracy of timestamps */
   guint32 snaplen;        /* max length of captured packets, in octets */
   guint32 network;        /* data link type */

   guint32 ts_sec;         /* timestamp seconds */
   guint32 ts_usec;        /* timestamp microseconds */
   guint32 incl_len;       /* number of octets of packet saved in file */
   guint32 orig_len;       /* actual length of packet */

   guint16 fcf;
   char seqno;
   guint16 dpan;
   guint16 daddr;
   guint16 saddr;
   gint16  payload_data;
   } pcaprec_hdr_t;

   pcaprec_hdr_t packet_header;

   packet_header.magic_number      =       PCAP_MAGIC;
   packet_header.version_major     =       2;
   packet_header.version_minor     =       4;
   packet_header.thiszone          =       0;
   packet_header.sigfigs           =       0;
   packet_header.snaplen           =       65535;
   packet_header.network           =       195;

   struct timeval tv;
   gettimeofday(&tv, NULL);

   packet_header.ts_sec            =       tv.tv_sec;
   packet_header.ts_usec           =       tv.tv_usec;
   packet_header.incl_len          =       11;
   packet_header.orig_len          =       13;

   packet_header.seqno = 255;
   packet_header.dpan = 65535;
   packet_header.daddr = 65535;
   packet_header.saddr = 65535;
   packet_header.payload_data = 8;

   int fd = open("sample.cap", O_CREAT | O_WRONLY);
   printf("Bytes written: %d \n",pwrite(fd, &packet_header, sizeof(packet_header),0));

   }

The struct has a char var “seq” and next to the seq no value a single byte of random value gets added in the file.

  • 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-27T03:10:31+00:00Added an answer on May 27, 2026 at 3:10 am

    Your structure contains a char data member and you probably have word-alignment on. See if you can find a “pack” option in the compiler.

    You can probably use a #pragma pack(1) or similar pragma (pragma’s are implementation specific) to change the alignment for a particular set of classes.

    Be careful though, the compiler word-aligns for performance reasons. The memory bus typically works on word boundaries, so you could end up requiring two fetchs for each word that straddles the word boundary thereby slowing memory access down a bit.

    You might want to stream the structure members out individually if you’re concerned about the added bytes in the file.

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

Sidebar

Related Questions

I'm having a weird issue with Visual Studio 2008. Every time I fire it
I'm having a weird issue with a function I'm trying to call from within
I'm having a very weird issue in IE6. If I set a div container
I'm having this weird issue and I'm hoping someone could clear this up for
I am having a weird issue with cookies in CodeIgniter. The problem is I
I'm having a weird issue that I can't track down... For context, I have
I'm having a bit of a weird issue here, basically, im using hibernate together
Having a weird issue. I'm new to Macs and have a windows VM that
I'm having a weird issue with my Silverlight 4 project, although it's one I've
I am having some weird issue here. I have a database table which has

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.