Buffer Problems?

This is a discussion on Buffer Problems? within the C Programming forums, part of the General Programming Boards category; I dont understand why this is happening, but this code will only write 1000 RECORD_STRUCT's to a file when I ...

  1. #1
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182

    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.

    WEIRD.

    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?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    typedef struct
    {
    	char name[32];
    	char phone[16];
    }RECORD_STRUCT;
    
    int main(int argc, char * argv[])
    {
    	int i = 0;
    	FILE * outFile = NULL;
    	RECORD_STRUCT rs;
    	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(rs.name, lbuf);
    
    		memset(lbuf, 0x0, 10);
    
    		sprintf(lbuf, "phone #%d", i + 5);
    		strcpy(rs.phone, lbuf);
    
    		fseek(outFile, i * sizeof(RECORD_STRUCT), SEEK_SET);
    		fwrite(&rs, sizeof(RECORD_STRUCT), 1, outFile);
    	}
    
    	fclose(outFile);
    
    	return 0;
    }

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

    ie
    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, 10:32 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 09:09 PM
  3. Few problems with my program
    By kzar in forum C Programming
    Replies: 6
    Last Post: 06-22-2005, 08: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, 05:57 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