reading a file of this type

This is a discussion on reading a file of this type within the C Programming forums, part of the General Programming Boards category; hey people, im having a really big problem with this, ive tried alsorts but im not very good at C ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Post reading a file of this type

    hey people, im having a really big problem with this, ive tried alsorts but im not very good at C

    I have a file that was writen in this format, i can open it with notpad but its a bit unreadable!

    here is how the file is made up!



    First, 8 bytes of header:

    Code:
    struct TrackRecordHeader
    
    {
    
    	unsigned int	version;			// version == 0x0000000a
    
    	unsigned int	trackRecordCount;		// Number of track records stored in file
    
    };
    Then all the track records follow in succession. Here's how one track record looks:

    Code:
    struct TrackRecordEntry
    
    {
    
    	unsigned int	playerNameLength;		// Number of bytes in player name
    
    	wchar		playerName[??];			// Name of player, 2 bytes per character (wide-character format)
    
    	unsigned int	carNameLength;			// Number of bytes in car name
    
    	char		carName[??];			// Name of car, 1 byte per character
    
    	FILETIME	dateTime;			// Date & time when record was made, see <windows/wtypes.h>
    
    	float		time;				// Record time, measured in seconds
    
    };
    The FILETIME structure is defined in the standard Windows SDK. A look in MSDN yields the following information:



    The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.

    Code:
    	typedef struct _FILETIME { // ft 
    
    	    DWORD dwLowDateTime; 
    
    	    DWORD dwHighDateTime; 
    
    	} FILETIME;
    ... so you should be able to manipulate the FILETIME structure with standard Win32 API calls.







    Then all the checkpoint records follow, as a linear stream of floats:

    Code:
    	unsigned int	numberOfCheckpointTimes;		// Number of checkpoint times that follow
    
    	float		checkpointTimes[??];			// All the checkpoint times, measured in seconds

    could anybody help on how i would open this file and view it in a normal text or ascii!

    Thanks guys!

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    fopen() the file in binary mode
    fread() the structures
    printf() the data in the structure
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    thanks for the info, but im having problems!!!

    c:\program files\miracle c\test\test.c: line 8: Parse Error, expecting `']'' or `NUMBER'
    'float checkpointTimes [??]'

    Dunno what that is about!!!
    and wchar is not recognised

    im sorry if i sound stupid but im learning as you read this!

    o and how do i fread structs?

    Sorry

    please help

    Stu

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    First step is get a better compiler than that miracle toy
    Try www.compilers.net, and get say bloodshed dev-c++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    woooooooo, still getting errors witht he [??] bit
    and the wchar bit!

    This seems so hard todo but i really wanna crack it!

    plese anybody can you help as much as posible!

    thanks
    stu

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The wchar might be fixed with a wchar_t and #include <wchar.h>
    Code:
    struct TrackRecordEntry
    {
       unsigned int  playerNameLength;
       wchar_t       playerName[??];
       unsigned int  carNameLength;
       char          carName[??];
       FILETIME      dateTime;
       float         time;
    };
    As the error you encountered indicates, you need to either specify as having an unknown size ([]), or specify a size (using a number). You cannot declare an array like this.
    Code:
    char carName[??];
    I believe that it is implied that the length member preceding the array member is the length of the subsequent array. But the value contained in the file will not be known to the compiler at compile time. You might change the arrays to pointers and dynamically allocate memory to them given their lengths.
    Code:
    struct TrackRecordEntry
    {
       unsigned int  playerNameLength;
       wchar_t      *playerName;
       unsigned int  carNameLength;
       char         *carName;
       FILETIME      dateTime;
       float         time;
    };
    At some point, it will benefit you greatly to post your own code that you are developing (pruned reasonably to demonstrate any issues), not just some structure definitions. Attaching a sample file might also be beneficial after compilation is successful.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    thanks for the info mate, really appreciate it!

    Here is what ive come up with so far!

    Sorted out getting the file to open in Binary mode!
    the code that wsa suggested for [??] worked fine!

    but im still having problems with the FILETIME bit
    Parse error before DWORD and FILETIME here is my code, lol i know its not doing much or anything but like i said before im learning!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <wchar.h>
    
    typedef struct _FILETIME { // ft 
    	    DWORD dwLowDateTime; 
    	    DWORD dwHighDateTime; 
    	} FILETIME;
    
    unsigned int	numberOfCheckpointTimes;		// Number of checkpoint times that follow
    float		*checkpointTimes;			// All the checkpoint times, measured in seconds
    
    struct TrackRecordHeader
    {
    	unsigned int	version;			// version == 0x0000000a
    	unsigned int	trackRecordCount;		// Number of track records stored in file
    };
    
    struct TrackRecordEntry
    {
       unsigned int  playerNameLength;
       wchar_t      *playerName;
       unsigned int  carNameLength;
       char         *carName;
       FILETIME      dateTime;
       float         time;
    };
    
    int main() {
      FILE *file; /* declare a FILE pointer */
    
      file = fopen("1.rec", "rb"); 
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Error: can't open file.\n");
        /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
        return 1;
      }
      else {
        printf("File opened. Now closing it...\n");
    	fclose(file);
        return 0;
      }
    }
    Now i need to work out how to fread the strucs! lol im so poo at this! :P

    Thanks guys!

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    right sorted the FILETIME bit of this, hehe making progress!!!!

    Ive posted my 8.c file for you to look at, i ran a compile with no errors so im happy so far!

    now all i need is to fread the structs! and fprint them :P

    thanks for the help so far guys! been relly good! please help more, i need this cracked! :P
    Attached Files Attached Files
    • File Type: c test.c (1,001 Bytes, 89 views)

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >now all i need is to fread the structs!

    Well, there is a little more to it. As I mentioned, the dynamic allocation will be required before you can read the arrays into memory. And by attaching a file, I meant the file you intend to read.

    To give you something to chew on, here is a rather ugly example. I use foo() to create my own file in place of the one you are trying to read but didn't attach. The bar() function is where I read it back. This will be similar to the code you are developing.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void foo(const char *filename)
    {
       FILE *file = fopen(filename, "wb");
       if(file)
       {
          unsigned int value = 2;
          char buffer[BUFSIZ] = "Dave_Sinkula";
    
          fwrite(&value, sizeof(value), 1, file);
    
          value = strlen(buffer);
          fwrite(&value, sizeof(value), 1, file);
          fwrite(buffer, sizeof(*buffer), value, file);
    
          strcpy(buffer, "cooliced");
          value = strlen(buffer);
          fwrite(&value, sizeof(value), 1, file);
          fwrite(buffer, sizeof(*buffer), value, file);
    
          fclose(file);
       }
    }
    
    struct sRead
    {
       unsigned int  names;
       struct
       {
          unsigned int  length;
          char         *array;
       } *name;
    };
    
    void bar(const char *filename)
    {
       FILE *file = fopen(filename, "rb");
       if(file)
       {
          struct sRead info;
          fread(&info.names, sizeof info.names, 1, file);
          printf("info.names = %u\n", info.names);
          info.name = malloc(info.names * sizeof(*info.name));
          if(info.name)
          {
             unsigned int u;
             for(u = 0; u < info.names; ++u)
             {
                fread(&info.name[u].length, sizeof info.name[0].length, 1, file);
                printf("info.name[%u].length = %d\n", u, info.name[u].length);
                info.name[u].array = malloc((info.name[u].length * sizeof(*info.name[0].array)) + 1);
                if(info.name[u].array)
                {
                   fread(info.name[u].array, info.name[u].length, sizeof(*info.name[0].array), file);
                   info.name[u].array[info.name[u].length] = '\0';
                   printf("info.name[%u].array = \"%s\"\n", u, info.name[u].array);
                }
             }
          }
          fclose(file);
       }
    }
    
    int main(void)
    {
       const char filename[] = "file.txt";
       foo(filename);
       bar(filename);
       return(0);
    }
    
    /* my output
    info.names = 2
    info.name[0].length = 12
    info.name[0].array = "Dave_Sinkula"
    info.name[1].length = 8
    info.name[1].array = "cooliced"
    */
    And note that I'm sloppy and didn't free the memory that was dynamically allocated, nor did I check return values for success of fread and fwrite.
    Last edited by Dave_Sinkula; 10-28-2003 at 05:05 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    hey thanks dave for the code and the advice, sorry i didnt post the file i want to view but here it is!

    and dont worry about your sloppy coding! its helping me alot!

    thanks again!
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    hey guys, still not got much further, im a little stuck on using the fread() with strucs, ive been over you example a million times but i cant seem to grasp it, is there anyway witch you could comment on what bits do what!

    thanks guys again!

    Stu

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by cooliced
    hey guys, still not got much further, im a little stuck on using the fread() with strucs, ive been over you example a million times but i cant seem to grasp it, is there anyway witch you could comment on what bits do what!

    thanks guys again!

    Stu
    Code:
    #include <stdio.h>
    
    struct 
    {
        char name[10];  // Person's Name
        char addr[10];  // Person's Address
    } person;           // Define a person
    
    int main ()
    {
        FILE *st;
        
        st = fopen("adr.txt", "r");             // open the file
        fread(&person, sizeof(person), 1, st);  // Read the first record
              // &person        address of the structure
              // 1              number of person records to read
              // sizeof(person) size of one record
              // st             input stream
    
        printf(" [%s] [%s]\n", person.name, person.addr);
        fclose(st);
    
        return 0;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need help reading in specific type of file
    By Natase in forum C Programming
    Replies: 4
    Last Post: 09-12-2001, 08:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21