writing into file using structures

  • Thread starter sweeet_addiction16
  • Start date
S

sweeet_addiction16

hello experts...pls help me debug this code........im not able to
write in the data bytes ..only the few header bytes are written...

#include<stdio.h>

struct mthd_chunk
{

char id[4];
unsigned long Length; /* This will be 6 */

/* Here are the 6 bytes */
unsigned short Format;
unsigned short NumTrack;
unsigned short Division;
};

struct MTRK_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','r','k' */
unsigned long Length; /* This will be the actual size of
Data[] */

/* Here are the data bytes */
unsigned char Data[20]; /* Its actual size is Data[Length]
*/
};

int main()
{
FILE *fp;
struct mthd_chunk mthd;
struct MTRK_CHUNK mtrk;
mthd.id[0]=0x4d;
mthd.id[1]=0x54;
mthd.id[2]=0x68;
mthd.id[3]=0x64;
mthd.Length=0x00000006;
mthd.Format=0x0000;
mthd.NumTrack=0x0001;
mthd.Division=0x0080;

mtrk.ID[0]=0x4d;
mtrk.ID[1]=0x54;
mtrk.ID[2]=0x72;
mtrk.ID[3]=0x6b;
mtrk.Length=0x0000000d;

mtrk.Data[0]=0x40;
mtrk.Data[1]=0x90;
mtrk.Data[2]=0x3c;
mtrk.Data[3]=0x1e;
mtrk.Data[4]=0x82;
mtrk.Data[5]=0x00;
mtrk.Data[6]=0x90;structures in c
mtrk.Data[7]=0x3c;
mtrk.Data[8]=0x00;

mtrk.Data[9]=0x00;
mtrk.Data[10]=0xff;
mtrk.Data[11]=0x2f;
mtrk.Data[12]=0x00;


fp=fopen("star.mid","w");
fwrite(&mthd,sizeof mthd,1,fp);
fwrite(&mtrk,sizeof mtrk,1,fp);

}
 
M

Malcolm McLean

hello experts...pls help me debug this code........im not able to
write in the data bytes ..only the few header bytes are written...

#include<stdio.h>

struct mthd_chunk
{

char id[4];
unsigned long Length; /* This will be 6 */

/* Here are the 6 bytes */
unsigned short Format;
unsigned short NumTrack;
unsigned short Division;
};

struct MTRK_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','r','k' */
unsigned long Length; /* This will be the actual size of
Data[] */

/* Here are the data bytes */
unsigned char Data[20]; /* Its actual size is Data[Length]
*/
};

int main()
{
FILE *fp;
struct mthd_chunk mthd;
struct MTRK_CHUNK mtrk;
mthd.id[0]=0x4d;
mthd.id[1]=0x54;
mthd.id[2]=0x68;
mthd.id[3]=0x64;
mthd.Length=0x00000006;
mthd.Format=0x0000;
mthd.NumTrack=0x0001;
mthd.Division=0x0080;

mtrk.ID[0]=0x4d;
mtrk.ID[1]=0x54;
mtrk.ID[2]=0x72;
mtrk.ID[3]=0x6b;
mtrk.Length=0x0000000d;

mtrk.Data[0]=0x40;
mtrk.Data[1]=0x90;
mtrk.Data[2]=0x3c;
mtrk.Data[3]=0x1e;
mtrk.Data[4]=0x82;
mtrk.Data[5]=0x00;
mtrk.Data[6]=0x90;structures in c
mtrk.Data[7]=0x3c;
mtrk.Data[8]=0x00;

mtrk.Data[9]=0x00;
mtrk.Data[10]=0xff;
mtrk.Data[11]=0x2f;
mtrk.Data[12]=0x00;


fp=fopen("star.mid","w");
fwrite(&mthd,sizeof mthd,1,fp);
fwrite(&mtrk,sizeof mtrk,1,fp);

}
This is where the struct hack comes unstuck.
sizeof mtrk will be only 20 + 4 + a few for the long.

To write it do so member by member

fwrite(mtrk.ID, 1, 4, fp):
fwrite(&mtrk.Length, sizeof(unsigned long), 1, fp):
fwrite(mtrk.data, mtrk.Length, 1, fp);

Note that if you want the code to be portable you cannot write a raw
unsigned long, as it may be 32 or 64 bits on common systems, and even a
different number on some oddballs.
 
A

Abdo Haji-Ali

hello experts...pls help me debug this code........im not able to
write in the data bytes ..only the few header bytes are written...

[code snipped]
Have you actually tried this code? Apart from the missing fclose()
call at the end of main() everything works perfectly.
Is MTRK_CHUNK really defined like you metioned? Or is it define like
this?

struct MTRK_CHUNK
{
char ID[4];
unsigned long Length;
unsigned char *Data; /* Data is not an array but a pointer */
};

If that's the case then you can use Macolm's suggestion or create a
"variable-size structure".
http://www.ddj.com/cpp/184403480

Abdo Haji-Ali
Programmer
In|Framez
 
B

Barry Schwarz

hello experts...pls help me debug this code........im not able to
write in the data bytes ..only the few header bytes are written...

How do you know this? What technique are you using to examine the
output?
#include<stdio.h>

struct mthd_chunk
{

char id[4];
unsigned long Length; /* This will be 6 */

/* Here are the 6 bytes */
unsigned short Format;
unsigned short NumTrack;
unsigned short Division;
};

struct MTRK_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','r','k' */
unsigned long Length; /* This will be the actual size of
Data[] */

/* Here are the data bytes */
unsigned char Data[20]; /* Its actual size is Data[Length]
*/
};

int main()

