?
Solved

Write a Byte array to a file

Posted on 2007-11-29
12
Medium Priority
?
413 Views
Last Modified: 2010-04-01
How can i write a byte array,for eg:

BYTE *buf = new BYTE[200];
buf=somedata ;

into a file,for eg "bytedata.dat"

please provide some good eg
0
Comment
Question by:aslamshikoh1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20375217
The easiest way would be to

#include <stdio.h>

//...


BYTE *buf = new BYTE[200];

//...

FILE* p = fopen(bytedata.dat","wb");

fwrite(buf, 1, 200);

fclose(p);
0
 
LVL 86

Expert Comment

by:jkr
ID: 20375245
Or, more on the C++ side:

#include <fstream>
using namespace std;


BYTE *buf = new BYTE[200];

//..


ofstream os("bytedata.dat", ios_base::out | ios_base::binary);

os.write(buf,200);

os.close();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Expert Comment

by:evilrix
ID: 20375459
Don't forget to free your buffer
BYTE *buf = new BYTE[200];
delete [] buf;

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20375461
Also, in C++ consider using std::string instead...
http://msdn2.microsoft.com/en-us/library/syxtdd4f(VS.80).aspx
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20375525
An example using strings and streams...
#include <string>
#include <iostream>
#include <fstream>
 
int main()
{
	// The data we wish to write to file
	std::string sData("Hello world");
 
	// Open the file as a stream (destructor will close for us)
	std::ofstream ofs("c:\\temp\\data.txt");
 
	// If file was opened ok...
	if(ofs.is_open())
	{
		// Write out data to it
		ofs << sData;
 
		// If write error then report it
		if(!ofs.good())
		{
			std::cerr << "Failed to write data" << std::endl;
		}
	}
 
	return 0;
}

Open in new window

0
 

Author Comment

by:aslamshikoh1
ID: 20375878
Ok i got it but i have a continuous stream of bytes to write to a file.Its a thread which has a while loop like this:

while(oEditStreamImpl->isActive_  && --count)
      {
            

            ofstream os("bytedata.dat", ios_base::out | ios_base::binary);
                 os.write(buf,200);
}

os.close()

so how can i append the data to end of the file,like something to do with the file pointer,right now data is getting overwritten.
0
 

Author Comment

by:aslamshikoh1
ID: 20375906
also write(buf,200) complains about not able to accept a byte,i guess a char array is what it accepts,so im just casting it like

write((char*)buf,200)

is it OK to do this
0
 
LVL 40

Accepted Solution

by:
evilrix earned 375 total points
ID: 20376027
Try: -
ofstream os("bytedata.dat", ios_base::binary);
while(oEditStreamImpl->isActive_  && --count)
{
    os.write(reinterpret_cast<char *>(buf),200);
}
os.close();

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 375 total points
ID: 20376028
>>is it OK to do this

yes, absolutely, since the stream is openend as binary.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20376077
>> is it OK to do this
Yes, a stream expects a char * but you can cast any pointer type to that as long as you specify correctly the number of chars when writing it out. It helps if you just think of it as writing signed bytes.

NB. a wide stream expects wchar_t *
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376145
btw, this :

        BYTE *buf = new BYTE[200];
        buf=somedata ;

creates a memory leak ... you're overwriting the pointer to the allocated 200 bytes with a new value.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question