Solved

simple binary read/write question

Posted on 2004-08-13
3
239 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

776 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