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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:07:02+00:00 2026-06-13T12:07:02+00:00

I need to make the data in the structure as persistent i.e wanted to

  • 0

I need to make the data in the structure as persistent i.e wanted to store it in a file and need to read that character by character…For this i had written the below code…the below code is not working it is unable to write the structure into the file(character by character)…I needed that character by character

struct x *x1=(struct x*)malloc(sizeof(struct x));
x1->y=29;
x1->c='A';
char *x2=(char *)malloc(sizeof(struct x));
char *s=(char *)malloc(sizeof(struct x));
for(i=0;i<sizeof(struct x);i++)
{
    *(x2+i)=*((char *)x1+i);
}
fd=open("rohit",O_RDWR); 
num1=write(fd,x2,sizeof(struct x));
num2=read(fd,s,sizeof(struct x));
for(i=0;i<sizeof(struct x);i++)
     printf(" %d ",*(s+i));

I can use fread & fwrite…but i wanted to do that character by character…so i am using read & write(they are direct system calls rite)…i am unable to write into it my write function is showing error i.e it is returning -1…Is there anything wrong in the above code…

  • 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-06-13T12:07:03+00:00Added an answer on June 13, 2026 at 12:07 pm

    Here are two functions you can use if you want:

    int store(char * filename, void * ptr, size_t size)
    {
      int fd, n;
    
      fd = open(filename, O_CREAT | O_WRONLY, 0644);
      if (fd < 0)
        return -1;
    
      n = write(fd, (unsigned char *)ptr, size);
      if (n != size)
        return -1;
    
      close(fd);
      return 0;
    }
    
    int restore(char * filename, void * ptr, size_t size)
    {
      int fd, n;
    
      fd = open(filename, O_RDONLY, 0644);
      if (fd < 0)
        return -1;
    
      n = read(fd, (unsigned char *)ptr, size);
      if (n != size)
        return -1;
    
      close(fd);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make this data variable global: $.ajax({ url: get_data.php, cache: false, dataType:
I need to make a ListAdapter that presents data from multiple ContentProviders. The ContentProviders
I need to make a file format for my software. The data is basic
Following is the sample data. I need to make 3 copies of this data
So I have a subclassed UITableView that lists data. I need to make only
I need a data structure that Must be ordered (adding elements a, b and
Now that pandas provides a data frame structure, is there any need for structured/record
How do I make a tree data structure in C++ that uses iterators instead
I need make some action (dump statistical data) before the Dart program ends. The
I'm wondering what is the best way to make data thread-safe. Specifically, I need

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.