obtaining CD info

This is a discussion on obtaining CD info within the Linux Programming forums, part of the Platform Specific Boards category; hey guys, I need to get the number of tracks on a CD, my solution is below, but I am ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    obtaining CD info

    hey guys, I need to get the number of tracks on a CD, my solution is below, but I am not sure that it is the nicest way to do it, any comments?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <fcntl.h>
    #include <linux/cdrom.h>
                                                                                     
                                                                                     
    struct cdrom_tochdr header; /* struct for the header */
                                                                                     
    int main( int argc, char *argv[]){
                                                                                     
      int fh;  //CDROM file handle
      int firstTrack, lastTrack;
      int numberOfTracks;
      char *device = "/dev/cdrom";  //default device
                                                                                     
      /* check for argument in command line */
      if( argc > 1 ) //if argument, use it as the device
        device = argv[1];
                                                                                     
      fh = open(device, O_RDONLY); //open device
      /* error checking */
      if ( fh == -1 ) {
        fprintf( stderr,"%s: ", device );
        exit(1);
      }
                                                                                     
      /* read the header information */
      if(ioctl(fh, CDROMREADTOCHDR, &header) != 0) {
        fprintf(stderr, "Error in calling ioctl\n");     /* error handling */
        exit(0);
      }
    
      /* get number of tracks */
      firstTrack = header.cdth_trk0;
      lastTrack = header.cdth_trk1;
      numberOfTracks = lastTrack;    //last track equals total number of tracks
                                                                                     
      /* print the information */
      printf("There are %d tracks on this CD\n", numberOfTracks);
                                                                                     
      return 0;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    oh, and if anyone wants to compile this here is the Makefile:

    Code:
    CC=gcc
    
    numtrk: numtrk.o
            $(CC) -o numtrk numtrk.o
    
    numtrk.o:       numtrk.c
            $(CC) -c numtrk.c
    
    clean:  rm -f *.o *~ core* numtrk

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Looks fine to me, just reading it though. Why do you question whether it is good or not? It uses standard interfaces (ioctl), it should be fine.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well I'm asking because I was wondering if there is some other way of obtaining the tracks. Maybe CDs have the number of tracks set somewhere in their data - I'm just getting the last track and setting totaltracks equal to it.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    After flipping through the cdrom.h header, I found this function. You might want to look at it.
    Code:
    extern void cdrom_count_tracks(struct cdrom_device_info *cdi,tracktype* tracks);
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    thanks Chris, I will.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested for loop...search & display a list within a list
    By chadsxe in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2005, 01:34 PM
  2. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 02:12 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Nero CD recording take long time
    By Eagle16 in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-15-2002, 04:49 PM
  5. How to encrypt a CD perfectly?
    By Yin in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-13-2002, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21