fwrite file size

This is a discussion on fwrite file size within the C Programming forums, part of the General Programming Boards category; When writting a file using fwrite I get a file size bigger then when I wrote the same file using ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    fwrite file size

    When writting a file using fwrite I get a file size bigger then when I wrote the same file using fopen. How come? I thought fwrite packed the data something resulting in a smaller file size.

    If I have a structure like so:
    struct{
    char string[1000];
    };

    How does fwrite save that to disk?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: fwrite file size

    >>... file size bigger then when I wrote the same file using fopen.
    fopen() doesn't write to the file, it only opens it. Are you confusing this with another function?

    >> thought fwrite packed the data something resulting in a smaller file size.
    No, it writes the number of bytes you tell it to (with adjustments for newline characters on text mode files).

    >>How does fwrite save that to disk?
    Post some of your code.
    Normally, to write the struct, you'd use:

    >>fwrite (&mystruct, sizeof(mystruct), 1, fp)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Yes, I'm confusing fopen with the function fputs. With the smaller file I produced by writting to it in text mode while the larger file created by fwrite was opened in binary mode.

    I know this probabely result in posting useless code, but maybe it will help with the comparsion.

    Code:
    old code smaller filesize:
    struct dataBank {
    char question[200];
    char answer[200];
    };
    struct dataBank list[50];
    void svDatabank(char filename[25])
    {  // Saves entries to disk
      int ctr;
      fPtr = fopen(filename, "w+");
    	if(!fPtr)
    	{ printf("A problem occurred while trying to open the file.\n");
    	  pressKey();
    	  return;
    	}
    		fprintf(fPtr,"%d",numList);
    	for(ctr = 0;ctr <= numList; ctr++)
    	{
    		fputs(list[ctr].question, fPtr);
    		fputs(list[ctr].answer, fPtr);
    	}
    	fclose(fPtr);
    	printf("File successfully saved.");
    	pressKey();
    	return;
    }
    Code:
    new code bigger file size:
    struct dataBank {
    int num;
    char question[1000];
    char answer[1000];
    };
    
    struct node{
       struct dataBank data;
       struct node* next;
    };
    
    int saveDatabank(struct node* headRef, char filename[25])
    {  // Saves entries to disk
       struct node* current = headRef;
       FILE *fPtr;
       fPtr = fopen(filename, "wb");
    	if(!fPtr)
    	{ printf("A problem occurred while trying to open the file.\n");
    	  pressKey();
    	  return 1;
    	}
    
       while(current != NULL)
       {
          fwrite(& current->data,sizeof(struct dataBank), 1, fPtr);
          current = current->next;
       }
    
    	fclose(fPtr);
    	printf("File successfully saved.");
    	pressKey();
    	return 0;
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    ----edited out----- i posted something before i finished reading. Answered my own questions
    The keyboard is the standard device used to cause computer errors!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When you do this:
    >>fputs(list[ctr].question, fPtr);
    You are writing the question string to the file, which maybe something like:
    "Whats your fav color?"
    Now, thats a only a few bytes long, and it will only write upto the end of the string, not the end of the array (stops at nul terminator).

    When you do this:
    >>fwrite(& current->data,sizeof(struct dataBank), 1, fPtr);
    You are writing the complete struct to the file, which includes two char arrays, each with 1000 bytes. There is no concept of strings in this case.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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