14 Replies - 1720 Views - Last Post: 18 January 2011 - 09:34 AM Rate Topic: -----

#1 Guest_bob sands*


Reputation:

Manipulating raw binary data from a file, opechar* to double*

Posted 16 January 2011 - 11:49 AM

Hi there,
I'm stuck in something that maybe some of you consider pretty simple, but i'm not really an expert in c++, so i will explain a little bit what i want to do and where is my problem:
- I opened a raw image file and buffered in memory. With the buffered data i pretend to make calculations with the binary data, and here is when the problem arise: to open the file and charge it in memory i use char*, but for manipulations i pretend to use double* variables.

Does anyone can point me in the right direction in order to convert the file in memory from char* to double*?

Here what i have done until now:

#include <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;
fstream::pos_type size;
double* x;
char* y;
int main(int i, char *img[])   // i is a counter, img the name of the file
  { 

  i=1;
// Opening the file
  ifstream file (img[i], ios::in|ios::binary|ios::ate); 
  if (file.is_open())
  {
    size = file.tellg();       // knowing the size of the file
    y = new char [size];       // changing the size in the buffer
    file.seekg (0, ios::beg);  // repositioning pointer in the begining
    file.read (y, size);       // reading the file, loading to "y"
    file.close();              // closing file

// here is what i want to do, but i don't know how: assign from char* to double*
    for (i=0; i<size; i++) { x[i] = y[i]; } 
// Obviously what is above is wrong, i just leave it as a reference
    
// here i will call a future external function for calculations with the data
    futurefun(x,size);

// cleaning the mess
    delete[] x;
    delete[] y;
  }
  else cout << "Unable to open file";

  return 0;
}



I tried using:
 for (i=0; i<size; i++) { x[i] = atof(y[i]); } 

and also:
 for (i=0; i<size; i++) { stringstream(y[i]) >> x[i]; } 

but didn't work.

Any comments or ideas i will really appreciate.

Is This A Good Question/Topic? 0

Replies To: Manipulating raw binary data from a file, opechar* to double*

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1687
  • View blog
  • Posts: 3,206
  • Joined: 30-May 10

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 16 January 2011 - 12:07 PM

Well your loop would seem fine, so long as you had

x = new double [size]; 


as well.

Posting actual error messages as well can be useful.

This post has been edited by Salem_c: 16 January 2011 - 12:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 16 January 2011 - 04:48 PM

Why not treat the data read from the file as char*?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_bob sands*


Reputation:

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 16 January 2011 - 10:56 PM

Thanks for the answer.
I add your suggestion, but even when i don't have any error compiling, i'm making operations that don't give me the expected result, and i'm sure that this is because i'm "adding apples with pears" (working with char and double).

For instance, once i have all the data in the y[] (that is a char*) i want to make operations to all the array in a function, like:
  double a=-0.5;
  for (i=1;i<n-2;i+=2) {
    y[i]+=a*(y[i-1]+y[i+1]);
  } 



As you can see, having the data in char format is a problem.

Any comments, welcome.
Was This Post Helpful? 0

#5 Guest_bob sands*


Reputation:

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 16 January 2011 - 11:04 PM

View PostButchDean, on 16 January 2011 - 04:48 PM, said:

Why not treat the data read from the file as char*?


Thanks for the answer.
Yes, this could be a possibility, but i'm not really sure how to make, for instance, this operation work using only char:

double a=-0.5;
for (i=1;i<n-2;i+=2) {
y[i]+=a*(y[i-1]+y[i+1]);
} 



The data in y[] is raw image data, buffered in memory as char*, but the operation use the variable a, which is double and its value is -0.5.

Any comments, welcome.
Was This Post Helpful? 0

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 16 January 2011 - 11:12 PM

you should fix your include directives.

#include <cstdio>
#include <cstdlib>
#include <cmath>


instead of
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


with all of the standard headers from C you should drop the .h and add a c on to the front..
Was This Post Helpful? 1
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1687
  • View blog
  • Posts: 3,206
  • Joined: 30-May 10

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 17 January 2011 - 12:41 AM

