Solved

simple binary read/write question

Posted on 2004-08-13
3
246 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
[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
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

695 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