Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Files From Structures-PROBLEM!!

Status
Not open for further replies.

sajiv77

Programmer
Jan 2, 2002
14
0
0
IN
Hi
here is what iam trying to do.

->Create a File using structures

And Iam doing that by
1)Populating the structres
2)Putting the structres in a file

But the problem for me is that iam having a stucture which is unsigned char and i have to put double digit in it.
Iam enclosing a sample program I tried to dowith the header file


Header.h
typedef unsigned char byte;

typedef struct
{
byte nTest[4];
byte fTest;
byte cTest;
}TEST_T;

include <iostream.h>
#include <stdio.h>
#include &quot;St.h&quot;
#include <string.h>

class CF
{
public:
void Write1(char *,int);
void PutIn();
};

main()
{

CF c;
c.PutIn();
}

void CF::putIn()
{

// TEST_T TEST;
TEST_T * TEST = new TEST_T;
TEST->nTest[0]='1';
TEST->nTest[2]='2';
TEST->fTest=23;

TEST->cTest = 20;
Write1((char *)TEST,sizeof(TEST_T));

}
void CF::Write1(char *towrite,int nSize)
{

FILE *pFile;

if((pFile = fopen(&quot;Testotest&quot;,&quot;w&quot;)) == NULL)
cout<<&quot;Open failed&quot;<<endl;

if ((fwrite(towrite,nSize,1,pFile)) < 0)
cout<<&quot;Fwrite Failed&quot;<<endl;

fclose(pFile);


FILE *pFile1;
if((pFile1 = fopen(&quot;Testotest&quot;,&quot;r&quot;)) == NULL)
cout<<&quot;Open failed&quot;<<endl;

TEST_T TEST2;
size_t nSize1 = fread(&TEST2,sizeof(TEST2),1,pFile1);
cout<<&quot;SIZE = &quot;<<nSize1<<endl;
cout<<TEST2.nTest[0]<<endl;
cout<<TEST2.fTest<<endl;
cout<<&quot;Ftest&quot;<<TEST2.fTest<<endl;

fclose(pFile);
}

I am able to compile this program and also run it.The problem is that Iam not able to get the desired output in the file
Here are the list of problems
1)The assignment in PutIn( ) function fails as Iam assigning double digit to unsigned char.
2)The Read also fails in the Write1() function

Can somebody help me in solving this error.



 
You could use a &quot;union&quot; to make the same memory accessible to two different variable types, if they are the same memory size. Do something like this:
[ignore]
union PacificHaHa
{
unsigned char cMyChar;
double dMyDouble;
} MyUnion;

double dDoubleValue = 123.456;
char cCharValue = 'Y';


MyUnion.dMyDouble = dDoubleValue;
printf(MyUnion.dMyDouble);

MyUnion.cMyChar = cCharValue;
printf(MyUnion.cMyChar);
[/ignore]
 
The problem is that I have to use the only
typedef unsigned char       byte;
typedef struct
        {
                byte  nTest[4];
                byte fTest;
                byte cTest;
}TEST_T;

These strucrtures are given by the client for the project.SO i cannot change it.At the same time i have to populate these structures and create a file.

ANy more Ideas
 
When you talk of a double digit, do you mean two
digits or a digit of type double ?

You set fTest to 23 and cTest to 20. These represent
ASCII control characters; is this what you meant ?

Do you want to set nTest to a numeric value ? If so,
try sprintf. eg.

sprintf (TEST_T.nTest, &quot;%2.2d&quot;, 34);

This places the character 3 in nTest[0], 4 in in nTest[1]
and sets nTest[2] and nTest[3] to (binary) zero.

Of course, I may have totally misunderstood what you want ...
 
well, there is a way but it is a bit off the way of normally doing things

Code:
typedef unsigned char       byte;

typedef struct
        {
                byte  nTest[4];
                byte fTest;
                byte cTest;
}TEST_T;

...

TEST_T myT;

HERE IS WHERE IT CHANGES. We know that a structure is just a series of bits in a specific order and by looking at the struct we can see that if we got a pointer to myT it would point to the first byte of nTest

