Delete & print func()

This is a discussion on Delete & print func() within the C Programming forums, part of the General Programming Boards category; The program I am writting is attached. The problem I am having is inside my delete func() and also my ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Delete & print func()


    The program I am writting is attached. The problem I am having is inside my delete func() and also my print func().

    In the delete func() the user enters a code to delete. This code is saved in separate structures inside 'client.dat'. After the code is entered, the 'client.dat' file should be read until it finds the matching code. When it does an outside func() called tempfile() is called to delete. In this function, what I am trying to do, is create a file named 'temp.dat' and read all the contents of 'client.dat' (with the exception of the selected record) into that file. Then I want to re-write the contents of 'temp.dat' back into 'client.dat', thus deleting the client record.

    In the print func() I have no real idea what the hell I'm doing???

    Can someone help this hopeless idiot out of a jam?

    P.S. Explainations for the delete func() are not really what I need. I've already had people trying to talk me through but no go. Samples I could adapt or if I'm only missing something minor if you could let me know what the code should be and where the hell to put it, I would be eternally grateful.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Here is the code, I'm an idiot.
    Attached Files Attached Files

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Have a look at this version, it should do what you want.
    Code:
    int delete_record(void)
    {
    	FILE *dta_file, *temp_file;
    	struct fclient another_client;
    	int client_code;
    	
    	/*
    	 * Read the users input (client code)
    	 */
    		
    	if (scanf("%d",&client_code) != 1)
    	{
    		printf ("That was not a number!\n");
    		return (1);
    	}
    	
    	/*
    	 * Open the two files 
    	 */
    	if((dta_file=fopen(FILENAME,"rb")) == NULL)
    	{
    		perror(FILENAME);
    		return(1);
    	}
    	
    	if((temp_file=fopen(FILENAME2,"wb")) == NULL)
    	{
    		perror(FILENAME);
    		return(1);
    	}
    	
    	/*
    	 * Read the IN file, comparing each structs client_code with
    	 * that which the user entered.  If match found, we don't write
    	 * out, if no match we do write out.
    	 */
    	while((fread(&another_client,sizeof(struct fclient),1,dta_file)) == 1)
    	{
    		if(client_code == another_client.client_code)
    		{
    			printf("\n\nThe client has been deleted from the inventory...");
    		}
    		else
    		{
    			if((fwrite(&another_client,sizeof(struct fclient),1,temp_file)) != 1)
    			{
    				perror("write");
    				fclose(dta_file);
    				fclose(temp_file);
    				return(1);
    			}
    		}
    	}
    	
    	/*
    	 * Close the two files
    	 */
    	fclose(dta_file);
    	fclose(temp_file);
    	
    	/*
    	 * Remove the existing file, and rename the temporary one in it's place
    	 */
    	remove(FILENAME);
    	if (rename(FILENAME2, FILENAME) != 0) perror("rename");
    	
    	key_wait();
    	return (0);
    }
    I shouldn't really be writing a complete function for you, but I've been running out of ways to explain things
    Notice the functions return type has changed, there was no point in returning a char.

    Read through this code, and make sure you understand it properly before trying to use it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Thanks once again for your help. I am sure it's sort of 'against the rules' to do that for me (I can understand) but I appreciate it none the less.
    I understand what the code is doing but there is a problem. If you remove 'FILENAME' and then rename 'FILENAME2' to 'FILENAME' is it correct to say that you'd have to create 'temp.dat' everytime the delete func() is called? I implemented the code but it doesn't seem to create the 'temp.dat' file. Am I creating the file in the wrong area? Or am I scanning the user input wrong? The function doesn't work?

    New code is attached.
    Attached Files Attached Files

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Without looking at your code.... I'll say the function I provided was complete. It asked the user for their input, created the temporary file, read in the client.dat file, wrote out to temp.dat, then deleted client.dat and renamed temp.dat to client.dat.

    The only thing you need to do is call it (I think!).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Sorry Hammer, maybe I wasn't fully awake when I put it together. I was using scanf right before the if(scanf...... silly mistake. Thanks agian. Peace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. C Delete opened file
    By gidion in forum C Programming
    Replies: 25
    Last Post: 12-04-2008, 10:35 AM
  3. How to delete part of an array?
    By Ash1981 in forum C Programming
    Replies: 11
    Last Post: 01-01-2006, 06:06 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM
  5. Delete Item
    By emilyh in forum Windows Programming
    Replies: 10
    Last Post: 10-03-2001, 10:33 PM

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