954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read in file (sound file) to vector and divide up vector about 8

Hello. I need help with a thing.
I have a file (sound file) I will send via controller area network and therefore can only send data about 8 byte or 8 vector as data [8]. This is sent away with the command sendfile ()
And I need help how I will do this code
Stages 1:
I will firstly read in the file to one vector as examples file []. How do I do this? Is there some completed code for this?
Stages 2:
Read in the first 8 vector to data [], then send this.
Stages 3:
Read vector 9 to vector 17 to dates [], then send this.
This happens until the entire file has been sent.
pleas help me to give a basis on how I will intend and to type the code.
///
I must apology for my poor English
Anders

komany
Newbie Poster
5 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

by vector do you mean a character array ? Such as char data[255];

Use the FILE and associated functions located in stdio.h to read and write files. Here is a tutorial.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Exact. Thanks for the advice.The file I will read in is sound.wav or sound.mp3. Can I read in that file to char data [255]. Or must I do it to binary first
//
Anders

komany
Newbie Poster
5 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

since its binary data it will be better to read it into unsigned char array. There is one way to do it.

unsigned char* buf = 0;
size_t filesize = 0;
FILE* fp = fopen("sound.mp3","rb");
// get the size of the file and allocate input buffer space
fseek(fp,0,SEEK_END); 
filesize = ftell(fp);
// go back to beginning of file
fseek(fp,0,SEEK_SET);
// allocate input buffer
buf = malloc(filesize);
// read into buffer
fread(buf,1,filesize,fp);
fclose(fp);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

perfect. I think I can use that in my program.

komany
Newbie Poster
5 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You