Thread: writing in a specific position

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    72

    writing in a specific position

    i'm trying to delete an account in my file but the problem is that i can reach my position depending on my index but i cann'ty write in this record

    Code:
    void account_delete(char *filename)
    {
         
         
         
         int pos; 
         account temp;
         FILE *file=fopen(filename,"rb+");
         if(file!=NULL)
         {
                         
                              printf("enter the index to modify");
                              scanf("%d",&pos);          
                             
                         fseek(file, (pos -1 ) *sizeof(account), SEEK_SET );
                         fread(&temp, sizeof(account), 1,file);
                         printf("%s\n",temp.name);                    
                         strcpy(temp.name,"");strcpy(temp.password,"");strcpy(temp.previleges,"");
                         printf("%s\n",temp.name);
                         fwrite(&temp,sizeof(account),1,file); 
         }
                      
              viewall(filename);
              fclose(file);                                          
         }
    please some suggestions if possible

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why do you read?
    Just seek to the position, and write a record which means "deleted".

    As it stands, your code needs another fseek().

    Also, what does viewall do? Does it try to open the file as well before you've closed this one?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    72
    even if i remove the read there is no change .also in other functions i nedd to read the record to modify and then write it back.
    the viewall function display all the content off the file

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK, so viewall does open the file then.

    Perhaps close the file here, THEN call viewall?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    72
    sorry the delete operation works but those where i need to modify the record still doesn't write the modified code here is a function wich modify the password of an account record

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    72
    Code:
    void change_password(char *filename)
    {
         
         
         
        int pos; 
         char pwd[20];
        
         account temp;
         
         FILE *file=fopen(filename,"rb+");
         if(file!=NULL)
           {
                              printf("enter the index to modify");
                              scanf("%d",&pos);                     
                                  
                             
                              fseek(file, (pos -1 ) *sizeof(account), SEEK_SET );
                              fread(&temp, sizeof(account), 1,file);
                              printf("enter the new password ");
                              scanf("%d",&pwd);                     
                              strcpy(temp.password,pwd);
                             fwrite(&temp,sizeof(account),1,file); 
         }
         
     
     fclose(file);
     viewall(filename);                     
         
           
           
            }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    See post #2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D starfields regardless of ship position
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 10-17-2006, 11:49 PM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM