Change your i and size from long int's to unsigned long int's.
Edit: closing the two files would be helpful:
Code:fclose(fp3);
fclose(fp3_o);
Printable View
Change your i and size from long int's to unsigned long int's.
Edit: closing the two files would be helpful:
Code:fclose(fp3);
fclose(fp3_o);
But neither of those will actually fix antyhing REAL.
What is the difference between what you are actually getting and what you expect to get?
--
Mats
I was stepping through his program and i suddenly jumped to some crazy value in the for loop.
I cchanged his program to this, and it runs fine now.
The code in blue was where the variable i went crazy on me, before I changed it. I also changed the part in green, a smidgen.Code:include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *fp3;
FILE *fp3_o;
unsigned long int i, size = 1249820;
char *b3_dn;
b3_dn = malloc(size * sizeof(char));
fp3 = fopen("test", "rb");
fp3_o = fopen("test_o", "wb");
fread(b3_dn, sizeof(char), size, fp3);
for (i=0; i<size; i++){
if(b3_dn[i] >= 50)
b3_dn[i] = 0;
}
fwrite(b3_dn, sizeof(char), size, fp3_o);
free(b3_dn);
fclose(fp3);
fclose(fp3_o);
return 0;
}
I can't see what you changed, other than white-space/layout. Care to describe what you done?
--
Mats
My compiler didn't like the variable declarations after the code began, so things like FILE pointer declarations had to be moved up.
Then, when I was stepping through it, the long int variable "i", blew up to some fantastic value early on in the for loop. It should have been about 20, and it was 324578 or something of that ilk.
I know fread() wants a type size_t for the size and number, and I thought "maybe an unsigned long would work better than a (signed) long, in this case.
So I changed the long int, to an unsigned int for i and size, and then stepped through that for loop again, well past where it blew up last time, and it had no such problem, anymore.
I don't know if this was anything but cosmetic, but I also changed the declaration and malloc of the array, so that the array was declared, first, and the malloc line of code, was done on it's own line, immediately afterward.
adak, i tried your program, but it still has problems: not only does it not change data points that are >50 to 0, it also randomly changes data points that are <50 to 0...basically it seems to be randomly changing points to 0.
what is the cause of such problems? it seems to me the program is highly unstable and erratic.
if it helps at all, i am using code::blocks to wirte/run my c programs.
You know you're making a comparison based on ASCII (or whatever char set your system is using), right?
It did fine for me after a few tweaks, but I certainly did NOT step through it 100,000+ times and rigorously compare each change made. :)
Or tell us what the value of i is when it goofs up, on the input file you uploaded? That would be a big help.
actually would it be easier to work with .txt files instead of binary files, because .txt files contain ASCII data and is directly readable? or are they really one and the same?
It would be easier. Can you set up a small sample input file that shows the error?
You've called the input "points of data". Is it possible that the data is not char's at all?
a subset (10,000 points) of my input file is found at [http://] [www.megaupload.com] [/?d=XHXXXQ3C]. the same subset of the output file is at [http://] [www.megaupload.com] [/?d=PBFEIDXU] . you can use any binary viewer to compare these 2 files ... the errors are everywhere =(
i think my binary file contains char because its size (in bytes) exactly matches the number of data points (pixels in fact, because i am dealing with images). since sizeof(char) = 1 i guess it is only correct to conclude that my data is of type char.
I think your problem is that you are using
char *b3_dn;
instead of:
unsigned char *b3_dn;
this means when you do the compare:
if (b3_dn[i] >= 50)
All values above 0x7F (decimal 127) will be seen as negative and therefore less than 50 (decimal) and left alone.
All values greater than 0x31 (decimal 49) and less than 0x80 (decimal 128) will be seen as more than 50 (decimal) and set to 0.
All values less than 0x32 (decimal 50) will be left alone
So in summary:
change b3_dn to an unsigned char*
and you program should work
I believe you have solved the mystery, Wyliek! Good catch!!
hey thanks... thats one headache down.
after that problem, i have run into another one. now that i have carried out the masking, i wish to process the masked file by dividing each (unsigned char) value by a float-type number, and output the results to a binary file. here is my code
but when i run the code, the program hangs and when i try to force quit it, the error window appears prompting me to either 'end now' or 'cancel'. i hav run into this kind of problem before, and it almost always has to with assigning wrong malloc - for instance, wrong type, wrong size etc. this time i cant pinpoint the error...can anyone help? thanks.Code:FILE *fp1, *fp2;
FILE *fp1_o, *fp2_o;
fp1 = fopen("C:\\x\\b1_maskedDN", "rb");
fp2 = fopen("C:\\x\\b2_maskedDN", "rb");
fp1_o = fopen("C:\\x\\b1_o", "wb");
fp2_o = fopen("C:\\x\\b2_o", "wb");
unsigned long int i, size = 1249820; //size = number of data points per file
unsigned char *b1_maskedDN;
b1_maskedDN = malloc(size * sizeof(unsigned char));
unsigned char *b2_maskedDN;
b2_maskedDN = malloc(size * sizeof(unsigned char));
float *b1_L;
b1_L = (size * sizeof(float)); //mem alloc for arrays to store output data of type float
float *b2_L;
b2_L = (size * sizeof(float));
fread(b1_maskedDN, sizeof(unsigned char), size, fp1); //read data into array
fread(b2_maskedDN, sizeof(unsigned char), size, fp2);
for (i=0; i<size; i++){
b1_L[i] = (float)b1_maskedDN[i] / 1.43415;
b2_L[i] = (float)b2_maskedDN[i] / 1.88031;
}
fwrite(b1_L, sizeof(float), size, fp1_o);
fwrite(b2_L, sizeof(float), size, fp2_o);
free(b1_maskedDN);
free(b2_maskedDN);
free(b1_L);
free(b2_L);
fclose(fp1);
fclose(fp2);
fclose(fp1_o);
fclose(fp2_o);
Where is the malloc for b1_L or b2_L? :)