I am trying to create a file of bytes from my character array
char* chars with length int count.

I wrote the code:
FileStream file = new FileStream(S"chars.dat", FileMode::Create,
FileAccess::Write);
BinaryWriter binary = new BinaryWriter(file);
binary.write(chars, count);
binary.close();

My compiler doesn't recognize FileStream and BinaryWriter.
I tried:
using namespace System::IO;
but it doesn't recognize that either?

Is my code correct, and what "using" or "include" do I need?
By the way, in new FileStream(S"chars.dat", ...), what does the "S" mean?

Thanks for reading this.

RE: how to write a binary file by Ananya

Ananya
Thu May 24 04:13:03 CDT 2007

Well, I was "googleing" for some code, and even though I asked for C++, it
seems I ended up with some C# code, which I cannot use.

So I tried the following C++ code:
unsigned char *pixels = ...
char *charpixels = reinterpret_cast<char*>(pixels);
fstream file;
file.open("chars.dat", ios::out);
file.write(charpixels, count);

Now the code compiled, but it doesn't seem to write the correct byte.
Does reinterpret_cast not work right?


"Ananya" wrote:

> I am trying to create a file of bytes from my character array
> char* chars with length int count.
>
> I wrote the code:
> FileStream file = new FileStream(S"chars.dat", FileMode::Create,
> FileAccess::Write);
> BinaryWriter binary = new BinaryWriter(file);
> binary.write(chars, count);
> binary.close();
>
> My compiler doesn't recognize FileStream and BinaryWriter.
> I tried:
> using namespace System::IO;
> but it doesn't recognize that either?
>
> Is my code correct, and what "using" or "include" do I need?
> By the way, in new FileStream(S"chars.dat", ...), what does the "S" mean?
>
> Thanks for reading this.
>
>

Re: how to write a binary file by Alex

Alex
Thu May 24 05:02:27 CDT 2007

Ananya wrote:
> Well, I was "googleing" for some code, and even though I asked for C++, it
> seems I ended up with some C# code, which I cannot use.
>
> So I tried the following C++ code:
> unsigned char *pixels = ...
> char *charpixels = reinterpret_cast<char*>(pixels);
> fstream file;
> file.open("chars.dat", ios::out);

Try this:
file.open("chars.dat", ios::out | ios::binary);

Alex

Re: how to write a binary file by Ananya

Ananya
Thu May 24 05:23:01 CDT 2007

Well, I tried that, but the chars.dat file is empty.

By the way, reinterpret_cast does work correctly in the examples I tried.

So, why does my chars.dat file not get written?


"Alex Blekhman" wrote:

> Ananya wrote:
> > Well, I was "googleing" for some code, and even though I asked for C++, it
> > seems I ended up with some C# code, which I cannot use.
> >
> > So I tried the following C++ code:
> > unsigned char *pixels = ...
> > char *charpixels = reinterpret_cast<char*>(pixels);
> > fstream file;
> > file.open("chars.dat", ios::out);
>
> Try this:
> file.open("chars.dat", ios::out | ios::binary);
>
> Alex
>

Re: how to write a binary file by David

David
Thu May 24 06:28:29 CDT 2007

Ananya wrote:
> Well, I was "googleing" for some code, and even though I asked for C++, it
> seems I ended up with some C# code, which I cannot use.
>
> So I tried the following C++ code:
> unsigned char *pixels = ...
> char *charpixels = reinterpret_cast<char*>(pixels);
> fstream file;
> file.open("chars.dat", ios::out);
> file.write(charpixels, count);
>
> Now the code compiled, but it doesn't seem to write the correct byte.
> Does reinterpret_cast not work right?
>
>
> "Ananya" wrote:
>
>> I am trying to create a file of bytes from my character array
>> char* chars with length int count.
>>
>> I wrote the code:
>> FileStream file = new FileStream(S"chars.dat", FileMode::Create,
>> FileAccess::Write);
>> BinaryWriter binary = new BinaryWriter(file);
>> binary.write(chars, count);
>> binary.close();
>>
>> My compiler doesn't recognize FileStream and BinaryWriter.
>> I tried:
>> using namespace System::IO;
>> but it doesn't recognize that either?
>>
>> Is my code correct, and what "using" or "include" do I need?
>> By the way, in new FileStream(S"chars.dat", ...), what does the "S" mean?
>>
>> Thanks for reading this.

Ananya:

You should decide whether you want to use standard C++ or managed C++.
If the latter I would strongly advise you to move to VS2005 (or wait for
VS2007) because the new C++/CLI syntax is much improved over the Managed
C++ of VS2002/2003. For managed C++ questions, you would also be better
to use the group "microsoft.public.dotnet.languages.vc".

The second code you showed is standard C++. You need to open with
ios::binary.

--
David Wilkinson
Visual C++ MVP

Re: how to write a binary file by Ananya

Ananya
Thu May 24 06:07:02 CDT 2007

I am using standard C++ and I am using VS2005.

I tried

file.open("chars.dat", ios::out | ios::binary);
file.write(charpixels, pixcount);

but the chars.dat file only gets opened, it still does not get written, it
stays empty.

Shouldn't this work with standard C++? How can I make it work?


"David Wilkinson" wrote:

> Ananya wrote:
> > Well, I was "googleing" for some code, and even though I asked for C++, it
> > seems I ended up with some C# code, which I cannot use.
> >
> > So I tried the following C++ code:
> > unsigned char *pixels = ...
> > char *charpixels = reinterpret_cast<char*>(pixels);
> > fstream file;
> > file.open("chars.dat", ios::out);
> > file.write(charpixels, count);
> >
> > Now the code compiled, but it doesn't seem to write the correct byte.
> > Does reinterpret_cast not work right?
> >
> >
> > "Ananya" wrote:
> >
> >> I am trying to create a file of bytes from my character array
> >> char* chars with length int count.
> >>
> >> I wrote the code:
> >> FileStream file = new FileStream(S"chars.dat", FileMode::Create,
> >> FileAccess::Write);
> >> BinaryWriter binary = new BinaryWriter(file);
> >> binary.write(chars, count);
> >> binary.close();
> >>
> >> My compiler doesn't recognize FileStream and BinaryWriter.
> >> I tried:
> >> using namespace System::IO;
> >> but it doesn't recognize that either?
> >>
> >> Is my code correct, and what "using" or "include" do I need?
> >> By the way, in new FileStream(S"chars.dat", ...), what does the "S" mean?
> >>
> >> Thanks for reading this.
>
> Ananya:
>
> You should decide whether you want to use standard C++ or managed C++.
> If the latter I would strongly advise you to move to VS2005 (or wait for
> VS2007) because the new C++/CLI syntax is much improved over the Managed
> C++ of VS2002/2003. For managed C++ questions, you would also be better
> to use the group "microsoft.public.dotnet.languages.vc".
>
> The second code you showed is standard C++. You need to open with
> ios::binary.
>
> --
> David Wilkinson
> Visual C++ MVP
>

Re: how to write a binary file by Stefan

Stefan
Thu May 24 06:20:06 CDT 2007

On 5/24/2007 1:07 PM, Ananya wrote:
>> Ananya wrote:
>>> Well, I was "googleing" for some code, and even though I asked for C++, it
>>> seems I ended up with some C# code, which I cannot use.
>>>
>>> So I tried the following C++ code:
>>> unsigned char *pixels = ...
>>> char *charpixels = reinterpret_cast<char*>(pixels);
>>> fstream file;
>>> file.open("chars.dat", ios::out);
>>> file.write(charpixels, count);
>>>
>>> Now the code compiled, but it doesn't seem to write the correct byte.
>>> Does reinterpret_cast not work right?
> I am using standard C++ and I am using VS2005.
>
> I tried
>
> file.open("chars.dat", ios::out | ios::binary);
> file.write(charpixels, pixcount);
>
> but the chars.dat file only gets opened, it still does not get written, it
> stays empty.
>
> Shouldn't this work with standard C++? How can I make it work?

Is 'pixcount' > 0 ?
Did you correctly end your program (not killing it with CTRL-C or
anything else) ?

Try to explicitly close the file (file.close() ) and check the size
then.

S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html

Re: how to write a binary file by David

David
Thu May 24 07:21:15 CDT 2007

Ananya wrote:
> I am using standard C++ and I am using VS2005.
>
> I tried
>
> file.open("chars.dat", ios::out | ios::binary);
> file.write(charpixels, pixcount);
>
> but the chars.dat file only gets opened, it still does not get written, it
> stays empty.
>
> Shouldn't this work with standard C++? How can I make it work?

Ananya:

Well, the first code you showed us was managed C++, using the "old" syntax.

Maybe you should show us a complete program. Make a new Win32 console
project.

--
David Wilkinson
Visual C++ MVP

Re: how to write a binary file by Ananya

Ananya
Thu May 24 08:04:00 CDT 2007

Well, pixcount is the correct number, which is > 0.

However, the output file chars.dat was always only written once I exited the
method inside which I was doing the writing.

But saying file.close() also finishes the writing of the file.

Thanks a lot!


"Stefan Naewe" wrote:

> On 5/24/2007 1:07 PM, Ananya wrote:
> >> Ananya wrote:
> >>> Well, I was "googleing" for some code, and even though I asked for C++, it
> >>> seems I ended up with some C# code, which I cannot use.
> >>>
> >>> So I tried the following C++ code:
> >>> unsigned char *pixels = ...
> >>> char *charpixels = reinterpret_cast<char*>(pixels);
> >>> fstream file;
> >>> file.open("chars.dat", ios::out);
> >>> file.write(charpixels, count);
> >>>
> >>> Now the code compiled, but it doesn't seem to write the correct byte.
> >>> Does reinterpret_cast not work right?
> > I am using standard C++ and I am using VS2005.
> >
> > I tried
> >
> > file.open("chars.dat", ios::out | ios::binary);
> > file.write(charpixels, pixcount);
> >
> > but the chars.dat file only gets opened, it still does not get written, it
> > stays empty.
> >
> > Shouldn't this work with standard C++? How can I make it work?
>
> Is 'pixcount' > 0 ?
> Did you correctly end your program (not killing it with CTRL-C or
> anything else) ?
>
> Try to explicitly close the file (file.close() ) and check the size
> then.
>
> S.
> --
> Stefan Naewe stefan dot naewe at atlas-elektronik dot com
> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
> Plain text mails only, please http://www.expita.com/nomime.html
>