Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Troubleshooting with fread and fopen (http://www.programmingforums.org/showthread.php?t=15382)

grba878 Mar 10th, 2008 5:30 PM

Troubleshooting with fread and fopen
 
:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<alloc.h>
  4. #include<dos.h>
  5. #include<string.h>
  6. #include<share.h>
  7.  
  8. struct wavdata {unsigned int soundlength, frequency;
  9.                 char  *sample;
  10.       };
  11.  
  12.  
  13.  
  14.       struct RIFF
  15.       {
  16.  
  17.       long wave;
  18.       long fmt;
  19.       unsigned int BitRes;
  20.       long data[40];
  21.       long datasize[40];
  22.       long channels;
  23.       long samplerate;
  24.       }  header;
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.       char fp[25];
  32.       char data1[100];
  33.  
  34.  
  35.  
  36.  
  37.       fp =fopen("cwave1.wav", "rb");
  38.       if(fp ==NULL)
  39.       {
  40.       printf("unable to open to wave file\n");
  41.       return(0);
  42.       }
  43.  
  44.           fread( data1,sizeof data1,1,fp);
  45.         }
  46.  
  47.           if(header.RIFF != 0x46464952)
  48.           {
  49.             printf("%x\n");
  50.             printf(" RIFF format\n");
  51.           }
  52.  
  53.           if(header.wave == 45564157)
  54.                   {
  55.             printf("No wavfmt format\n");
  56.  
  57.  
  58.             }
  59.           if(header.fmt !=2074666)
  60.           {
  61.  
  62.             printf("No wavfmt format\n");
  63.                 }
  64.  
  65.  
  66.           //check channels
  67.           if(header.channels !=1)
  68.           {
  69.           printf("Not a mono wave file\n");
  70.           return(0);
  71.           }
  72.  
  73.           //check bit resolution
  74.           if(header.BitRes !=8)
  75.           {
  76.           printf("Not an 8-bit wave file\n");
  77.           return(0);
  78.           }
  79.           // Verify the sample size
  80.           if(header.samplerate!= 48000)
  81.           {
  82.           printf("%x\n");
  83.           printf("Sample rate is not 48KHZ\n");
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. int main()
  97. {
  98.  
  99.       clrscr();
  100.       printf("Play a wave file\n");
  101.       printf("Please input filename + extension\n");
  102.       gets(fp);
  103.  
  104.  
  105.  
  106.       return(0);


Ancient Dragon Mar 10th, 2008 7:13 PM

Re: Troubleshooting with fread and fopen
 
Nice code, and thanks for using code tags correctly, but why ????? Sorry, but we are not mind readers, you need to give just a tad more information about the problem is you expect anyone to help you.

The Dark Mar 10th, 2008 10:11 PM

Re: Troubleshooting with fread and fopen
 
Apart from the missing braces and the fact that your code isn't actually inside a function, I think the main problem is that you are reading the data into the data1 variable and then trying to access it through the header variable.

grba878 Mar 12th, 2008 1:49 AM

Re: Troubleshooting with fread and fopen
 
@ancient dragon
yeah sry bout htat...actually m tryin to verify the sample rate for a 48 kHZ .wav file ,so m tryin to call a hex file saved in my pc and tryin to read the format for RIFF , fmt chunk and the sample rate chunk !!

grba878 Mar 12th, 2008 2:00 AM

Re: Troubleshooting with fread and fopen
 
@the dark
thnksssss !!
well after making the necessary changes the errors that get are
" type name expected "
"Variable 'fp' is initalized more than once"

The Dark Mar 12th, 2008 2:12 AM

Re: Troubleshooting with fread and fopen
 
It sounds a bit like you have made almost all the necessary changes :)

Can you post your current code, along with the line numbers for the errors?

grba878 Mar 12th, 2008 6:36 PM

Re: Troubleshooting with fread and fopen
 
Now i am able to run the code but m unable to open my wave file,are my 'fopen' and 'fread' commands proper !!




:

#include<stdio.h>

#include<conio.h>
#include<alloc.h>
#include<dos.h>
#include<string.h>
#include<share.h>

struct wavedata {unsigned int soundlength, frequency;
                char  *sample;
      };



      struct HeaderType
      {
      long RIFF;
      long wave;
      long fmt;
      unsigned int BitRes;
      long data[40];
      long datasize[40];
      long channels;
      long samplerate;
      }  Header;

      struct wavedata voice;
      char wavfile[25];
      char data[100];

      void playback (struct wavedata *wavfile)
      {

      }



        int LOADVOICE(struct wavedata *voice,char *FILENAME)
        {
        FILE *wavfile;



      wavfile =fopen("cwav1.wav", "rb");
      wavfile =fopen("cwav2.wav", "rb");
      wavfile =fopen("cwav3.wav", "rb");
      if(wavfile ==NULL)
      {
      printf("unable to open wave file\n");
      return(0);
      }

          fread( &Header,sizeof(Header),3,wavfile);


          if(Header.RIFF != 0x46464952)
          {

            printf(" RIFF format\n");
            return(0);
          }

          if(Header.wave ==0x45564157)
                  {
            printf("No wavfmt format\n");
              return(0);

            }
          if(Header.fmt !=0x20746D66)
          {

            printf("No wavfmt format\n");
            return (0);
                }


          //check channels
          if(Header.channels !=1)
          {
          printf("Not a mono wave file\n");
          return(0);
          }

          //check bit resolution
          if(Header.BitRes !=8)
          {
          printf("Not an 8-bit wave file\n");
                return(0);
          }
          // Verify the sample size
          if(Header.samplerate!= 48000)
          {
          printf("%x\n");
          printf("Sample rate is not 48KHZ\n");
            return(0);
          }
          return(0);
    }

int main()
{

      clrscr();
      printf("Play a wave file\n");
      printf("Please input filename + extension\n");
      gets(wavfile);
        if (LOADVOICE( &voice,wavfile))
        {
          playback( &voice);



      getch();
      return(1);
      }
    getch();
  }


titaniumdecoy Mar 12th, 2008 6:43 PM

Re: Troubleshooting with fread and fopen
 
You should return 0 when the program exits successfully and 1 otherwise, not the other way around.

Benoit Mar 12th, 2008 6:50 PM

Re: Troubleshooting with fread and fopen
 
:

      FILE *wavfile;

      wavfile =fopen("cwav1.wav", "rb");
      wavfile =fopen("cwav2.wav", "rb");
      wavfile =fopen("cwav3.wav", "rb");


You are re-assigning wavfile twice after you open the first file. When you do this, the pointer to the other files gets lost.

grba878 Mar 12th, 2008 6:56 PM

Re: Troubleshooting with fread and fopen
 
{
FILE *wavfile;
FILE *wavfile1;
FILE *wavfile2;



wavfile =fopen("cwav1.wav", "rb");
wavfile1 =fopen("cwav2.wav", "rb");
wavfile1 =fopen("cwav3.wav", "rb");
if(wavfile ==NULL)|| if(wavfile1== NULL)|| if(wavfile ==NULL)

----- it gives me 1 error at if statement sayin expression syntax error------


All times are GMT -5. The time now is 4:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC