File won't write.....

This is a discussion on File won't write..... within the C Programming forums, part of the General Programming Boards category; I wanted this module to modify data on a file, but it doesn't seem to write into the file at ...

  1. #1
    Registered User Diamonddrago's Avatar
    Join Date
    Nov 2009
    Posts
    6

    File won't write.....

    I wanted this module to modify data on a file, but it doesn't seem to write into the file at all...pls help....

    Code:
    void ModEmp(void)
    {       clrscr();
    
    	int Target,EmpID,Found=0;
    	char check,EmpName[40],DOA[9],DOP[9],DOB[9],Mstat;
    	FILE *mem,*pay,*temp;
    
    	temp=fopen("E:\\Proj\\Temp.dat","w");
    	if((mem=fopen("E:\\Proj\\Member.dat","r"))==NULL)
    	{	printf("File is Empty!");
    		sleep(2);
    	}
    
    	else
    	{	printf("Please Enter Employee ID to Modify	:	");
    		fflush(stdin);
    		scanf("%i",&Target);
    
    		while (!feof(mem))
    		{	fscanf(mem,"%i %[^/]%*c %[^/]%*c %[^/]%*c %[^/]%*c %c",&EmpID,EmpName,DOA,DOP,DOB,&Mstat);
    			if(feof(mem))
    			{	break;
    			}
     //				if(Target!=EmpID)
    					fprintf(temp,"%i %s/ %s/ %s/ %s/ %c\n",EmpID,EmpName,DOA,DOP,DOB,Mstat);
    				    //	Found=0;
    
    	   //		else
    				Found=1;
    				printf("\nEmployee ID			:	%04i\n",EmpID);
    				printf("\nEmployee Name			:	%s\n",EmpName);
    				printf("\nDate of Appoinment		:	%s\n",DOA);
    				printf("\nDate of Probation Period	:	%s\n",DOP);
    				printf("\nDate of Birth			:	%s\n",DOB);
    				printf("\nMaritial Status		:	%c\n",Mstat);
    	   //		}
    
    		}
    
    		if(Found==0)
    		{	printf("Record not Found\n");
    		}
    
    		else if(Found==1)
    		{	printf("\nNew Employee Name		:	");
    			scanf("%s",EmpName);
    			printf("\nNew Date of Appointment	:	");
    			scanf("%s",DOA);
    			gets(DOA);
    			printf("\nNew Date of Probation Period	:	");
    			scanf("%s",DOP);
    			printf("\nNew Date of Birth		:	");
    			scanf("%s",DOB);
    			printf("\nNew Marital Status		:	");
    			scanf("%c",&Mstat);
    
    			fscanf(temp,"%i %[^/]%*c %[^/]%*c %[^/]%*c %[^/]%*c %c",&EmpID,EmpName,DOA,DOP,DOB,&Mstat);
    		}
    			fflush(stdin);
    			fclose(mem);
    			fclose(temp);
    			remove("E:\\Proj\\Member.dat");
    			rename("E:\\Proj\\Temp.dat","E:\\Proj\\Member.dat");
    			printf("\nRecords Edited");
    			sleep(2);
    
    	}
    	Employeemgt();
    
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    #1.
    Code:
    fflush(stdin);
    fflush is undefined for input streams.

    #2.
    Code:
    while (!feof(mem))
    {
        fscanf(mem,"%i %[^/]%*c %[^/]%*c %[^/]%*c %[^/]%*c %c", &EmpID, EmpName, DOA, DOP, DOB, &Mstat);
        if(feof(mem))
        {
            break;
        }
    You manage to get around the problems using feof to control a loop by using a second test for feof after the read. Usually the better solution is to test the read operation itself within the conditional part of the while loop to see how many values were read and seeing if this matches your expectations:
    Code:
    while (fscanf(mem,"%i %[^/]%*c %[^/]%*c %[^/]%*c %[^/]%*c %c",&EmpID,EmpName,DOA,DOP,DOB,&Mstat) == 6)
    {
    #3. What's with the "//"? Are those meant to be there because it looks like some of them should not. You also have a place where it looks like you'd want to put in a bracket "{" (after an else perhaps?).

    #4.
    Code:
    else if(Found==1)
    {
    
        ...
    
        fscanf(temp,"%i %[^/]%*c %[^/]%*c %[^/]%*c %[^/]%*c %c",&EmpID,EmpName,DOA,DOP,DOB,&Mstat);
    }
    temp is open for writing. This is the place where you need to be using fprintf to write to temp, not fscanf trying to read from temp.

    #5.
    Code:
    printf("\nNew Date of Appointment	:	");
    scanf("%s",DOA);
    gets(DOA);
    Huh? First off, there are better choices than gets. Second, why are you reading into DOA twice?

    May be other issues... I don't know.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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