Solved

simple binary read/write question

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PCH Sample and Makefile 14 70
MFC Model Dialogs 10 57
C++ get user from AD  (VS6) 7 48
How to split this in C++ 4 76
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now