Using the read function to read in a file.
Posted
by robUK
on Stack Overflow
See other posts from Stack Overflow
or by robUK
Published on 2010-04-28T10:58:10Z
Indexed on
2010/04/28
11:03 UTC
Read the original article
Hit count: 178
c
Hello,
gcc 4.4.1
I am using the read function to read in a wave file. However, when it gets to the read function. Execution seems to stop and freezes. I am wondering if I am doing anything wrong with this.
The file size test-short.wave is: 514K.
What I am aiming for is to read the file into the memory buffer chunks at a time. Currently I just testing this.
Many thanks for any suggestions,
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char buff = malloc(10240);
int32_t fd = 0;
int32_t bytes_read = 0;
char *filename = "test-short.wav";
/* open wave file */
if((fd = (open(filename, O_RDWR)) == -1))
{
fprintf(stderr, "open [ %s ]\n", strerror(errno));
return 1;
}
printf("Opened file [ %s ]\n", filename);
printf("sizeof(buff) [ %d ]\n", sizeof(buff));
printf("strlen(buff) [ %d ]\n", strlen(buff));
bytes_read = read(fd, buff, sizeof(buff));
printf("Bytes read [ %d ]\n", bytes_read);
return 0;
}
© Stack Overflow or respective owner