.wav file reading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joby mathai
    New Member
    • Sep 2006
    • 3

    .wav file reading

    i was able to open the .wav file using c program, but while reading,its not reading the full file.i opened the .wav file in visual cpp and compared it with the program output.so many data missing.its reading only less than 1/4th of the file.no error is shown by the program.so how can i read a wav file correctly in the hex format.my program is given below.
    [code]
    #include<stdio. h>
    #include<conio. h>

    void main()

    {
    int i;
    long n;
    FILE *f1;
    clrscr();
    printf("the data output is \n\n");
    f1=fopen("silen ce.wav","r");
    while(feof(f1)= =0)
    {
    i=getw(f1);
    if(ferror(f1)!= 0)
    {
    printf("\n an error has occurred"); just to check if any error occured.
    n=ftell(f1);
    printf("\n\nthe value of n is %ld",n);
    getch();
    }
    printf("%x ",i);
    }
    n=ftell(f1);
    printf("\n\nthe value of n is %ld",n);
    fclose(f1);
    printf("\n\n end of the file");
    getch();
    }
    [code]
  • Ratheesh Kumar
    New Member
    • Sep 2006
    • 4

    #2
    hai Joby,
    Your problem is simple.Here you are trying to open the file in read mode using the "r" in fopen().This mode is generally used for text files .the .wav is not text file.So that you should open it in binary read mode using "rb" or "rb+" instead of "r" in fopen()

    Comment

    • pukur123
      New Member
      • Sep 2006
      • 61

      #3
      I will suggest you to go through the open,read,write ,close functions in c.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I do not think that getw is an ANSI function and is certainly non-portable since it reads an integer which may differ from system to system in both size and endianess.

        An improved method would be to use fread to read in the number of bytes and then to reconstruct the integer from that

        Code:
        unsigned char byte[4];
        
        if (fread(byte, sizeof byte, 1, f) == sizeof byte)
        {
            int i = byte[0] | (byte[1]<<8) | (byte[2]<<16) | (byte[3]<<24);
        }

        Comment

        • Ratheesh Kumar
          New Member
          • Sep 2006
          • 4

          #5
          Hai joby,
          you can do this by opening it in binary mode ,the code may be as follows
          #incude<io.h>
          //add additional headder files
          void main()
          {
          int han,c;
          char file[20],buf[20];
          gets(file);
          han=open(file,O _RDONLY|O_BINAR Y);
          while(1)
          {
          c=read(han,&buf ,10);
          if(c==0)
          {
          break();
          }
          //manipulate the characters upto buf[c-1]
          }
          }

          Comment

          Working...