Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > write function

Reply
Thread Tools

write function

 
 
muser
Guest
Posts: n/a
 
      11-17-2003
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.
 
Reply With Quote
 
 
 
 
Joe Estock
Guest
Posts: n/a
 
      11-17-2003
"muser" <> wrote in message
news: m...
> 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);


 
Reply With Quote
 
 
 
 
Rick Noelle
Guest
Posts: n/a
 
      11-17-2003
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


On Mon, 17 Nov 2003, Joe Estock wrote:

> "muser" <> wrote in message
> news: m...
> > 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);
>
>
>


--
--------------------------------------------
My real email address excluded to avoid SPAM

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      11-17-2003
On 17 Nov 2003 09:18:39 -0800, (muser) wrote:

>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
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      11-17-2003
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.
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

 
Reply With Quote
 
Rick Noelle
Guest
Posts: n/a
 
      11-17-2003
On Mon, 17 Nov 2003, Thomas Matthews wrote:

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

 
Reply With Quote
 
Rick Noelle
Guest
Posts: n/a
 
      11-17-2003
On Mon, 17 Nov 2003, Rick Noelle wrote:

> On Mon, 17 Nov 2003, Thomas Matthews wrote:
>
> > 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
>
>


--
--------------------------------------------
My real email address excluded to avoid SPAM

 
Reply With Quote
 
muser
Guest
Posts: n/a
 
      11-24-2003
Rick Noelle <> wrote in message news:<Pine.LNX.4.44.0311171556280.25286->...
> On Mon, 17 Nov 2003, Rick Noelle wrote:
>
> > On Mon, 17 Nov 2003, Thomas Matthews wrote:
> >
> > > 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.
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-24-2003


muser wrote:
>
>
> 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.

--
Karl Heinz Buchegger

 
Reply With Quote
 
muser
Guest
Posts: n/a
 
      11-24-2003
Karl Heinz Buchegger <> wrote in message news:<>...
> muser wrote:
> >
> >
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
DVD Write speed - Not able to write at 8 speed Vincent Wonnacott Computer Support 1 09-16-2004 03:14 PM
Using write function within email Module to write get_payload to afile. Chuck Amadi Python 0 06-22-2004 12:13 PM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57