opening an MP3 file for bit access

This is a discussion on opening an MP3 file for bit access within the C Programming forums, part of the General Programming Boards category; Hi all, I need to open a file of extension .mp3 in C. This file needs to give me access ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    16

    opening an MP3 file for bit access

    Hi all,

    I need to open a file of extension .mp3 in C. This file needs to give me access to it 8 bits at a time so i can send then down a physical connection. (part of an external system). However im not sure how to open a file in this way. I need advice on how to do the following:-

    open file.

    read 8 bits into variable (i.e. two hex characters)

    loop each time reading the next 8 bits.

    any advice would be greatly appreciated ive been trying to open the file in binary mode 'rb' and use 'fread' but have had no success.

    Thankyou all for your time

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    any advice would be greatly appreciated ive been trying to open the file in binary mode 'rb' and use 'fread' but have had no success.
    Post the code you tried. It should work if you do it properly.

    Binary mode is a good idea, especially if you're using Windows. Any sequences of CR+LF will be translated into a single newline character otherwise, which is not what you want.

    fread() could read "8 bits" if you read an unsigned char. Something like this.
    Code:
    unsigned char c;
    
    fread(&c, 1, 1, fp);
    You'll have to check for EOF and probably errors too, of course.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    8 bits is essentially one byte so just reading one byte would do the trick.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Elysia View Post
    8 bits is essentially one byte
    ... on most systems.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > ... on most systems.
    Use CHAR_BIT from limits.h if you're unsure

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    Hi all, thanks for the replies

    The code i have been trying is as a single test project below:-

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    	FILE *file_ptr;
    	unsigned char data;
    
    	int i=0;
    
    	file_ptr = fopen("C:\\Documents and Settings\\Gareth Devine\\Desktop\\alanis_morissette_clip.mp3","rb");
    	if (file_ptr !=NULL)
    	{
    		printf("mp3 file found\n");
    		
    		for (i=0;!feof(file_ptr);i++)
    			{
    				
    				fread(&data, 1, 1, file_ptr);
    				printf("%s	\n", data);
    				//would have funtion here passing the 8 bits to arm assembler code, hence the necessatity for 8 btis only!
    			}
    
    			
    		fclose(file_ptr);
    		
    	}
    	else
    		printf("file not found\n");
    	return 0;
    }
    whenever i run this it finds my file, and hence enters the first if statement printing the found file to screen. Then it raises an exception "Unhandled exception at 0x004040be in mp3test.exe: 0xC0000005: Access violation reading location 0x000000ff."

    Any ideas. i am using Visual C 2005 express edition if that is of any concern?

    Thakyou all for you time and knowledge

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Data is not a string, so you can't print it using &#37;s. And further, it's a bad idea to use feof as loop control. You could see the FAQ for more information.
    I think you can print it using %c or something, for char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    update, i ahev got the code working

    however my PC internal speaker has decided to join in the fun!!! and beeps each time it reads a value!? any ideas?

    Code:
    #include <stdio.h>
    
    
    
    int main()
    
    {
    
    	FILE *file_ptr;
    	unsigned char data;
    
    	int i=0;
    	int a;
    
    	file_ptr = fopen("C:\\Documents and Settings\\Gareth Devine\\Desktop\\alanis_morissette_clip.mp3","rb");
    	if (file_ptr !=NULL)
    	{
    		printf("settings file found\n");
    		
    		for (i=0;!feof(file_ptr);i++)
    			{
    				printf("%d \n",i);
    				fread(&data, 1, 1, file_ptr);
    				printf("%c	\n", data);
    				//would have funtion here passing the 8 bits to arm assembler code, hence the necessatity for 8 btis only!
    			}
    
    			scanf("%d",a);
    		fclose(file_ptr);
    		
    	}
    	else
    		printf("file not found\n");
    	return 0;
    
    
    
    }
    Cheers all

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Don't use feof as loop control.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    Elysia i have jst read the article you link to and now understand why u say this. I will chance my methods. thanks

    Any idea over the beeping though?

    cheers

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do not print bynary data as &#37;c - use %02X
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Yeah, the beeping is due to some special character you print using &#37;c, so use as vart mentions, %02X instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    thankyou all!!!! my problems are solved. well code realted ones anyhow lol.

    Your help is greatly appreciated

    Gaz

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The special character is character 7, I believe -- you can print it intentionally with \a.
    Code:
    printf("\a");
    Great fun . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

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