Qbasicnews.com

Full Version: nathan et al...help with standard c++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Sorry for asking this here, but I'm stuck, and you guys seem to know a lot. I've tried getting help on comp.lang.c++ newsgroup, but haven't had much luck.

What I'm trying to do is test some ideas for hash functions I've been toying with. I'm having problem with file I/O.

What I want to to is:
1) open a file
2) determine it's length so that I determine the number of pad bytes I'll need
3) dump the file to memory and pad as needed.
4) access the padded file in memory as an array of unsigned long.

My problems are 1--I don't know how to find the size of the file.
if I do:

fstream in("myfile.dat");
cout << sizeof(in);

the reported size is different than the size on disk. (I think white spaces are stripped in the fstream).

if I load the file into a char array, then I can't access the array as an array of unsigned int.

I'm thinking that I need to do something like:
ifstream in("myfile.dat");
stringstream inmem;
inmem << in.rdbuf();
//pad inmem
//send inmem to properly sized array using inmem.read()

However, when I compare:
sizeof(in)
and
sizeof(inmem)
I get 2 different values...neither of which match the size on disk of myfile.dat!!!


Any suggestions?? thanks.
well, sizeof(in) would be the size of an integer pointer not how big the file is. I'm not sure what would tell you the size but that's why sizeof(in) wont work.
Well, I dislike/don't know C++. I prefer the way C works. But before: your sizeof is not working 'cause sizeof is giving you the size of the variable itself, not the file. I'd work like this:

Code:
#include <stdio.h>

int main(void)
{
   FILE *pf;   /* file pointer */
   long l;

   if( (pf = fopen("filename.dat", rb) ) == NULL)
   {
      fprintf(stderror, "Error opening file for reading\n");
      exit(-1);
   }

   l = lof(pf);

   ...

   fclose(pf);
}

/* I am providing this function 'cause file length calculators are not
   ANSI nor POSIX and vary from compiler to compiler. This one is
   portable code. */

long lof(FILE *pf)
{
   long curpos;
   long l=0;

   curpos = ftell(pf);
   fseek(pf, 0, SEEK_SET);   /* locate at the beginning of the file */
  
   while not feof(pf)
   {
      l++;
      fgetc(pf);
   }

   fseek(pf, curpos, SEEK_SET); /* restore */
   return l;
}

That way you get the length of the file in "l". I'm not quite sure what you want to do (I don't understand what is "padding", sorry Sad ).

As for the array, you have to create it as an unsigned int array:

Code:
usigned int myarray[length_of_array];

and you read from file using fread:

Code:
fread (myarray, sizeof(unsigned int), length_of_array, pf);

That would read length_of_array records of (unsigned int) size from file pf and would it store in the memory location pointed by myarray.
hmmm....

i believe eof works only for text files, where eof is char 26. binaries are not likely to be terminated by eof. moreover, to get the file size you need to access the file system -- which is naturally not-system-independent. but each os has its own libs for that.






[Flexibal>
fopen, fclose and feof work in the same way on both binary and text files. Standard C simply recognizes them as streams.

An example implementation of feof looks like (from mlibc after preprocessing);
Code:
int
(feof)(FILE *stream)
{
        return ((( stream )->_flags & 0x010 ) != 0) ;
}

Text files would pressumably set that flag when the EOF character is encountered, Im not entirely sure what causes a binary file to set the flag.
I suppose that the OS carries somehow a comparison between the file pointer and the file length. This is done platform-wise, that's why I provided my own lof code.
hrrm

i'm more of a --

#include <io.h>

int fh = _open(...);

-- type of guy. i love rare meat. dont need no wrapper classes to take my freedom away.




[Flexibal>