[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help with fseek funtion



I am doing this input output program for an introductory comp sci course.
How do I get the file to identify a player's name (part of a structure
called "players" in a file and move back to that spot in the file and
write over that part of the file that is a match with the inputed data
from the screen?? I attached my program.  Please help ASAP!!!!!!!!!!

Thank you,
Keith Torrence

*  _____________________________________________________  *
  | Looty B says: "Losing feels alot worse than winning |
  |                feels good."                         |
  |                                                     |     
  |_____________________________________________________|
*                                                        *
                                                
/*
	Keith Torrence
	CISC 105-440
	Prog 3 Basic Update
	5-4-98
	
*/


#include <stdio.h>

FILE *point;

/* STRUCTURE with TWO VARIABLES (STATS and UPDATE)*/

struct players
{

char name[20];
int ab, runs, hits, rbi;

};




void compare (struct players, struct players);





/* MAIN FUNCTION */

main()

{


struct players update, stats;

point=fopen("CISC", "r+");
   if (point == NULL)
   {
   printf("The CISC file failed to open.\n");
   exit(1);
   }

compare (update, stats);


}

/* COMPARE FUNCTION */

void compare (struct players update, struct players stats)

{
long l;

int i, a, b, c, d, x;


printf("To begin updating baseball records in the CISC file press '3', to exit press '4'. Thanks.\n");
scanf("%d", &i);

	if(i==3)
        {
	
 	 do
          {
           printf("You will be updating another/a player's last name, at-bats, runs, hits, and RBI's; are you sure you want to continue? Press 1 to proceed, 2 to exit.\n");  
	   scanf("%d", &x);

  	   switch (x)
	   {
	   case 1:
         
           printf("Please enter the player's last name (Caps for first letter).\n"); 
           scanf("%s", update.name);
           printf("His number of at-bats.\n");
           scanf("%d", &update.ab);
           printf("His number of runs.\n");
           scanf("%d", &update.runs);
           printf("His number of hits.\n");
           scanf("%d", &update.hits);
           printf("His number of RBI's.\n");
           scanf("%d", &update.rbi);


           l= ftell(point);

           fscanf(point, "%-20s", stats.name);
           fscanf(point, "%d", &stats.ab);
           fscanf(point, "%d", &stats.runs);
           fscanf(point, "%d", &stats.hits);
           fscanf(point, "%d", &stats.rbi);
         
       
            if (strcmp(update.name, stats.name)==0)
            {
            fseek(point,-20l,*(stats.name));      /* Is this the proper
way to set up*/
                                     /* the fseek and ftell functions for our */
                                     /* purposes for this program? */
                        
            a=update.ab+stats.ab;
            b=update.runs+stats.runs;
            c=update.hits+stats.hits;
            d=update.rbi+stats.rbi;

	    fprintf(point, "%-20s %d %d %d %d", update.name, a, b, c, d); 
            printf("Here is what %s's new CISC record consists of.\n", update.name);
            printf("%d at-bats\n%d runs\n%d hits\n%d rbi's\n" , a, b, c, d);   
            }
          
            else 
           
            printf("The name you entered does not exist in the CISC file, sorry.\n"); 
                    
          
            break;
	
     
           case 2:
          
           printf("I guess you are leaving me, we were just starting to cook :-( \n");    

           break;

  	   default:
          
           printf("You must type a 1 or a 2 at that prompt. Thank-you.\n");
           
           break;
           }
         }
         while(x==1);
         
        } 
                

       else if(x!=3 || x!=4)
         {
         printf("You have entered an invalid integer!\n");
         }
     
      	 
       else 
         {       
       printf("I guess you don't want to update anymore.\n");
         }    
              
 

printf("                                                    \n");
printf("Thanks for being interested in Keith's programs.\n");

fclose(point);
}