Click to See Complete Forum and Search --> : pointer to string/ file problem


BigSadBear
October 6th, 2005, 02:36 AM
Well, I have this problem: I want to read a file, get the size, and read all symbols. Then I do some stuff with the data and I`m trying to write it in file. But the program is adding another 65 symbols at the end of the file. The file is 6151 symbols long. Here is the code:

long file_size;
//here it reads the file_size - I`m sure it`s OK

unsigned char* input=new unsigned char[file_size]; //def. and init. the pointer
for(i=0; i<size; i++){
input[i]=in.get();
}
//and then do something to the data........
//then write it in file
for(int i=0;i<size;i++) out_file<<input[i];

So...any suggestions. Thanks!

Marc G
October 6th, 2005, 03:38 AM
Don't read/write character by character, because that's extremely slow. Read/write blocks, like:
long file_size;
//here it reads the file_size - I`m sure it`s OK

unsigned char* input=new unsigned char[file_size]; //def. and init. the pointer
in.read(input, file_size);

//and then do something to the data........
//then write it in file
out_file.write(input, file_size);
NOTE: How do you open the file? How do you retrieve the length of the file?

BigSadBear
October 6th, 2005, 04:19 AM
It`s OK. I`ve decided to use vector and it`s work.Thanks anyway.