-1

I'm want to fill hdr (struct) variable with data from in.wav file and I want to copy the first 64 bytes of in.wav file to another file (out.wav).

But! When using fread() the second time it starst to copy in.wav from the place where it finished when using fread() the first time. Why?

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

typedef struct FMT
{
    char        SubChunk1ID[4];
    int         SubChunk1Size;
    short int   AudioFormat;
    short int   NumChannels;
    int         SampleRate;
    int         ByteRate;
    short int   BlockAlign;
    short int   BitsPerSample;
} fmt;

typedef struct DATA
{
    char        Subchunk2ID[4];
    int         Subchunk2Size;
    int         Data[441000]; // 10 secs of garbage. he-he)
} data;

typedef struct HEADER
{
    char        ChunkID[4];
    int         ChunkSize;
    char        Format[4];
    fmt         S1;
    data        S2;
} header;



int main()
{
    FILE *input = fopen("in.wav", "rb");
    FILE *output = fopen("out.wav", "wb");

    unsigned char buf[64];
    header hdr;

    if(input == NULL)
    {
        printf("Unable to open wave file\n");
        exit(EXIT_FAILURE);
    }

    fread(&hdr, sizeof(char), 64, input);


    fread(&buf, sizeof(char), 64, input);
    fwrite(&buf, sizeof(char), 64, output);


    printf("\n>>> %4.4s", hdr.ChunkID);

    fclose(input);
    fclose(output);

    return 0;
}

What is the matter?

yulian
  • 1,601
  • 3
  • 21
  • 49
  • 2
    why would you NOT want to read sequentially from the file ? Why would you want to read the same data over and over again ? – Sander De Dycker May 06 '13 at 14:15
  • 1
    `fread` starts reading from the current file position and advances it the number of bytes read. – Daniel Fischer May 06 '13 at 14:15
  • @SanderDeDycker The first for some actions (to manipulate) and the second for verifying **out.wav** data (after I rewrite it). – yulian May 06 '13 at 14:17
  • @Julian: And the third? How were you expecting to read the next bit of data from the file? – cHao May 06 '13 at 14:18
  • @cHao Are you talking about `fwrite(&buf..)` after `fread(&buf..)`? – yulian May 06 '13 at 14:21
  • @DanielFischer I've been looking for specifications about `fread()` and didn't find something like: "fread starts reading from the current file position and advances it the number of bytes read"... Thank you for telling me this information. – yulian May 06 '13 at 14:24
  • 1
    @Julian: I'm talking about another `fread`. In typical use, you *want* the position to automatically update. Consider what would happen if that file were 16 GB in size. You couldn't read the whole thing at once, and you're not going to want to have to `fseek()` each and every time, either. (Usually, one only remotely even *cares* about the current file position, let alone keeps track of it themselves.) Yours is a semi-special case, so you get the extra work. :P – cHao May 06 '13 at 14:34
  • 2
    C effectively treats files as a *stream* of characters. (You'll see that word used a whole lot where standard I/O is concerned.) – cHao May 06 '13 at 14:42
  • @Julian 7.21.8.1 (2): "The fread function reads, into the array pointed to by `ptr`, up to `nmemb` elements whose size is specified by `size`, from the stream pointed to by `stream`. For each object, `size` calls are made to the `fgetc` function and the results stored, in the order read, in an array of `unsigned char` exactly overlaying the object. **The file position indicator for the stream (if defined) is advanced by the number of characters successfully read.** [...]" – Daniel Fischer May 06 '13 at 14:48

2 Answers2

5

This is intended. fread always reads from the file's current read pointer and advances that same pointer, so you can a file in sequential chunks without having to explicitly seek.

You shouldn't have to read the same chunk twice in a row. What you're checking this way is whether some other process has changed the file in the meantime, and if one has, then your program will erroneously report that the copy failed.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

The file pointer is moved. Read more about it here: Does fread move the file pointer? . You can use fseek or rewind to position at the beginning of a file.

Community
  • 1
  • 1
Cristina_eGold
  • 1,463
  • 2
  • 22
  • 41