save a struct


 
Thread Tools Search this Thread
Top Forums Programming save a struct
# 1  
Old 10-29-2004
save a struct

hi all ,
can i save a structure in c in a file? how ?

help me , thx. Smilie
# 2  
Old 10-29-2004
Yeah, you can.

Say you have a struct variable named s. You could simply write/read from ((void*)&s) to (((void*)&s) + sizeof s) using whatever I/O functions. But there are 2 problems with that.

1) Some compilers will produce code that use "sparse" structs. structs with holes in them, that is. This is to make the code faster. Memory accesses that is aligned at some power of 2 multiple is usually faster, for example. If the compiler or compiler settings were to change, the sparseness may change too and old data files would become incompatible.

2) This method will create data files that are architecture-dependent. Fundamental C types, like pointers, ints, floats, etc, won't read/write the same on different architecture. So the data files will not be portable.

So it would be better to encode your data in a standard way. There are a few ways to do that:

1) Just read/write your stuff using ASCII printable chars. You could use scanf(3)/printf(3) for that.

2) Check xdr(3) (eXternal Data Representation); that's what is used for rpc(3) (Remote Procedure Call). It can encode/decode your stuff automatically.

3) Use some library that uses XML format. That's the current hype.
# 3  
Old 10-29-2004
By save, do you mean write the data in it to a file?
Try something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    struct {
        char a[20];
        int b;
     } mystruct;
    FILE *out;
    char *somestring="A test value";
    memset(mystruct.a,0x0,sizeof(mystruct.a));
    strcpy(mystruct.a,somestring);
    mystruct.b = 8;
    out=fopen("myfile","wb");
    if(out==NULL)
    {
        perror("");
        exit(EXIT_FAILURE);
    }
    fwrite(&mystruct,sizeof(mystruct),1,out);
    if(fclose(out)!=0)
    {
       perror("");
       exit(EXIT_FAILURE);
    }

    return 0;
}

To see what is in the file try
Code:
od myfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

2. Programming

Get struct definition

I have many headers with huge amount of structures in them, typical one looks like this: $ cat a.h struct Rec1 { int f1; int f2; }; struct Rec2 { char r1; char r2; }; struct Rec3 { int f1; float k1; float ... (6 Replies)
Discussion started by: migurus
6 Replies

3. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

4. UNIX for Dummies Questions & Answers

struct winsize

what is struct winsize used for? i tried looking it up, but no luck. (0 Replies)
Discussion started by: l flipboi l
0 Replies

5. Programming

help with struct command in C

in C i am using this code to get the c time or a time or m time struct dirent *dir; struct stat my; stat(what, &my); thetime = my.st_ctime; How can i check if i have permission to check the c time of the file? (1 Reply)
Discussion started by: omega666
1 Replies

6. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

7. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

8. Linux

GCOV : struct bb

Hi, I am working on gcov.Meaning, analysing the functionality of gcov. There is one structure called "struct bb". I am not sure, how struct bb members are getting assigned values. If anyone knows how it is happening pls let me know. Thanks in advance. --Vichu (0 Replies)
Discussion started by: Vichu
0 Replies

9. Programming

Struct Array

in my .c file i have a struct atop of the program defined as follows: #define MAX 10 int curtab; static struct tab { int count; int use; } tab; with the initial function following it like so: int tab_create(int init_count) { int i; for(i=0; i < MAX; i++) {... (1 Reply)
Discussion started by: micmac700
1 Replies

10. Programming

struct tm problem

I receive an integer as argument for a function. within function definition i want it to be of type struct tm. eg.. main() { int a; ...... } function(...,..,a,..) int a; { struct tm tm; if(!a) ^ time(&a); ^ ... (4 Replies)
Discussion started by: bankpro
4 Replies
Login or Register to Ask a Question