int main(void) to be portable.
{
FILE *fp;
struct mthd_chunk mthd;
struct MTRK_CHUNK mtrk;
mthd.id[0]=0x4d;

These values are ASCII specific and don't work on my system. Is there
some reason you could not use 'M', 'T', 'r', etc.
mthd.id[1]=0x54;
mthd.id[2]=0x68;
mthd.id[3]=0x64;
mthd.Length=0x00000006;
mthd.Format=0x0000;
mthd.NumTrack=0x0001;
mthd.Division=0x0080;

mtrk.ID[0]=0x4d;
mtrk.ID[1]=0x54;
mtrk.ID[2]=0x72;
mtrk.ID[3]=0x6b;
mtrk.Length=0x0000000d;

mtrk.Data[0]=0x40;
mtrk.Data[1]=0x90;
mtrk.Data[2]=0x3c;
mtrk.Data[3]=0x1e;
mtrk.Data[4]=0x82;
mtrk.Data[5]=0x00;
mtrk.Data[6]=0x90;structures in c
mtrk.Data[7]=0x3c;
mtrk.Data[8]=0x00;

mtrk.Data[9]=0x00;
mtrk.Data[10]=0xff;
mtrk.Data[11]=0x2f;
mtrk.Data[12]=0x00;


fp=fopen("star.mid","w");

You should check to insure the file opened properly.
fwrite(&mthd,sizeof mthd,1,fp);

You should check to insure the write succeeded.
fwrite(&mtrk,sizeof mtrk,1,fp);

You never close the file. Data may be left in the buffer.


Remove del for email
 
B

Barry Schwarz

hello experts...pls help me debug this code........im not able to
write in the data bytes ..only the few header bytes are written...

#include<stdio.h>

struct mthd_chunk
{

char id[4];
unsigned long Length; /* This will be 6 */

/* Here are the 6 bytes */
unsigned short Format;
unsigned short NumTrack;
unsigned short Division;
};

struct MTRK_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','r','k' */
unsigned long Length; /* This will be the actual size of
Data[] */

/* Here are the data bytes */
unsigned char Data[20]; /* Its actual size is Data[Length]
*/
};

int main()
{
FILE *fp;
struct mthd_chunk mthd;
struct MTRK_CHUNK mtrk;
mthd.id[0]=0x4d;
mthd.id[1]=0x54;
mthd.id[2]=0x68;
mthd.id[3]=0x64;
mthd.Length=0x00000006;
mthd.Format=0x0000;
mthd.NumTrack=0x0001;
mthd.Division=0x0080;

mtrk.ID[0]=0x4d;
mtrk.ID[1]=0x54;
mtrk.ID[2]=0x72;
mtrk.ID[3]=0x6b;
mtrk.Length=0x0000000d;

mtrk.Data[0]=0x40;
mtrk.Data[1]=0x90;
mtrk.Data[2]=0x3c;
mtrk.Data[3]=0x1e;
mtrk.Data[4]=0x82;
mtrk.Data[5]=0x00;
mtrk.Data[6]=0x90;structures in c
mtrk.Data[7]=0x3c;
mtrk.Data[8]=0x00;

mtrk.Data[9]=0x00;
mtrk.Data[10]=0xff;
mtrk.Data[11]=0x2f;
mtrk.Data[12]=0x00;


fp=fopen("star.mid","w");
fwrite(&mthd,sizeof mthd,1,fp);
fwrite(&mtrk,sizeof mtrk,1,fp);

}
This is where the struct hack comes unstuck.

What struct hack? He has not placed a short array at the end of a
structure and over-allocated space for it.
sizeof mtrk will be only 20 + 4 + a few for the long.

Plus whatever is needed for alignment and arbitrary padding. Does he
want to write any more?
To write it do so member by member

fwrite(mtrk.ID, 1, 4, fp):
fwrite(&mtrk.Length, sizeof(unsigned long), 1, fp):
fwrite(mtrk.data, mtrk.Length, 1, fp);

Note that if you want the code to be portable you cannot write a raw
unsigned long, as it may be 32 or 64 bits on common systems, and even a
different number on some oddballs.


Remove del for email
 
D

David Thompson

struct mthd_chunk
{
char id[4];
unsigned long Length; /* This will be 6 */

/* Here are the 6 bytes */
unsigned short Format;
unsigned short NumTrack;
unsigned short Division;
};

struct MTRK_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','r','k' */
unsigned long Length; /* This will be the actual size of
Data[] */

/* Here are the data bytes */
unsigned char Data[20]; /* Its actual size is Data[Length]
*/
};

int main()
{
FILE *fp;
struct mthd_chunk mthd;
struct MTRK_CHUNK mtrk;
mtrk.Length=0x0000000d;
fp=fopen("star.mid","w");

Should check for failure. But that isn't your problem this time, since
you say you did get partially correct output.
fwrite(&mthd,sizeof mthd,1,fp);

This should work. But you should check the return value just in case.
fwrite(&mtrk,sizeof mtrk,1,fp);

First, this will write the entire struct, including all 20 bytes of
Data. If you want only the amount defined by the Length field, use
fwrite (&mtrk, 1, offsetof(struct MTRK_CHUNK, Data) + mtrk.Length, fp)
(or swap the two middle arguments if you like). If you do want all of
it, and just use Length to identify the valid part, you might want to
pad out the remaining bytes, to make it easier to look at, especially
for debugging.

Second, one of the bytes in .Length is 0x0D. This character has been
used on some systems, although not many, as the C newline character
\n; if your system is such, this byte may get translated into
something different on text output, and back on input. On systems
where it is not used so, it is not among the characters that the
standard guarantees can be written to and read back from a text file.
To deal with either of these, open the file in binary mode "wb".
(Although on some systems, notably POSIX/Unix, this won't matter.)
- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top