Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9315479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:22:35+00:00 2026-06-19T02:22:35+00:00

I need to read the header variables from a wave file and display what

  • 0

I need to read the header variables from a wave file and display what they are. I am using the following code, but my output has numbers far too large. I’ve searched for solutions for hours. Help would be much appreciated! Thanks. I got the wave soundfile format from https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Output:
Wav file header information:
Filesize 3884 bytes
RIFF header RIFF
WAVE header WAVE
Subchunk1ID fmt
Chunk Size (based on bits used) 604962816
Subchunk1Size 268435456
Sampling Rate 288030720
Bits Per Sample 2048
AudioFormat 256
Number of channels 2048
Byte Rate 288030720
Subchunk2ID
Subchunk2Size 1684108385

Here is the source:

#include <stdio.h>
#include <stdlib.h>

typedef struct  WAV_HEADER
{
char                RIFF[4];        
int                 ChunkSize;     
char                WAVE[4];       
char                fmt[4];        
int                 Subchunk1Size;                              
short int           AudioFormat;  
short int           NumOfChan;      
int                 SamplesPerSec;  
int                 bytesPerSec;    
short int           blockAlign;    
short int           bitsPerSample;  
int                 Subchunk2Size; 
char                Subchunk2ID[4];
}wav_hdr; 

int getFileSize(FILE *inFile); 
int main(int argc,char *argv[])
{
//check startup conditions
if(argc >= 2); //we have enough arguments -- continue
else { printf("\nUSAGE: program requires a filename as an argument -- please try again\n"); exit(0);}

wav_hdr wavHeader;
FILE *wavFile;
int headerSize = sizeof(wav_hdr),filelength = 0;
wavFile = fopen(argv[1],"r");
if(wavFile == NULL)
{
    printf("Unable to open wave file\n");
    exit(EXIT_FAILURE);
}
fread(&wavHeader,headerSize,1,wavFile);
filelength = getFileSize(wavFile);
fclose(wavFile);
printf("\nWav file header information:\n");
printf("Filesize\t\t\t%d bytes\n",filelength);
printf("RIFF header\t\t\t%c%c%c%c\n",wavHeader.RIFF[0],wavHeader.RIFF[1],wavHeader.RIFF[2],wavHeader.RIFF[3]);
printf("WAVE     header\t\t\t%c%c%c%c\n",wavHeader.WAVE[0],wavHeader.WAVE[1],wavHeader.WAVE[2],wavHeader.WAVE[3]);
     printf("Subchunk1ID\t\t\t%c%c%c%c\n",wavHeader.fmt[0],wavHeader.fmt[1],wavHeader.fmt[2],wavHeader.fmt[3]);
printf("Chunk Size (based on bits used)\t%d\n",wavHeader.ChunkSize);
printf("Subchunk1Size\t\t\t%d\n",wavHeader.Subchunk1Size);
printf("Sampling Rate\t\t\t%d\n",wavHeader.SamplesPerSec); //Sampling frequency of the wav file
printf("Bits Per Sample\t\t\t%d\n",wavHeader.bitsPerSample); //Number of bits used per sample
printf("AudioFormat\t\t\t%d\n",wavHeader.AudioFormat);
printf("Number of channels\t\t%d\n",wavHeader.bitsPerSample);     //Number of channels (mono=1/sterio=2)
printf("Byte Rate\t\t\t%d\n",wavHeader.bytesPerSec);   //Number of bytes per second
printf("Subchunk2ID\t\t\t%c%c%c%c\n",wavHeader.Subchunk2ID[0],wavHeader.Subchunk2ID[1],wavHeader.Subchunk2ID[2],wavHeader.Subchunk2ID[3]);
printf("Subchunk2Size\t\t\t%d\n",wavHeader.Subchunk2Size);
printf("\n");
return 0;
}

int getFileSize(FILE *inFile)
{
int fileSize = 0;
fseek(inFile,0,SEEK_END);
fileSize=ftell(inFile);
fseek(inFile,0,SEEK_SET);
return fileSize;
}`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:22:36+00:00Added an answer on June 19, 2026 at 2:22 am

    So, your code basically works — if you compile it with the same compiler and O/S that the author of the file format spec was using (32-bit Windows). You’re hoping that your compiler has laid out your struct exactly as you need to match the file bytes. For example, I can compile and run it on win32 and read a WAV file perfectly — right up to the variable part of the header whose variability you failed to code for.

    Having written a great deal of code to manipulate a variety of file formats, I would advise you give up on trying to read into structs and instead make a few simple utility functions for things like “read next 4 bytes and turn them into an int”.

    Notice things like the “extra format bytes”. Parts of the file format depend on the values in previous parts of the file format. That’s why you generally need to think of it as a dynamic reading process rather than one big read to grab the headers. It’s not hard to keep the result highly portable C that will work between operating systems without relying on O/S specific things like stat() or adding library dependencies for things like htonl() — should portability (even portability to a different compiler or even just different compiler options on the same O/S) be desirable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read in an expression from a file using a string stream
I need to read CSV file header from FTP. As these files can be
I need to read 16 bits from the binary file as std::string or char
I need to read certain statistics from iw_statistics structure, here's the code: struct net_device
I need to read data from XML to a List<>. The XML file contains
I need to read the size of a file but the server is forcing
I need to read read values from below xml and am using saxparser for
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I need to read a file line by line, and change a variable accordingly.
I need to read the editurl from one jqgrid for use in a DND

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.