Thread: Am i doing this right? (file reading)

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    55

    Am i doing this right? (file reading)

    so i set up the following structure in my document class header

    Code:
    struct header
    	{
    		 short NumBytesPerSample;
    		 bool BigorLittleEndian;
    		 float VoltsPerDigitizerUnit;
    	};
    then I declare the following in the document class header file

    Code:
    CFile Datafile;
    	header hdr;
    	header *phdr;

    This is the code i have to open the file, and to load the structure with the first 7 bytes of the opened file..

    is this correct?
    Code:
    phdr = &hdr;
    	Datafile.Read(phdr,7);
    I know that the first 7 bytes will be a short (2 bytes) a bool (1 byte) and a float (4 bytes)

    I'm not sure if this is correct, cause my professor doesn't really teach us anything. he just leaves us guessing... i've never done windows programming till now.. it's always been console based.. and fairly simple too..

    Also, how can i make my own file to test this?

    Thanks,

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In theory, it should be 7 bytes. In pratice, it may be more, because of alignment. Most compilers (on 32-bit processors) enjoy aligning variables on 4-byte boundaries, because it makes address computation easier. A good way to check if it's doing this is to output sizeof(header).

    Also, in your reading code, you can avoid using the extra variable phdr by just using Datafile.Read(&hdr,sizeof(hdr)). You'll notice that I changed the 7 to sizeof(hdr). This is so that if you ever change the internals of the header struct, you won't need to search thru the code to find every place that you typed in 7 and replace it with the new size.

    As for making your own file, what are you using for your file reading (i.e. which class)?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    wouldn't putting "pragma once" on the top fix that?

    anyway i like what you suggested though.. i think i'm gonna go with that.. as for reading the files.. i'm not sure what you question is.. i'm just gonna use .read, and get a crap load of values out.

    The data is supposed to be in binary/hex

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Heh. I missed the definition of ReadFile. If you wanted to a struct to a file, you'd use the exact same command as you would to read, except you'd call the Write() function instead. Same parameters and everything.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM