Thread: Buffer Problems?

  1. #1
    Climber spoon_'s Avatar
    Join Date
    Jun 2002

    Buffer Problems?

    I dont understand why this is happening, but this code will only write 1000 RECORD_STRUCT's to a file when I want it to write 2000, or 10000, or 30000, or N+1000 (with N >= 1) records.


    fseek, fwrite, and ferror do not return errors.

    I know I dont need to seek, but I thought I would just slap that in there for the sake of debugging.

    Am I missing something here?
    Am I writing too fast?

    typedef struct
    	char name[32];
    	char phone[16];
    int main(int argc, char * argv[])
    	int i = 0;
    	FILE * outFile = NULL;
    	char lbuf[10];
    	int x;
    	memset(&rs, 0x0, sizeof(RECORD_STRUCT));
    	memset(lbuf, 0x0, 10);
    	outFile = fopen("c:\\testNrecords.rkw", "w+b");
    	if(outFile == NULL)
    		printf("null file!\n");
    		return 0;
    	for(; i < 2000; i++)
    		sprintf(lbuf, "record #%d", i);
    		strcpy(, lbuf);
    		memset(lbuf, 0x0, 10);
    		sprintf(lbuf, "phone #%d", i + 5);
    		strcpy(, lbuf);
    		fseek(outFile, i * sizeof(RECORD_STRUCT), SEEK_SET);
    		fwrite(&rs, sizeof(RECORD_STRUCT), 1, outFile);
    	return 0;

  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    You are going OOB with the lbuf, this could cause the error.

    sprintf(lbuf, "record #%d", i);

    "record #" is 8 + '\0' = 9

    if the index i is greater than 9 an error occurs.

    try just using the array vars in the sprintf.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print out a buffer
    By SwarfEye in forum C Programming
    Replies: 4
    Last Post: 09-08-2006, 09:32 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. Few problems with my program
    By kzar in forum C Programming
    Replies: 6
    Last Post: 06-22-2005, 07:58 AM
  4. Im having linking problems with turbo c++ ver 4.5
    By Marcos in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 04:57 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM