write function

M

muser

I want to use the write function to write a long varible to file, e.g.

struct crecord{
long customer code[5];
}
crecord Newcrecord;

validdata.write((char*) record, Newcrecord.code))

I get compiler errors saying write cannot convert long[5] into int.
How can the variable be written to write?

I'm using the write function because the file I'm writing the data to
is a binary file.
 
J

Joe Estock

muser said:
I want to use the write function to write a long varible to file, e.g.

struct crecord{
long customer code[5];
}
crecord Newcrecord;

validdata.write((char*) record, Newcrecord.code))

I get compiler errors saying write cannot convert long[5] into int.
How can the variable be written to write?

I'm using the write function because the file I'm writing the data to
is a binary file.

struct crecord {
long customer_code[5];
}

crecord myrecord;
myrecord.customer_code[0] = 100754;
myrecord.customer_code[1] = 200012;
....

FILE *fp;
fp = fopen("mybinaryfile.dat", "w");
fwrite(&myrecord, sizeof(myrecord), fp);
fclose(fp);
 
R

Rick Noelle

Hi Joe and muser,

I may be wrong but I believe the fwrite line in your reply Joe should be:

fwrite(&myrecord, sizeof(long) , sizeof(myrecord), fp);

instead of:

fwrite(&myrecord, sizeof(myrecord), fp);

Best Regards,
Rick


muser said:
I want to use the write function to write a long varible to file, e.g.

struct crecord{
long customer code[5];
}
crecord Newcrecord;

validdata.write((char*) record, Newcrecord.code))

I get compiler errors saying write cannot convert long[5] into int.
How can the variable be written to write?

I'm using the write function because the file I'm writing the data to
is a binary file.

struct crecord {
long customer_code[5];
}

crecord myrecord;
myrecord.customer_code[0] = 100754;
myrecord.customer_code[1] = 200012;
...

FILE *fp;
fp = fopen("mybinaryfile.dat", "w");
fwrite(&myrecord, sizeof(myrecord), fp);
fclose(fp);
 
T

tom_usenet

I want to use the write function to write a long varible to file, e.g.

struct crecord{
long customer code[5];
}
crecord Newcrecord;

validdata.write((char*) record, Newcrecord.code))

Assuming validdata is an ostream opened with ios::binary, you want

validdata.write(
reinterpret_cast<char*>(Newcrecord),
sizeof Newcrecord
);
I get compiler errors saying write cannot convert long[5] into int.
How can the variable be written to write?

By realising that the two parameters are the address of the start of
the object (cast to char) and the size of the object.

Tom
 
T

Thomas Matthews

Rick said:
Hi Joe and muser,

I may be wrong but I believe the fwrite line in your reply Joe should be:

fwrite(&myrecord, sizeof(long) , sizeof(myrecord), fp);

instead of:

fwrite(&myrecord, sizeof(myrecord), fp);

Best Regards,
Rick

1. Don't top-post. Replies are appended to the bottom of the reply
{like this one) or interspersed.
2. The format of the fwrite function:
#include <stdio.h>
size_t fwrite(void * pointer, /* pointer to data */
size_t element_size,
size_t count,
FILE * stream);
As taken from Section 15.13 in Harbison & Steele.

So according to your correction, you want to output
"sizeof(myrecord)" amount of "sizeof(long)" {element size}.
Which is confusing at best.

Generally, the fwrite call is:
unsigned int num_records_written;

num_records_written = fwrite(&data, sizeof(myrecord),
quantity, fp);

Or if you want:
size_t bytes_written =
fwrite(&data, 1, sizeof(myrecord), fp);

The former is used to write many chunks, while the
latter writes many one-byte quantities.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Rick Noelle

1. Don't top-post. Replies are appended to the bottom of the reply
{like this one) or interspersed.

Sorry, my bad.
2. The format of the fwrite function:
#include <stdio.h>
size_t fwrite(void * pointer, /* pointer to data */
size_t element_size,
size_t count,
FILE * stream);
As taken from Section 15.13 in Harbison & Steele.

So according to your correction, you want to output
"sizeof(myrecord)" amount of "sizeof(long)" {element size}.
Which is confusing at best.

Sorry again, I had meant to indicate the array element count with the
second sizeof parameter and I indicated it incorrectly. Assuming the user
has a constant for the number of elements he could simply use that but
since I did not know the constant value I tried to use sizeof to derive
it. I should have written this:

fwrite(&myrecord, sizeof(long), (sizeof(myrecord)/sizeof(long)), fp);

or alternatively

fwrite(&myrecord, sizeof(long), (sizeof(myrecord)/sizeof myrecord.customer_code[0]), fp);

The way I understand the function, the first size indicates the size, in
bytes, of the chunks to be written, and the second size represents the
number of chunks to be written (the number of elements in the array).
sizeof(myrecord) will return the total bytes in the array and sizeof(long)
will return the number of bytes in a single element. By dividing the two,
we can derive the number of elements in the array.
Or if you want:
size_t bytes_written =
fwrite(&data, 1, sizeof(myrecord), fp);

The former is used to write many chunks, while the
latter writes many one-byte quantities.

This was the format I was after but I don't want to write one byte chunks,
I want to write chunks that are the same size as the individual
array elements. A long is one bytes (at least on my Linux box) so the one
would not work. If there were 5 elements in the array and each element
was one bytes long, by using a one you would end up with 20 chunks instead
of five.

Best Regards,
Rick
 
R

Rick Noelle

1. Don't top-post. Replies are appended to the bottom of the reply
{like this one) or interspersed.

Sorry, my bad.
2. The format of the fwrite function:
#include <stdio.h>
size_t fwrite(void * pointer, /* pointer to data */
size_t element_size,
size_t count,
FILE * stream);
As taken from Section 15.13 in Harbison & Steele.

So according to your correction, you want to output
"sizeof(myrecord)" amount of "sizeof(long)" {element size}.
Which is confusing at best.

Sorry again, I had meant to indicate the array element count with the
second sizeof parameter and I indicated it incorrectly. Assuming the user
has a constant for the number of elements he could simply use that but
since I did not know the constant value I tried to use sizeof to derive
it. I should have written this:

fwrite(&myrecord, sizeof(long), (sizeof(myrecord)/sizeof(long)), fp);

or alternatively

fwrite(&myrecord, sizeof(long), (sizeof(myrecord)/sizeof myrecord.customer_code[0]), fp);

The way I understand the function, the first size indicates the size, in
bytes, of the chunks to be written, and the second size represents the
number of chunks to be written (the number of elements in the array).
sizeof(myrecord) will return the total bytes in the array and sizeof(long)
will return the number of bytes in a single element. By dividing the two,
we can derive the number of elements in the array.
Or if you want:
size_t bytes_written =
fwrite(&data, 1, sizeof(myrecord), fp);

The former is used to write many chunks, while the
latter writes many one-byte quantities.

This was the format I was after but I don't want to write one byte chunks,
I want to write chunks that are the same size as the individual
array elements. A long is one bytes (at least on my Linux box) so the one

Sorry again! I meant to say a long is 4 bytes long on my Linux box, not
one byte. Doh!
 
M

muser

Rick Noelle said:
Rick Noelle wrote:
Hi Joe and muser,

I may be wrong but I believe the fwrite line in your reply Joe should be:

fwrite(&myrecord, sizeof(long) , sizeof(myrecord), fp);

instead of:

fwrite(&myrecord, sizeof(myrecord), fp);

Best Regards,
Rick

1. Don't top-post. Replies are appended to the bottom of the reply
{like this one) or interspersed.

Sorry, my bad.
2. The format of the fwrite function:
#include <stdio.h>
size_t fwrite(void * pointer, /* pointer to data */
size_t element_size,
size_t count,
FILE * stream);
As taken from Section 15.13 in Harbison & Steele.

So according to your correction, you want to output
"sizeof(myrecord)" amount of "sizeof(long)" {element size}.
Which is confusing at best.

Sorry again, I had meant to indicate the array element count with the
second sizeof parameter and I indicated it incorrectly. Assuming the user
has a constant for the number of elements he could simply use that but
since I did not know the constant value I tried to use sizeof to derive
it. I should have written this:

fwrite(&myrecord, sizeof(long), (sizeof(myrecord)/sizeof(long)), fp);

or alternatively

fwrite(&myrecord, sizeof(long), (sizeof(myrecord)/sizeof myrecord.customer_code[0]), fp);

The way I understand the function, the first size indicates the size, in
bytes, of the chunks to be written, and the second size represents the
number of chunks to be written (the number of elements in the array).
sizeof(myrecord) will return the total bytes in the array and sizeof(long)
will return the number of bytes in a single element. By dividing the two,
we can derive the number of elements in the array.
Or if you want:
size_t bytes_written =
fwrite(&data, 1, sizeof(myrecord), fp);

The former is used to write many chunks, while the
latter writes many one-byte quantities.

This was the format I was after but I don't want to write one byte chunks,
I want to write chunks that are the same size as the individual
array elements. A long is one bytes (at least on my Linux box) so the one

Sorry again! I meant to say a long is 4 bytes long on my Linux box, not
one byte. Doh!
would not work. If there were 5 elements in the array and each element
was one bytes long, by using a one you would end up with 20 chunks instead
of five.

Best Regards,
Rick

This chunk of code doesn't do what it is suppose to do.

if(!checkdigitforcustomercode( Newcrecord.customercode )){
prnfile<< "Invalid: Incorrect check digit for c record\n";
prnfile<< record << endl;
return false;
}
else
{
validdata.write((char*) record, Newcrecord.customercode[5]);
}
the function works (checkdigitforcustomercode), but the
validdata.write part doesn't write to the intended file. Can anyone
tell me why they think that is?
record is a character array. And Newcrecord.customercode[5] is a
structure member. There is no compiling error and the file is create
but nothing is written to it when the program is compiled.

Thank you in advance for your help.
 
K

Karl Heinz Buchegger

muser said:
This chunk of code doesn't do what it is suppose to do.

if(!checkdigitforcustomercode( Newcrecord.customercode )){
prnfile<< "Invalid: Incorrect check digit for c record\n";
prnfile<< record << endl;
return false;
}
else
{
validdata.write((char*) record, Newcrecord.customercode[5]);
}
the function works (checkdigitforcustomercode), but the
validdata.write part doesn't write to the intended file. Can anyone
tell me why they think that is?

For one you posted a littel bit to less information.
But remembering a little bit on what you posted through the last
weeks
record is a character array. And Newcrecord.customercode[5] is a
structure member.

When will you learn it eventually.
If you have an array

char customercode[5];

Then valid array indices in this array are:

customercode[0]
customercode[1]
customercode[2]
customercode[3]
customercode[4]

count them! There are 5 of them! The highest valid index into
an array is always 1 less then the number you specified when
defining that array.

One should think that you have learned that lesson after posting
that same mistake now for weeks and beeing corrected for it, again
for weeks.

The write itself looks suspicious:
validdata.write((char*) record, Newcrecord.customercode[5]);

write to validdata, start at the starting address of the variable 'record'
and write Newrecord.customercode[5] number of bytes to it.
That doesn't look right. What has a customers code to do with how many
bytes of record are written to a file?

Why don't you hire a programmer writing this program for you?
He could have finished it since months.
 
M

muser

Karl Heinz Buchegger said:
muser said:
This chunk of code doesn't do what it is suppose to do.

if(!checkdigitforcustomercode( Newcrecord.customercode )){
prnfile<< "Invalid: Incorrect check digit for c record\n";
prnfile<< record << endl;
return false;
}
else
{
validdata.write((char*) record, Newcrecord.customercode[5]);
}
the function works (checkdigitforcustomercode), but the
validdata.write part doesn't write to the intended file. Can anyone
tell me why they think that is?

For one you posted a littel bit to less information.
But remembering a little bit on what you posted through the last
weeks
record is a character array. And Newcrecord.customercode[5] is a
structure member.

When will you learn it eventually.
If you have an array

char customercode[5];

Then valid array indices in this array are:

customercode[0]
customercode[1]
customercode[2]
customercode[3]
customercode[4]

count them! There are 5 of them! The highest valid index into
an array is always 1 less then the number you specified when
defining that array.

One should think that you have learned that lesson after posting
that same mistake now for weeks and beeing corrected for it, again
for weeks.

The write itself looks suspicious:
validdata.write((char*) record, Newcrecord.customercode[5]);

write to validdata, start at the starting address of the variable 'record'
and write Newrecord.customercode[5] number of bytes to it.
That doesn't look right. What has a customers code to do with how many
bytes of record are written to a file?

Why don't you hire a programmer writing this program for you?
He could have finished it since months.

Hi Karl, please don't be too disparaging in your reply to me, I'm
grateful for your help, and i do understand array indices. I wrote in
another post that when I did Newcrecord.customercode[6] = '\0';
it was actually reading a character it wasn't suppose to, therefore
leaving me no option but to try Newcrecord.customercode[5] = '\0';
This previous lead to access violation errors, which I posted for help
on, but now the compilers seems happy with the code. I'm (like so many
newbies before me) beginning to hate working with arrays, if i had a
better knowledge of vectors I would use them, but i don't and my
course doesn't specify its use either so I'm lumbered with arrays for
the time being.
 
M

muser

Karl Heinz Buchegger said:
muser said:
This chunk of code doesn't do what it is suppose to do.

if(!checkdigitforcustomercode( Newcrecord.customercode )){
prnfile<< "Invalid: Incorrect check digit for c record\n";
prnfile<< record << endl;
return false;
}
else
{
validdata.write((char*) record, Newcrecord.customercode[5]);
}
the function works (checkdigitforcustomercode), but the
validdata.write part doesn't write to the intended file. Can anyone
tell me why they think that is?

For one you posted a littel bit to less information.
But remembering a little bit on what you posted through the last
weeks
record is a character array. And Newcrecord.customercode[5] is a
structure member.

When will you learn it eventually.
If you have an array

char customercode[5];

Then valid array indices in this array are:

customercode[0]
customercode[1]
customercode[2]
customercode[3]
customercode[4]

count them! There are 5 of them! The highest valid index into
an array is always 1 less then the number you specified when
defining that array.

One should think that you have learned that lesson after posting
that same mistake now for weeks and beeing corrected for it, again
for weeks.

The write itself looks suspicious:
validdata.write((char*) record, Newcrecord.customercode[5]);

write to validdata, start at the starting address of the variable 'record'
and write Newrecord.customercode[5] number of bytes to it.
That doesn't look right. What has a customers code to do with how many
bytes of record are written to a file?

Why don't you hire a programmer writing this program for you?
He could have finished it since months.

sorry to disturb you with another post, but your query as to why I
wanted to write part of a struct to a file and not the whole struct is
that I don't how to write a struct to the file, once it has been
validated.
e.g

if(!CheckDigitForCustomerCode( Newcrecord.customercode ))
{
prnfile<< "Invalid customer code"<< endl;
}
return false;

if(!CheckDigitIsaNumber( Newcrecord.customercode ))
{
prnfile<< "Invalid customer code" <<endl;
}
return false;

return true

validdata.write((char*) record, Newcrecord)

// does this mean that the function
has processed the two Check digit functions and found them to be true,
and is it safe to now write the whole struct?
 
K

Karl Heinz Buchegger

muser said:
if(!CheckDigitForCustomerCode( Newcrecord.customercode ))
{
prnfile<< "Invalid customer code"<< endl;
}
return false;

if(!CheckDigitIsaNumber( Newcrecord.customercode ))
{
prnfile<< "Invalid customer code" <<endl;
}
return false;

return true

validdata.write((char*) record, Newcrecord)

// does this mean that the function
has processed the two Check digit functions and found them to be true,
and is it safe to now write the whole struct?

I assume with 'the whole struct' you mean 'Newrecord'.

stream.write( Pointer_to_beginning_of_data, How_many_bytes );

Thus:

validdata.write( &Newrecord, sizeof( Newrecord ) );

or if you just want the customercode from the Newrecord:

validdata.write( Newrecord.customercode, sizeof Newrecord.customercode ) ;
 
M

muser

Karl Heinz Buchegger said:
I assume with 'the whole struct' you mean 'Newrecord'.

stream.write( Pointer_to_beginning_of_data, How_many_bytes );

Thus:

validdata.write( &Newrecord, sizeof( Newrecord ) );

or if you just want the customercode from the Newrecord:

validdata.write( Newrecord.customercode, sizeof Newrecord.customercode ) ;

thank you karl you are a star.
 
M

muser

thank you karl you are a star.

I've used: validdata.write((char*) &Newcrecord, sizeof(Newcrecord)
using the example of the above code, could anyone please tell me how a
binary file could be created, (which my program does) but not actually
open a text space, or write the above information to it. If the
validdata file had the .DAT extension, i.e.
validdata.open("A:\\514650VD.DAT" ios::eek:ut || ios::binary) does it
change the way in which I write to a binary file?
My compiler opens in the visual J++ environment.
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top