So if you have 3 values for y[] as being
char y[] = { 40, 80, 120 };


And you try to update the middle one with
y[1]+=a*(y[0]+y[2]);


What answer do you get, and what were you expecting?

There are lot of opportunities for overflow, rounding and truncation in this expression.
Was This Post Helpful? 0
  • +
  • -

#8 (Cryptic)  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 13-January 11

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 17 January 2011 - 01:17 AM

I'm not sure if this is any help, I also don't really understand why you want to multiply a char or one byte value by negative half.

But you could try reinterpret_cast.

double * d;
for(i = 1; i < n-2; i += 2)
{
  d = interpret_cast<double*>(y[i-1] + y[i + 1]);
  y[i] += inrerpret_cast<char*>(a * d);
}




Were you looking for something like that?
Was This Post Helpful? 0
  • +
  • -

#9 Guest_bob sands*


Reputation:

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 17 January 2011 - 06:51 AM

View Post(Cryptic), on 17 January 2011 - 01:17 AM, said:

I'm not sure if this is any help, I also don't really understand why you want to multiply a char or one byte value by negative half.

But you could try reinterpret_cast.

double * d;
for(i = 1; i < n-2; i += 2)
{
  d = interpret_cast<double*>(y[i-1] + y[i + 1]);
  y[i] += inrerpret_cast<char*>(a * d);
}




Were you looking for something like that?


Thanks for your answer.
I will explain a little bit more about what i want to do in order to check with you guys if i'm in the right path or i'm completely lost (and right now i think is the second option for me).

My intentions are:
1. Open a file, which is a raw image file.
2. Load it in memory.
3. Apply "in house" compression:
3.1. Apply it a forward wavelet transform to the entire file
3.2. Coding using Huffman coding
4. Apply "in house decompression:
4.1. Decoding using Huffman decoding
4.2. Apply inverse wavelet transform to the entire file

The code that i had assembled until know is this:

#include <fstream>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;
fstream::pos_type size;
char* x; 
double* tempbank=0;

// ***** FORWARD WAVELET *****
void fwt53(char* x,int n) {
  double a[3] = {-0.5,0.25,sqrt(2.0)};
  int i;

  // Predict 1 (a=-0.5)
  for (i=1;i<n-2;i+=2) {
    x[i]+=a[0]*(x[i-1]+x[i+1]);
  } 
  x[n-1]+=2*a[0]*x[n-2];

  // Update 1 (a=0.25)
  for (i=2;i<n;i+=2) {
    x[i]+=a[1]*(x[i-1]+x[i+1]);
  }
  x[0]+=2*a[1]*x[1];

  // Scale (a=sqrt(2.0))
  for (i=0;i<n;i++) {
    if (i%2) x[i]*=a[2];
    else x[i]/=a[2];
  }

  // Pack
  if (tempbank==0) tempbank=(double *)malloc(n*sizeof(double));
  for (i=0;i<n;i++) {
    if (i%2==0) tempbank[i/2]=x[i];
    else tempbank[n/2+i/2]=x[i];
  }
  for (i=0;i<n;i++) x[i]=tempbank[i];
}
// ***** END OF FORWARD WAVELET *****

// ***** INVERSE WAVELET *****
void iwt53(char* x,int n) {
  double a[3] = {1/sqrt(2.0), -0.25, 0.5};
  int i;

  // Unpack
  if (tempbank==0) tempbank=(double *)malloc(n*sizeof(double));
  for (i=0;i<n/2;i++) {
    tempbank[i*2]=x[i];
    tempbank[i*2+1]=x[i+n/2];
  }
  for (i=0;i<n;i++) x[i]=tempbank[i];

  // Undo scale
//  a=1/sqrt(2.0);
  cout << a << endl;
  for (i=0;i<n;i++) {
    if (i%2) x[i]*=a[0]; 
    else x[i]/=a[0];
  }
  
  // Undo update 1
//  a=-0.25;
  for (i=2;i<n;i+=2) {
    x[i]+=a[1]*(x[i-1]+x[i+1]);
  }
  x[0]+=2*a[1]*x[1];

  // Undo predict 1
//  a=0.5;
  for (i=1;i<n-2;i+=2) {
    x[i]+=a[2]*(x[i-1]+x[i+1]);
  }
  x[n-1]+=2*a[2]*x[n-2];
}
// ***** END OF INVERSE WAVELET *****

