Thread: Question about accessing record information from a file

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    44

    Question about accessing record information from a file

    So I am writing a program which stores the details of a movie in a file. I have about 25 records. I need to write a function which checks the sizeof Name[] in each record, and if it is 0 , it must printf "Empty" , else it must printf "Taken".

    This is the details structure of each record :

    Code:
    typedef struct Movies
    {
            
            char Name[50];
    	char Artist[25];
    	char Rating [10];
    	
            
    } Movies;

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You need to check the length of a string inside the array, not its size - it is not changeable.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    How can I access the length of string from the record in file please?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. Read file content using fopen() , fread() , fclose() functions from stdio.h
    2. Check string length with strlen() from string.h

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    When adding records to a file, the program is automatically saving records after each other , for example it starts at 0,1,2 etc. But can I for example store a record directly at position 40 for example ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Regardless of whether your movie name is "The Matrix" or "Gone With The Wind", each of your records will be the same size - namely sizeof(Movies)

    The only way to print "Empty" is to make sure that say .Name[0] is '\0' say (which is an easy thing to do and check anyway).

    An empty record still takes up sizeof(Movies) in the file.
    But the plus side is, you can update single records without having to rewrite the entire file.
    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. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  3. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  5. Adding record to a file
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-14-2002, 12:14 AM