browna3
March 21st, 2002, 12:48 PM
I have been working on this same problem for weeks and it is really starting to get to me. I have a file that is written using the "wt" attribute in a seperate program:
f = fopen(fileName,"wt");
fwrite(&no,sizeof(int),1,f);
for(i=0;i<no;i++){
for(j=0;j<size;j++){fwrite(&btm[i*size+j],sizeof(double),1,f);}
for(i=0;i<size;i++){fwrite(&n[i],sizeof(double),1,f);}
}
fclose(f);
If I compile the program below using Metroworks Codewarrior and the "r" or "rt" option for the read the program works great and I get correct read results. However, if I use MVC++ 6 and the "r" or "rt" option I get an end of file in rdstate_bin() immediately, but the initial read in main is fine. In addition if I use the "rb" attribute in either Codewarrior or MVC I get results, but they are incorrect results. I would just use Codewarrior, but I have to use MVC because I am making a CFX tag and MVC is the only compiler that has a wizard. If you know of anywhere I can find more information about the fread problem please let me know or even if you know how to create a CFX tag in Codewarrior. Any help at all will be wonderful. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void rdstate_bin(FILE *f,int n, double *neweigvectors, int imRows, int imCols, double *mean)
{
int i, j, numread;
for(i=0;i<n;i++)
{
for(j=0;j<(imCols*imRows);j++)
{
numread = fread(&neweigvectors[(i*imRows*imCols)+j],sizeof(double),1,f); <!-- read that gives the errors...
printf("Number of items read = %d\n", numread); // debug
if(ferror(f) != 0)
printf("File error.");
if(feof(f) != 0)
printf("End of file.");
printf("Contents of buffer = %d\n", neweigvectors[i*imRows*imCols+j]); // debug
}
}
for(i=0;i<(imCols*imRows);i++)
{
numread = fread(&mean[i],sizeof(double),1,f);
printf("Number of items read = %d\n", numread); // debug
if(ferror(f) != 0)
printf("File error.");
if(feof(f) != 0)
printf("End of file.");
}
}
int main(void)
{
FILE *f = fopen ("test.dat", "r");// <-- here is where i change
int numread, n;
int imRows = 150;
int imCols = 130;
double *MEANVAL,*neweigvectors;
numread = fread(&n,sizeof(int),1,f); // <-- initial read
printf("Number of items read = %d\n", numread); // debug
printf("Contents of buffer = %d\n", n); // debug
MEANVAL = (double*)malloc(imRows*imCols*sizeof(double));
assert(MEANVAL);
neweigvectors = (double*)malloc(imRows*imCols*n*sizeof(double));
assert(neweigvectors);
rdstate_bin(f,n,neweigvectors,imRows,imCols,MEANVAL);
fclose(f);
return 0;
}
Thanks!!!
Ames
f = fopen(fileName,"wt");
fwrite(&no,sizeof(int),1,f);
for(i=0;i<no;i++){
for(j=0;j<size;j++){fwrite(&btm[i*size+j],sizeof(double),1,f);}
for(i=0;i<size;i++){fwrite(&n[i],sizeof(double),1,f);}
}
fclose(f);
If I compile the program below using Metroworks Codewarrior and the "r" or "rt" option for the read the program works great and I get correct read results. However, if I use MVC++ 6 and the "r" or "rt" option I get an end of file in rdstate_bin() immediately, but the initial read in main is fine. In addition if I use the "rb" attribute in either Codewarrior or MVC I get results, but they are incorrect results. I would just use Codewarrior, but I have to use MVC because I am making a CFX tag and MVC is the only compiler that has a wizard. If you know of anywhere I can find more information about the fread problem please let me know or even if you know how to create a CFX tag in Codewarrior. Any help at all will be wonderful. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void rdstate_bin(FILE *f,int n, double *neweigvectors, int imRows, int imCols, double *mean)
{
int i, j, numread;
for(i=0;i<n;i++)
{
for(j=0;j<(imCols*imRows);j++)
{
numread = fread(&neweigvectors[(i*imRows*imCols)+j],sizeof(double),1,f); <!-- read that gives the errors...
printf("Number of items read = %d\n", numread); // debug
if(ferror(f) != 0)
printf("File error.");
if(feof(f) != 0)
printf("End of file.");
printf("Contents of buffer = %d\n", neweigvectors[i*imRows*imCols+j]); // debug
}
}
for(i=0;i<(imCols*imRows);i++)
{
numread = fread(&mean[i],sizeof(double),1,f);
printf("Number of items read = %d\n", numread); // debug
if(ferror(f) != 0)
printf("File error.");
if(feof(f) != 0)
printf("End of file.");
}
}
int main(void)
{
FILE *f = fopen ("test.dat", "r");// <-- here is where i change
int numread, n;
int imRows = 150;
int imCols = 130;
double *MEANVAL,*neweigvectors;
numread = fread(&n,sizeof(int),1,f); // <-- initial read
printf("Number of items read = %d\n", numread); // debug
printf("Contents of buffer = %d\n", n); // debug
MEANVAL = (double*)malloc(imRows*imCols*sizeof(double));
assert(MEANVAL);
neweigvectors = (double*)malloc(imRows*imCols*n*sizeof(double));
assert(neweigvectors);
rdstate_bin(f,n,neweigvectors,imRows,imCols,MEANVAL);
fclose(f);
return 0;
}
Thanks!!!
Ames