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
Solved

simple binary read/write question

Posted on 2004-08-13
3
241 Views
Last Modified: 2008-02-26
Hi everyone,

I have an mp3 file. I want to open it, then read in every byte into an array. Then using that storage array, I would like to write it back out to a new mp3 file.

Can I use c++'s ifstream class to simply get() a char at a time from the mp3 file, store it in a char array, then write it back out to the new file using put() for each element?

I am uncertain if this method will only work for a text document. If so, what can I use to accomplish this feat for a binary file, if an mp3 file indeed is one.

I am restrained to keeping the data alive in a buffer array and writing the copied file out in that manner.

Thanks for your time
0
Comment
Question by:ragin_cajun
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 100 total points
ID: 11798061
You can do either binary file or text file that way.  You just have to open the file in binary mode.

Why wold you store the whole file in memory just to rewrite it out to a different file?  You could do it a few bytes at a time (chunks of a few K at a time or something) or just use a file copy command.
0
 
LVL 1

Author Comment

by:ragin_cajun
ID: 11798073
Thanks drichards, I know it is strange to do it this way.

I'm trying to teach myself winsock. I have a server and a client setup now, they can pass messages which is pretty fancy. I wanted to try passing a file. I thought on the server side I could load the file up into an array, then pass the bits of the array in messages to the client so it could reconstruct it on that side.

There must be a better way, I thought this might work just for kicks to start, thanks again!
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798124
Over the network especially, you'll have to read in smaller chunks because you can't send huge packets over the network.  You'll probably want to keep it around 1K chunks.  Network MTU is usually 1500 bytes or less.  1K is pretty safe.  Even if you read the entire file into memory at once, you'll still have to break it into smaller size units for transmission.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

790 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