Thread: Help: Updating Record In Random Access File

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    Spanish Town, Jamaica, Jamaica
    Posts
    33

    Red face Help: Updating Record In Random Access File

    I have been searching through the forums and found a couple snippets of code and from that i came up with this. What i want to do is search for the specific movie code and then update the movie status from inactive to active (for argument sake). Thanks for any help you can give.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct SYSTEM_MOVIE{
        int movie_code;
        int movie_dur;
        char movie_title[25];
        char movie_rating[5];
        char movie_dir[10];
        char movie_status[10];
        char movie_genre[10];
    
    }MOVIE;
    
    int main(){
    
    FILE *movie_fp;
        movie_fp = fopen("movies.dat", "r+b");
    
        int m_code;
        MOVIE movie_data;
    
        printf("*** Welcome to the movie updater! ***\n\n\n");
    
            if(movie_fp != NULL){
    
                printf("Please enter the code of the movie you wish to update: ");
                    scanf("%d", &m_code);
    
                        fseek(movie_fp, sizeof(movie_data)*(m_code - 1), SEEK_SET);
                            fread(&movie_data, sizeof(movie_data), 1, movie_fp);
                                fclose(movie_fp);
    
                    if (movie_data.movie_code != 0)
                    {
                        printf("%d", movie_data.movie_code);
                    }
    
                    else
    
                        printf("Record not found");
            }
    
            else{
    
                printf("File not found!!!");
            }
    
        }

  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
    First, make extra effort to keep your code looking presentable.
    It'll help you to organise your thoughts, and help us when you ask questions.
    Indent style - Wikipedia, the free encyclopedia

    All you're missing is another fseek() call followed by an fwrite()
    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. Specif Record Read error in random access files.
    By infantheartlyje in forum C++ Programming
    Replies: 0
    Last Post: 10-31-2011, 01:41 AM
  2. Help compiling random access file
    By newbc in forum C Programming
    Replies: 5
    Last Post: 04-08-2011, 12:54 PM
  3. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  4. updating and deleting a record
    By Burritt in forum C Programming
    Replies: 2
    Last Post: 04-19-2003, 04:58 PM
  5. random file access
    By bob20 in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2002, 10:28 PM

Tags for this Thread