Thread: Search a file for specif data and update it (random access)

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

    Question Search a file for specif data and update it (random access)

    I really need the help. This is what i'm working on. The file name is "movie.dat" and currently i was able to save data into the file in this order:

    [movie_code] [movie_dur] [movie_title] [movie_rating] [movie_dir] [movie_genre] [movie_status]

    [12345] [120] [Movie] [PG13] [Director] [Comedy] [Active]

    I want to search for the "movie_code" and change the [movie_status] from Active to Inactive.

    So lets say for example i have a movie code 12345 saved in my movie file. I want to change the value from "Active" to "Inactive"for argument sake.

    This is the code i was trying to do it with:
    Code:
    FILE *movie_fp;
    movie_fp = fopen("movie.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);
    
    
    if(movie_data.movie_code != 0){
    
    movie_data.movie_code = "Inactive";
    
    }
    else {
    
    
    printf("Record not found");
    }
    
    
    
    
    }
    
    else{
    
    printf("Something went wrong, file was not found!");
    
    
    }
    fclose(movie_fp);

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Hi,

    Can you let us know , what error you are getting ?.

    Did you try to debug the issue on your own before posting here?.

    Did you seen complete usage of fseek ?.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should post your MOVIE structure.

    So we can make sense of this.
    if(movie_data.movie_code != 0)
    movie_data.movie_code = "Inactive";
    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.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by teensicle View Post
    Code:
    …
    
    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);
    
    …
    I suggest to check the return value of scanf. If scanf can not find an integer, then m_code has an undefined value.
    After that, check the scaned value that it is greater then zero or you seek to an negativ position.
    You should also check the filesize of "movie.dat" to know how many entries are there in (see command stat and fstat).
    Have the database 24 entries and you enter 42, then close the file if it is open and drop an error.

    Next time, please post comlete code so we can see the definition of function main and (as Salem says) the definition of the struct MOVIE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A random file access question
    By Shadow20 in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2011, 11:30 AM
  2. Specif Record Read error in random access files.
    By infantheartlyje in forum C++ Programming
    Replies: 0
    Last Post: 10-31-2011, 01:41 AM
  3. reading then displaying some data from a random access file
    By kool_hamster in forum C Programming
    Replies: 5
    Last Post: 01-21-2010, 10:56 AM
  4. random file access
    By bob20 in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2002, 10:28 PM
  5. question on random file access
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2002, 05:21 PM