EXAMPLE:
Code:
void* ptr = (void*)(&myT);

What you can do from here are a few things but what I would suggest to make things clearer is to do the following.

1. Define a new struct to represent how you want to save data.

2. Use this just as a pointer type

EXAMPLE:
Code:
typedef struct{
   double* nTest;
   byte* fTest;
   byte* cTest;
}CONVERTER;

now in the code we can do the following

// myT already declared

CONVERTER myConverter;

myConverter.nTest = (double*)myT.nTest;
myConverter.fTest = &myT.fTest;
myConverter.cTest = &myT.cTest;

// any changes done to myConverter will affect myT

// THIS WAS ONE WAY

another way would be to just do the following

double* dptr = (double*)&myT;
*dptr = double_value;

// set fTest and cTest as normal

I hope these helped.

Matt


 
First of all by double digit I meant assigning a two digit to unsigned char.

For example
The assignment-
TEST.fTest=11;
TEST.cTest =21;

This complies but the output from the file read is empty.

(or)

TEST.fTest='11';
TEST.cTest ='21';
This is the warning I am getting.
Structures.cpp: In method `void CF::putIn()':
Structures.cpp:32: warning: multi-character character constant
Structures.cpp:32: warning: large integer implicitly truncated to unsigned type
Structures.cpp:33: warning: multi-character character constant
Structures.cpp:33: warning: large integer implicitly truncated to unsigned type

The above both are invalid as iam assigning two digit to an a unsigned char.
-----where ftest and ctest is declared as

typedef unsigned char byte;
typedef struct
{
byte nTest[2];
byte fTest;
byte cTest;
}TEST_T;

So,now my problem is still the same.
Iam creating a file dumping these structures in the file and then reading the same file.But I dont get the desired output.

Please try executing my program.Ultimately when i want to read the file i have written i must read the same values that i gave while writing.

---------------------------------

Matt ---> I tried your tips .I have even put the coding i tried here.But still the same problem.I hope this is what u meant.
typedef unsigned char byte;

typedef struct
{
byte nTest[2];
byte fTest;
byte cTest;
}TEST_T;

typedef struct{
double* nTest;
byte* fTest;
byte* cTest;
}CONVERTER;



#include <iostream.h>
#include <stdio.h>
#include &quot;St.h&quot;
#include <string.h>
class CF
{
public:
void Write1(char *,int);
void PutIn();
};
main()
{
CF c;
c.PutIn();
}

void CF::putIn()
{
TEST_T TEST;
TEST.nTest[0]='7';
// TEST.nTest[1]='9';

// double* dTemp=(double*)&TEST;
// *dTemp=12;

TEST.fTest=11;
TEST.cTest =21;

CONVERTER myConverter;
myConverter.nTest = (double*)TEST.nTest;
myConverter.fTest = &TEST.fTest;
myConverter.cTest = &TEST.cTest;
Write1((char *)&TEST,sizeof(TEST_T));
}

void CF::Write1(char *towrite,int nSize)
{

FILE *pFile;
if((pFile = fopen(&quot;Test.txt&quot;,&quot;w&quot;)) == NULL)
cout<<&quot;Open failed&quot;<<endl;
if ((fwrite(towrite,nSize,1,pFile)) < 0)
cout<<&quot;Fwrite Failed&quot;<<endl;

fclose(pFile);

FILE *pFile1;
if((pFile1 = fopen(&quot;Test.txt&quot;,&quot;r&quot;)) == NULL)
cout<<&quot;Open failed&quot;<<endl;

TEST_T TEST2;
size_t nSize1 = fread(&TEST2,sizeof(TEST2),1,pFile1);

cout<<&quot;SIZE = &quot;<<nSize1<<endl;
cout<<&quot;nTest[0]--->&quot;<<TEST2.nTest[0]<<endl;
// cout<<&quot;nTest[1]--->&quot;<<TEST2.nTest[1]<<endl;
cout<<&quot;fTest--->&quot;<<TEST2.fTest<<endl;
cout<<&quot;ctest--->&quot;<<TEST2.cTest<<endl;

fclose(pFile);
}

Hope to get more help on this

Thanks


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top