writing binary/struct data to file


 
Thread Tools Search this Thread
Top Forums Programming writing binary/struct data to file
# 1  
Old 05-22-2009
writing binary/struct data to file

I am trying to write binary data to a file. My program below:

Code:
#include <stdlib.h>
#include <stdio.h>

struct tinner {
  int j;
  int k[3];
};

struct touter {
  int i;
  struct tinner *inner;
};

int main() {
        struct touter data;
        data.i = 10;

        struct tinner in;
        in.j = 5;
        in.k[0] = 1, in.k[1] = 2, in.k[2] = 3;
        data.inner = &in;

        printf("sizeof(data)=%d, sizeof(in)=%d\n", sizeof(data), sizeof(in));

        FILE *fp = fopen("test.dat", "w+b");
        fwrite((const void *) &data, sizeof(data), 1, fp);
        fflush(fp);
        fclose(fp);
}

The output that I get is:
sizeof(data)=8, sizeof(in)=16

Why is it so ? Why is the full structure that is hold by the variable 'data' not written to the file ? What can I do so that full data is written that is when I write instances of touter, touter.tinner is also written ?

-Satish

Last edited by Franklin52; 05-22-2009 at 10:36 AM.. Reason: Replace font tag with code tags
# 2  
Old 05-22-2009
You are not writing tinner to the file - just a pointer which means nothing when you try to read the file try:
Code:
#include <stdlib.h>
#include <stdio.h>

struct tinner {
  int j;
  int k[3];
};

struct touter {
  int i;
  struct tinner inner;
};

int main() {
        struct touter data;
        data.i = 10;

       
        data.inner.j = 5;
        data.inner.k[0] = 1, data.inner.k[1] = 2, data.inner.k[2] = 3;
        

        printf("sizeof(data)=%d\n", sizeof(data) );

        FILE *fp = fopen("test.dat", "w+b");
        fwrite((const void *) &data, sizeof(data), 1, fp);
        fflush(fp);
        fclose(fp);
        return 0;
}

# 3  
Old 05-25-2009
Code:
The output that I get is:
sizeof(data)=8, sizeof(in)=16
Why is it so ?

int i;:::: 4 bytes for integer
struct tinner *inner;::::::: 4 bytes for pointer.
# 4  
Old 05-25-2009
How can I write the full structure (obviously I am not interested in writing the pointer but the actual record) ?
# 5  
Old 05-25-2009
Code:
(obviously I am not interested in writing the pointer but the actual record)

Copy the actual record into some buffer using sprintf, then write it to the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script writing for tables with binary data

Hi There, I'm trying to write a simple script that will email me when we have an application job in a certain status that needs human intervention. I've used this script for other tables and it works great. However, this one gives me the warning that there is binary data so it might not. ... (2 Replies)
Discussion started by: Colel2
2 Replies

2. Programming

Writing a file in Binary

Hello All, I need to read values from database and then need to write to a file. Planning to do that as binary since no one can modify the values. We tried to write the file in "B" mode and the file is created but when I try to open in notepad I am able to do. I there a way that someone should... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

3. Windows & DOS: Issues & Discussions

Xterm logging on Cygwin/X - binary data in log file.

I have Cygwin/X installed on Windows 7. In an xterm, I turned on logging via Main Options > Log to File. When I open my log file with Vim I get a warning that it might be binary. Looking through the file I see what I think are VT datastream escape characters. It makes it hard to use the... (1 Reply)
Discussion started by: gctaylor
1 Replies

4. Shell Programming and Scripting

Writing input data in file

I am having an input file for eg. file.txt which contains pipe delimited rows file.txt: xx|yyy|zz|12|3|aaaaa|..... yy|zz|1|3|4|xxxxx|....... . . . i want the above file name to be transformed into another file with giving input from the user and replacing the corresponding fields based... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

5. Shell Programming and Scripting

Removing inline binary data from txt file

I am trying to parse a file but the filehas binary data inline mixed with text fields. I tried the binutils strings function , it get the binary data out but put the char following the binary data in a new line . input file app_id:1936 pgm_num:0 branch:TBNY ord_num:0500012(–QMK) deal_num:0... (12 Replies)
Discussion started by: tasmac
12 Replies

6. 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

7. Programming

writing binary file (C++)

Hi guys, I am writing a vector into a binary file (linux of course), but somehow there seems to be something wrong with this code, because the output file size in binary is BIGGER than doing it with the same vector but insead of binary, in plain text, and I as far as I understand, binary files size... (8 Replies)
Discussion started by: lamachejo
8 Replies

8. Shell Programming and Scripting

AWK Help- Writing Data into a File

Hi All, I need to maintain a AUDIT file in system for every incoming and outgoing file. For this i am trying to append a record by using the AWK every time the file arriving. I have used the code as below. AWK '{print "FILENAME IS", $1,"DATE IS", $2}' >> AUDITFILE.txt $1 and $2 are global... (1 Reply)
Discussion started by: Raamc
1 Replies

9. Shell Programming and Scripting

how to check the file data type(ascii or binary)

hi i am receiving a file from one system , i have to verify the format of the file data i.e whether the data is in acii format or binary format, please help thanks in advance satya (1 Reply)
Discussion started by: Satyak
1 Replies

10. UNIX for Dummies Questions & Answers

Binary data to text file conversion

Dear Sir; i want to know how the binary data convert to text file or readablw format (ASCII).If possible pl. help me for the software and where it is available for download. i.e. (1 Reply)
Discussion started by: auro123
1 Replies
Login or Register to Ask a Question