|
-
February 19th, 2009, 07:38 PM
#1
Question re: fread/fwrite
hello all,
I have a unsigned char 1d array of size_x, let's call it Array1. (It contains information on intensity of a grey scale graph). Before saving it to a file, i can go Array1[245] and obtain the intensity value at that position of the array. Then I save it this way:
FILE * the_file;
the_file = fopen(filename,"w+b")
fwrite(Array1,sizeof(unsigned char),size_x,the_file);
so now the saved file should contain all these information right?
then I tried to read the same file back
unsigned char * read_mat;
file_read = fopen(filename, "rb" );
fseek (file_read , 0 , SEEK_END);
file_size = ftell (file_read);
rewind (file_read);
read_mat = new unsigned char[file_size];
fread(read_mat,sizeof(unsigned char),file_size,file_read);
now should the read_mat array contain the same information as Array1 the one i used to save my file with?
because I tried to just read a random number from read_mat[] but it always gives me zero.
..this is one way i did:
CString temp_text;
for (int i=0;i<file_size;i++)
{
int temp_num=(int)read_mat[i];
temp_text.Format("int is %d",temp_num);
MessageBox(temp_text);
}
did something go wrong with saving? or something go wrong with reading?
the file I saved shows the expected size.
-
February 20th, 2009, 12:15 AM
#2
Re: Question re: fread/fwrite
hi,
try to print your array before writing to the file.. ! have you checked the array contents. ???
-Anant
"Devise the simplest possible solution that solves the problems"
-
February 20th, 2009, 04:50 AM
#3
Re: Question re: fread/fwrite
Does the binary file contain the correct information? You can check it with a hex viewer for instance. Both fread and fwrite are returning the number of bytes successfully read or written. Check that and compare with the actual number of bytes you expect.
BTW, if you write in C++, you could use ifstream/ofstream to read/write and vector<unsigned char> to hold the array.
-
February 20th, 2009, 12:33 PM
#4
Re: Question re: fread/fwrite
I've used a hex viewer to verify my message, it looks perfectly fine. The same size as I've expected it to be. there are valid data within it.
I've also printed out the array before saving and it was perfectly fine. So the file itself is not the problem. Just how I read it out and how I print out the read file seems to be the problem.
i've checked the read/written byte and they agree with each other.
-
February 23rd, 2009, 02:34 AM
#5
Re: Question re: fread/fwrite
Hi,
FILE * the_file;
the_file = fopen(filename,"w+b")
fwrite(Array1,sizeof(unsigned char),size_x,the_file);
fclose(the_file );
either close file before opening again or use same handle to read contents....from file i.e alraedy open file handle !
-Anant
"Devise the simplest possible solution that solves the problems"
-
February 23rd, 2009, 05:32 AM
#6
Re: Question re: fread/fwrite
No need to close file. It's ok to have several open handles to the same file. Try calling fflush before calling fread though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|