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

  • Home
  • SEARCH
  • 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 3952974
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:55:07+00:00 2026-05-20T01:55:07+00:00

The following function is not working i.e. it exits: fread(buf, 1, 4, stdin); buf[4]

  • 0

The following function is not working i.e. it exits:

fread(buf, 1, 4, stdin);
buf[4] = '\0';

if (strcmp((char*)buf, "data")) exit(EXIT_FAILURE);

I think if I can manually push fread farther down the stream it will eventually hit “data”.

In other words how do I increment fread so that it skips bytes.

Examples of code always appreciated.

Thanks!


EDIT 1

Basically I’m parsing the header of a wav file on the iPhone. It is giving me some trouble and I believe it has to do with the way apple formats its audio files. Someone recommended to me running through the stream until I get “data” and then moving forward from there.

I hope this clarify things.


EDIT 2

Here is documentation as to how the wav file header should look like, but I’m wondering if the way apple formats theirs makes this inaccurate.

You will notice that ‘data’ is offset by 36 which is a multiple of four.

  • 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-05-20T01:55:08+00:00Added an answer on May 20, 2026 at 1:55 am

    This works in a stream like fashion and does what I think you want:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char** argv)
    {
        long pos = 0;
        char buf;
        char str[5] = {'\0','\0','\0','\0','\0'};
    
        while ( fread(&buf, sizeof(char), 1, stdin) > 0 )
        {
            pos++;
            str[0] = str[1]; 
            str[1] = str[2];
            str[2] = str[3];
    
            str[3] = buf;
            str[4] = '\0';
    
            /* uncomment to see what got read ** printf("Read %s\n", str); */
    
            if ( strcmp(str, "data") == 0 )
            {
                break;
            }
        }
    
    
        printf("\"data\" occured after %ld bytes\n", pos);
        return 0;
    }
    

    This works by using a buffer I’ve called str and rotating the positions around in it. It will work until data appears.

    Be aware it reads binary data, not text. So anything on stdin gets read, newlines included. However, if you adapt that to a file handle that shouldn’t be a problem.

    You can probably include this. The problem with using fread is that by design:

    The file position indicator for the stream (if defined) shall be advanced by the number of bytes successfully read.

    Therefore if you advance by 4 bytes at a time, unless your data is exactly a multiple of 4 from the start of the data, you’re going to miss it. For example:

    123DATA
    

    Fails if you read 4 bytes at a time.

    Now, given this is a documented file format, are there not some header specs somewhere that tell you exactly how wide the fields are on the header? Or at least where they vary, such that you can read them off appropriately? Reading until data works, but isn’t elegant, really.

    Or, better still, I’m sure there must be a library for doing this somewhere.


    Edit In response to the header of the wave file, since it is fixed and not that large, read the whole thing into a buffer.

    uint8_t* hdr = malloc(36*sizeof(uint8_t));
    fread(hdr, sizeof(uint8_t), 36);
    

    Don’t forget to free. At this point, you have the entire header extracted. I’ve used uint8_t to definitely be 8 bits. At this stage you can pull some interesting tricks, like casting that data to a struct. Just be aware of the endianness of the fields.

    From then on, the stream is available to you in chunks, I believe. The first thing you need to do is this:

    uint8_t chkid;
    uint8_t chksz;
    fread(&chkid, sizeof(uint8_t), 4, stream);
    fread(&chksz, sizeof(uint8_t), 4, stream);
    

    That’ll grab you the data of that particular chunk. Assuming you’re using a little endian system, You ought to be able to use chksz directly as an integer at this point, so now you can do:

    uint8_t dataframe = malloc(chksz * sizeof(uint8_t));
    

    Into which you can read the data:

    fread(&dataframe, sizeof(uint8_t), chksz, stream);
    

    This is of course assuming that the Apple wave format is the one described. Now, from that page:

    The WAVE file format is a subset of Microsoft’s RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single “WAVE” chunk which consists of two sub-chunks — a “fmt ” chunk specifying the data format and a “data” chunk containing the actual sample data. Call this form the “Canonical form”. Who knows how it really all works.

    I’ve given you instructions that if used in a continual loop until there’s nothing more on the stream, will allow you to read any number of data chunks a-la RIFF. You then need to process the data you get appropriately to break it down; i.e. split your data chunk that you read in up appropriately. If this is the only format you expect to read, you could just ignore additional chunks.

    Now, the problem remains, what is the apple format and to be honest I’ve no idea!

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

Sidebar

Related Questions

Why does n not equal to 8 in the following function? void foo(char cvalue[8])
I have the following JavaScript code: Link In which the function makewindows does not
The following code in javascript gives me the error this.callback is not a function
I have the following function that is pulling data from a database. The ajax
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
I have written the following function in a bash script but it is not
I have the following function in access, which was working fairly well. But now
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
Take the following function: DataTable go() { return someTableAdapter.getSomeData(); } When I set a
So: I have the following function, adapted from a formula found online, which takes

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.