// ***** MAIN *****
int main(int i, char *img[])   // i is a counter, img the name of the file
{
  i=1;
// Opening the file
  ifstream file (img[i], ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();       // knowing the size of the file
    x = new char [size];       // changing the size in the buffer
    file.seekg (0, ios::beg);  // repositioning pointer in the begining
    file.read (x, size);       // reading the file, loading to "y"
    file.close();              // closing file

    // Display Original Raw Data
    printf(" \n");
    cout << "Original Data" << endl;
    for (i=0; i<5; i++) { printf("x:%x ", x[i]); }
    printf(" \n");

// Forward
    fwt53(x,5);
    cout << "Wavelet Coefficients" << endl;
    for (i=0; i<5; i++) { printf("wc:%x ", x[i]); }
    printf(" \n");
    printf(" \n");

// Inverse
    iwt53(x,5); 
    cout << "Reconstructed Data" << endl;
    for (i=0; i<5; i++) { printf("x':%x ", x[i]); }
    printf(" \n");
    printf(" \n");

// cleaning the mess
    delete[] x;

  }
  else cout << "Unable to open file";

return 0;
// *****END OF MAIN *****
}



Maybe i'm going in the right way, but using the wrong path, so any comments are welcome.

Thanks in advance for your suggestions.
Was This Post Helpful? 0

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,685
  • Joined: 25-December 09

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 17 January 2011 - 07:31 AM

You should not mix C style input/output (scanf/printf) with C++ style input/output (cin/cout).



Jim
Was This Post Helpful? 1
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 17 January 2011 - 10:44 AM

Well what follows is not exactly answering your question but I hope that it will help you be a better programmer and perhaps have less issues moving forward:

A "Conceptual Error" is when someone does something without really *understanding* -- even if they get things to work, they are not working the way the programmer "thinks" they do. For example, thinking that the sun rises each day because the sun revolves around the earth is a basic model that explains the behavior and works well enough -- but it isn't really what is going on.

Your programming seems to have a few conceptual errors.

// ***** MAIN *****
int main(int i, char *img[])   // i is a counter, img the name of the file
{
  i=1;
// Opening the file
  ifstream file (img[i], ios::in|ios::binary|ios::ate);
...



Defining main in this way is not an error, but the variable names and the comment indicate that you don't understand how the declaration works.

The standard declaration of main() is:

int main(int argc, char* argv[]) //argc is the number of arguments, argv is the command line arguments


so here "argc" stands for "Argument Count", and "argv" stands for "Argument Values" -- argc gives you the "size" of that array argv.

now argv[0] is always the program as it was called
argv[1] the first argument
argv[2] the second etc.

so if I run my program as:

> C:\dev\project1\MyProgram.exe image.raw /?

then argc = 3
argv[0] = "C:\dev\project1\MyProgram.exe"
argv[1] = "image.raw"
argv[2] = "/?"

to demonstrate:
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
    cout << "argc = " << argc << endl;
    for(int i = 0; i < argc; ++i) {
        cout << "argv[" << i << "] = \"" << argv[i] << "\"" << endl;
    }
    return 0;
}
> C:\CProjects\Forum Help\mainArguments.exe image.raw /?
argc = 3
argv[0] = "C:\CProjects\Forum Help\mainArguments.exe"
argv[1] = "image.raw"
argv[2] = "/?"
> C:\CProjects\Forum Help\mainArguments.exe 1 2 3 4 5 6 7 8 9
argc = 10
argv[0] = "C:\CProjects\Forum Help\mainArguments.exe"
argv[1] = "1"
argv[2] = "2"
argv[3] = "3"
argv[4] = "4"
argv[5] = "5"
argv[6] = "6"
argv[7] = "7"
argv[8] = "8"
argv[9] = "9"


So if you wanted the program to be launch as:

"MyProgram.exe image.raw"

you would do something like this:
#include <iostream>
#include <fstream>

using namespace std;


const int IMAGE_FILENAME = 1; // argv[1] should be our image file name.

int main(int argc, char* argv[]) {
    if (argc < 2) {
        cout << "Usage:\n> MyProgram filename\n\nWhere *filename* is the path to a raw image file" << endl;
        return 1; // return values other than 0 generally indicate an error;
    }
    
    //By using a constant we make our intentions more explicite
    ifstream imageFile (argv[IMAGE_FILENAME], ios::in|ios::binary|ios::ate);
    if ( !imageFile.is_open() ) {
        //Do you really want this at the end of your program or would it be better to place it near 
        //   the point where you attempt to open the file?
        cout << "ERROR: Unable to open file" << endl;
        return 2;
    } else {
        cout << "File opened successfully" << endl;
    
    
        //Always release resources when you are done with them...
        imageFile.close();

    }
    return 0; // return value of 0 generally indicates that no errors occurred.
}


NEXT

A file contain data in for form of bytes stored on a disk. There are two general ways of looking at the data: As text, or as binary data.

Note that all files are "binary files" -- because ALL data is "binary data" in a computer. What is different is how we look at the encoding of that data.

Text files are generally encoded using something like ASCII where for each possible value that can be stored in a byte we assign some "meaning" to it by assigning a character. ASCII is not the only text encoding and the byte value of 65 does not always represent an "A". However when you open a file up in text mode in C/C++ text files it generally does.

When you open a file binary you are asking to skip this text encoding and access the data directly. So here you are responsible for what the "bytes" represent.

You can choose to imagine them as text or as a long list of integers, or a long list of doubles, or a long list of records, or records mixed with text mixed with integers etc. Its up to you to determine what the data represents.

So to read in data you need to know what the file format looks like. When you say "raw image" what does that mean?

is it a text file like a PPM/PPG file or is it raw RGB data? if so then is it 8bit, 24bit, 32bit data?

these differences -- the format for the file -- is important if you wish to preform some kind of transform on the data itself.

however for what you are doing (some kind of compression) maybe you don't actually care what the data is. In this case you just need to read the data in for format you would like to view it in (in this case doubles) -- taking into consideration that the file may not actually work out evenly to doubles. So here is an example that reads in a file as an array of doubles. Note that I am going to use my snippet "Debugger Style Memory Dump" to view the actual data in memory -- this DOES go against jimblumberg's good advice not to mix cout/printf etc but I think it will be fine for these purposes.

#include <iostream>
#include <fstream>

//needed for C-snippet of debugger style memory dump...
#include <cstdio>
#include <cstdlib>
void displayMemory(char *address, int length);


using namespace std;


const int IMAGE_FILENAME = 1; // argv[1] should be our image file name.

int main(int argc, char* argv[]) {
    if (argc < 2) {
        cout << "Usage:\n> MyProgram filename\n\nWhere *filename* is the path to a raw image file" << endl;
        return 1; // return values other than 0 generally indicate an error;
    }
    
    //By using a constant we make our intentions more explicite
    ifstream imageFile (argv[IMAGE_FILENAME], ios::in|ios::binary|ios::ate);
    if ( !imageFile.is_open() ) {
        //Do you really want this at the end of your program or would it be better to place it near 
        //   the point where you attempt to open the file?
        cout << "ERROR: Unable to open file" << endl;
        return 2;
    } else {
        cout << "File opened successfully" << endl;
        size_t sizeBytes = imageFile.tellg();
        size_t sizeDoubles = sizeBytes / sizeof(double); // note that is sizeof(double) does not divide evenly then we need to add 1
        bool needsPadding = sizeBytes % sizeof(double) != 0;
        if (sizeDoubles == 0) {
            cout << "WARNING: File contains no data." << endl;
        } else {
            // if we need padding then we need to allocate 1 extra double, else just sizeDoubles...
            double *dataDoubles = new double[needsPadding ? sizeDoubles + 1: sizeDoubles];
            imageFile.seekg (0, ios::beg);  
            imageFile.read( reinterpret_cast<char*>(dataDoubles), sizeDoubles*sizeof(double) );
            if (needsPadding) {
                //ok we still have a couple of extra bytes we need to pack into our array...
                char *leftover = new char[sizeof(double)];
                for (int i = 0; i < sizeof(double); ++i) { leftover[i] = 0; }
                imageFile.read(leftover, sizeBytes % sizeof(double));
                dataDoubles[sizeDoubles] = *(reinterpret_cast<double*>(leftover));
                delete leftover;
            }
            imageFile.close(); // done with the file
            cout << "Data read in successfully..." << endl;
            
            for (int i = 0; i < sizeDoubles; i++) {
                cout << "data[" << i << "] =\t" << dataDoubles[i] << ((i%4 == 3) ? "\n" : "\t");
            }
            if (needsPadding) {
                //print out the padded double at the end...
                cout << "*data[" << sizeDoubles << "] =\t" << dataDoubles[sizeDoubles] << endl;
            }
            
            cout << "\n-------------- DATA ---------------------\n";
            
            
            displayMemory((char*)dataDoubles, sizeDoubles*sizeof(double));
            
            delete dataDoubles;
        }
        
    
        //Always release resrouces when you are done with them...
        imageFile.close();

    }
    return 0; // return value of 0 generally indicates that no errors occured.
}


/****
 * This function will print out a memory dump of the current address for the specified
 * length. Note that if the current program does not have access to the block of memory
 * from address to address + length then an access violation will occur. So the user
 * must be careful not try to display memory that the program does not have access to.
 * <br>
 * The following is the format for the output:
 * <bR>
 * ADDRESS  | 16 CHARACTERS OF DATA IN HEX                    | ASCII CHARS
 * 0040103C | 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 21 21 00 __ | Hello.World!!!.
 * <br>
 * Note that '__' is place holder if the length is not a multiple of 16 (one complete
 * line.) This helps keep the report consistent and easy to scan with eyes.
 */
void displayMemory(char *address, int length) {
	int i = 0; //used to keep track of line lengths
	char *line = (char*)address; //used to print char version of data
	unsigned char ch; // also used to print char version of data
	printf("%08X | ", (int)address); //Print the address we are pulling from
	while (length-- > 0) {
		printf("%02X ", (unsigned char)*address++); //Print each char
		if (!(++i % 16) || (length == 0 && i % 16)) { //If we come to the end of a line...
			//If this is the last line, print some fillers.
			if (length == 0) { while (i++ % 16) { printf("__ "); } }
			printf("| ");
			while (line < address) {  // Print the character version
				ch = *line++;
				printf("%c", (ch < 33 || ch == 255) ? 0x2E : ch);
			}
			// If we are not on the last line, prefix the next line with the address.
			if (length > 0) { printf("\n%08X | ", (int)address); }
		}
	}
	puts("");
}


So the output of this (when using the source file as the input) is roughly:
> "C:\CProjects\Forum Help\asDoubleArray.exe " asDoubleArray.cpp
File opened successfully
Data read in successfully...
data[0] =	2.65293e+180	data[1] =	4.78618e+180	data[2] =	7.26906e+223	data[3] =	2.98782e+184
data[4] =	6.96284e-245	data[5] =	2.77893e+180	data[6] =	2.19993e-152	data[7] =	4.2634e+180
...
data[628] =	6.01347e-154	data[629] =	7.09523e-077	*data[630] =	6.8004e-310

-------------- DATA ---------------------
002D8FF0 | 23 69 6E 63 6C 75 64 65 20 3C 69 6F 73 74 72 65 | #include.<iostre
002D9000 | 61 6D 3E 0D 0A 23 69 6E 63 6C 75 64 65 20 3C 66 | am>..#include.<f
...
002DA380 | 3C 20 65 6E 64 6C 3B 0D 0A 2F 2F 20 20 20 20 7D | <.endl;..//....}
002DA390 | 0D 0A 2F 2F 20 20 20 20 72 65 74 75 72 6E 20 30 | ..//....return.0

