buffer type for fread & fwrite

This is a discussion on buffer type for fread & fwrite within the C Programming forums, part of the General Programming Boards category; So far, I've used stdio functions with standard buffers, namely char arrays. But in doing a b-tree project, the sample ...

  1. #1
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42

    buffer type for fread & fwrite

    So far, I've used stdio functions with standard buffers, namely char arrays. But in doing a b-tree project, the sample

    provided used fileio.h. Since I was not familiar with that & don't know where to get, I converted I/O statements to the stdio

    equivalents. The sample had code to read in data from file to a btree page structure. I used the stdio fread to do the

    equivalent. I know that fread() uses a void type buffer, but is it best to use std type buffers like char? Here's what I

    mean:

    the converted code goes like this:

    Code:
    typedef struct {
        short keycount; // # keys in page
        char  key[MAXKEYS][7]; // the actual keys, a key is 6 chars long
    	short kyrrn[MAXKEYS]; // ptrs to rrns of the key data records
        short child[MAXKEYS+1];  // ptrs to rrns of descendants
    } BTPAGE;
    
    #define PAGESIZE  sizeof(BTPAGE)
    
    int btread(short rrn, BTPAGE *page_ptr)
    {
    	long addr;
    
    	addr = (long)rrn * (long)PAGESIZE + 2L;
    	fseek(btfd, addr, 0);
    	return ( fread(page_ptr, PAGESIZE, 1, btfd) );
    }
    Notice that a btree page structure is passed to fread as the buffer to read in pagesize bytes. My question is does this

    method work as I intended? I'm not sure when testing the code & I've never done btrees file-based, only done memory-based.
    I mean does using the code like above write a btree page structure to file in orderly fashion in order of keycount 1st, then

    keys, then rrns, last child ptr rrns & read it back to memory as a btree page structure, storing the values correctly, in the

    right place?

    Or am I better off converting the btree page structure contents into characters to be dumped in a char buffer equivalent to

    size of btree page & then do read/writes with that to file. using this method I think I can better visualize & analyze the

    content & structure in file & memory.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Reading or representing on disk is really irrelevent if you're loading it into memory to manipulate it anyway.

    You create a tree when you read it, right? So save it to disk in any order you prefer, read it also in whatever order you prefer, and then assemble your tree.

    Natrually if you know it's going to be X size and only X size, you would get some speed gains by having a fixed read/write so it assembles your tree quicker.

    But that aside, if this isn't something that's hugely disk/memory intensive, speed is likely not a major concern.

    In which case, you just use whatever's easier to implement.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> I know that fread() uses a void type buffer, but is it best to use std type buffers like char?<<
    fread() doesn't use a "void buffer", it simply takes a void pointer as it parameter denoting where to write to. The sector of memory it's writing to could be anything, to fread(), it's just a sequence of bytes.

    >>does using the code like above write a btree page structure to file in orderly fashion in order of keycount 1st<<
    Well, it's fread(), so it'll read not write.

    With regards to fwrite: The contents of the structure will be written out exactly as it is stored in memory.

    To help you understand, try this simple program:
    Code:
    #include<stdio.h>
    
    typedef struct
    {
      int i;
      int j;
    } FOO;
    
    int main(void)
    {
      FILE *fp;
      FOO foo;
    
      foo.i = 11;
      foo.j = 22;
    
      if ((fp = fopen("file.bin","w+b")) == NULL)
      {
        perror("file.bin");
        return 0;
      }
    
      fwrite (&foo, sizeof(foo), 1, fp);
      rewind(fp);
      foo.i = 0; foo.j = 0;
      fread (&foo, sizeof(foo), 1, fp);
      printf ("i: %d\nj: %d\n", foo.i, foo.j);
    
      fclose(fp);
    
      return 0;
    }
    It simply writes a structure to a file, then reads it back in.

    >> am I better off converting the btree page structure contents into characters<<
    This will depend on your requirements. Writing a structure to a file is quick and easy, but the file itself will not be portable to other platforms. So if you want to share data between Unix and Windows, I'd suggest outputing in a character based format.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    ok, thanks for the input.

    Hammer,

    then would the following file output be correct, if btree has 2 keys & 2 kyrrn, null key is "@", null child & key rrn is "-1", & key used is single char rather than 6 char long (I wasn't able to implement as string key w/o a segmentation fault)

    file contents: (using key/rrn pair of t,5 & g,8)
    t@@@5gt@@85c

    hexdump: (just for reference)
    0000 : AA AA 00 01 74 40 40 40 00 35 FF FF FF FF FF FF ....t@@@.5......
    0010 : FF FF FF FF FF FF FF FF FF FF 00 02 67 74 40 40 ............gt@@
    0020 : 00 38 00 35 FF FF FF FF FF FF FF FF FF FF FF FF .8.5............
    0030 : FF FF ..

    I'm new at this so I'm not sure if the output is correct. would like verification for assurance.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's a bit difficult to tell. The easiest way to verify that you've written it out correctly, is to simply read it back in again, and use printf() to print the contents, just like I did in my sample prog.

    For reference, here's a hex dump of the file containing the struct in my program:

    Code:
    00000000h: 0B 00 00 00 16 00 00 00                         ; ........
    You can see that there are 8 bytes that make up the 2 int variables within the structure. The first 4 bytes, 0b000000, represent 11, and the remaining: 22.

    From a portability point, there are (obvious) problems. For example, a structure could contain padding between fields. Also, my system uses 4 bytes for an int, but your's may use a different number (2 or 8 for example).

    >>(I wasn't able to implement as string key w/o a segmentation fault)>>
    Then you were doing it wrong Show some more code if you want help with it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    ok, here's how I tried to print out the btree. tell me if the code needs rework:

    Code:
    void print_btree(short root){
    	BTPAGE page;
    	int i;
    
    	btread(root,&page);
    	
    	printf("#key: %d -> ",page.keycount);
    
    	//print keys of page
    	for(i = 0; i < MAXKEYS; i++)
    		printf("%c ",page.key[i]);
    	printf("-> ");
    
    
    	for(i = 0; i < MAXKEYS; i++)
    		printf("%d ",page.kyrrn[i]);
    	printf("-> ");
    
    	//print child ptrs of page
    	for(i = 0; i <= MAXKEYS; i++)
    		printf("%d ",page.child[i]);
    	printf("\n");
    
    	/*not testing levels below for now
    	//print next level of btree
    	for(i = 0; i <= MAXKEYS; i++){
    		//print_page(page.child[i], 0);
    		if(page.child[i] != NIL)
    			print_btree(page.child[i]);
    	}
    	*/
    
    	return;
    
    }
    in the btree, I insert key "f", kyrrn "1".

    resulting file:
    f@@@1

    but printing with code above gives this:
    #key: 0 -> -> 0 0 0 -66 -> -796 -66 -788 1 52

    that doesn't seem quite right. suppose I need to look at the rest of the other btree code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Segmentation Fault?
    By John_L in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 09:37 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 04:33 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 10:28 AM

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