Thread: Help, File I/O problem

  1. #1
    DirtyHarry
    Guest

    Post Help, File I/O problem

    Hi, writing data in a structure to file. That seems to be going fine. When I read it back though the last record is getting duplicated, so I get a double output of it to screen an it also doubles in calculations. Any suggestions? Below are the two functions I am using.
    Code:
    void add_customer()
    {
    	struct customer new_cust={0,"",0,0};
    	
    	logo();
    	
    	if((cfPtr=fopen("cust_file.txt","r+"))==NULL)
    	{
    		printf("File could not be opened.\n");
    	}
    	else
    	{
    		printf("\n\nNew Customer Account                Number:\t%d\n\n", cust_counter);
    		Sleep(1500);
    		
    		fseek(cfPtr,(cust_counter-1)*sizeof(struct customer),SEEK_SET);
    
    		fread(&new_cust, sizeof(struct customer),1,cfPtr);
    
    		if(new_cust.acct_num!=0)		//Validation to avoid account overwrite
    		{
    			printf("\nAccount %d already contains information.\n",new_cust.acct_num);
    			stall();
    		}
    		else
    		{
    		logo();
    		printf("\n\nEnter Customer Name\n\n?");
    		fscanf(stdin,"%s",new_cust.cust_name);
    
    		new_cust.acct_num=cust_counter;
    
    		fseek(cfPtr,(new_cust.acct_num-1)*sizeof(struct customer), SEEK_SET);
    			
    		fwrite(&new_cust, sizeof(struct customer),1,cfPtr);
    		fclose(cfPtr);
    		cls();
    		}
    	}
    
    }
    
    void disp_cust_record()
    {
    	if((cfPtr=fopen("cust_file.txt","r"))==NULL)
    	{
    		printf("File could not be opened.\n");
    	}
    	else
    	{
    		logo();			//Prints a logo
    		printf("\n\n%-6s%-16s%-11s%15s\n", "Acct", "Customer Name", "B_Code Start", "B_Code End");
    
    		while(!feof(cfPtr))
    		{
    			fread(&new_cust,sizeof(struct customer),1,cfPtr);
    			//////////////////////////////////////////>>>>>>>>>>>><<<<<<<<<<<<
    			if(new_cust.acct_num!=0)
    			{
    				printf("%-6d%-16s%-10d%10d\n", new_cust.acct_num, 
    				new_cust.cust_name, new_cust.barcode_start, new_cust.barcode_end);
    			}
    		}	
    		fclose(cfPtr);			
    		stall();		//Generates a pause
    	}
    }
    I'd really appreciate help here, new to File I/O and battling this all week.
    Thanks,

    H

    Code tags added by Hammer.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [Deja vu?]

    >Any suggestions?
    Maybe while(!feof(cfPtr)).
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  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