> Process Exit Code: 0
> Time Taken: 00:01


not that I use ... to hide much of the output.

So you can see that the last double #630 is a padding double and the last row of our HexDump does not have the ending semi-colon and brace etc. that one would expect -- that is because I didn't display the data for the padding double in the HexDump. -- Bug you can see that the array of doubles *actually* contains the text data for the program.

Notes:

#1 I do not recommend doing this with the source file as I have done here.
#2 Floating point operations in the computer have rounding errors -- so any process that converts data to doubles and then does ANY ARITHMETIC on those doubles will not be able to reproduce the original data with 100% accuracy!!! This is very important to note if you are expecting to reproduce text using a scheme like this.
Was This Post Helpful? 3
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,685
  • Joined: 25-December 09

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 17 January 2011 - 11:44 AM

You should also use more descriptive variable names.

You also have global variables with the same names as variables in your function parameter lists. This means that the variable inside the function is not the same as the global variable. This can lead to hard to find bugs. Global variables should only be use in very rare situations.

You have a memory leak in both your functions since you are using malloc and never freeing this memory. Also since you are programming in C++ you should use new/delete instead of malloc.

Jim
Was This Post Helpful? 1
  • +
  • -

#13 Guest_bob sands*


Reputation:

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 18 January 2011 - 01:47 AM

ButchDean, ishkabible, Salem_c, (Cryptic), NickDMax & jimblumberg
Thanks for all your comments, really appreciate the patience and courtesy to answer the post.

I'm not an expert, just experimenting and trying to do something, not because i have to, but because i want to, learning by myself, with your help, and all your comments helps me a lot.

About my initial c++ project, i will put my horses in hold until i read a little bit more about the "Conceptual Errors" that NickDMax nicely point it.

I would like to ask a question, regarding one of the NickDMax comments:
- If i open a file (let's call ORI-FILE.raw) and create a new one (NEW-FILE.raw), and use a For statement in order to read and copy byte a byte the from ORI-FILE.raw to NEW-FILE.raw, the result is not an exact copy?.

I ask this because he wrote "When you open a file binary you are asking to skip this text encoding and access the data directly". So did i misunderstand and the "copy" will be exactly as the original file, or this "copy" in fact is totally different?... Maybe the question could be stupid from some, but i prefer to ask rather than stay ignorant.

Anyways, thank you very much for your kindly answers/suggestions, and any comments for my last question will be welcome.
Was This Post Helpful? 0

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 18 January 2011 - 08:41 AM

Short answer: yes. A byte for byte copy done in binary mode is pretty much the original file.

Long answer:
If you open a file in binary mode and copy byte-for-byte in a for-loop then you will make a *nearly* exact copy -- you will exactly copy all data contain IN the file. However some operating systems attach "additions" to a file such as "resource forks" or "streams" which are technically part of the file but do not reside inside the data available to you in C++'s standard "open". Think of this as coping "mytext.txt" to "mycopy.bmp" -- the contents of the files would be the same, but mytext.txt would be associated to a text editor while the OS would think mycopy.bmp was a bitmap -- the extension (.txt) contained data important to the file but not IN the file.

If you opened a file in text mode you *may* actually introduce some differences depending upon how you read the file. As I understand it, if you get 1 char at a time you would be able to do the same kind of copy as in binary, but if you used other input mechanism such as cin's formatted input or getline() you may introduce differences in whitespace.
Was This Post Helpful? 1
  • +
  • -

#15 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1687
  • View blog
  • Posts: 3,206
  • Joined: 30-May 10

Re: Manipulating raw binary data from a file, opechar* to double*

Posted 18 January 2011 - 09:34 AM

I would suggest that before you start messing with large files, you start with something like
unsigned char x[] = { 0x01, 0x22, 0x44, 0x66, 0x88 };
fwt53(x,5);
iwt53(x,5);



Not only is it easier to debug with a small data set, it is also a lot more convenient for when you post on a forum. Your readers can simply copy/paste the complete program and see exactly what you see (without the need for you to upload additional data files).
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1