Question: Show me the steps to solve Now, complete the code for the write _ data function. You can write your code in the same box

Show me the steps to solve Now, complete the code for the write_data function. You can write your code in the same box above where I left the function stub (the return 0 in the write_data function). You will need to look up and learn how to use the fwrite() function.
This function is given a filename, a number specifying the number of bytes to write, and a string of bytes. Why must we pass the number of bytes as a parameter? Why can't we simply find out the length from the string? Well, C doesn't have strings. What we have is a char*, which is the address where the "string" starts. We can't ask the address how long it is - it is just an address!
Modify the main function above so that it reads a file's contents into memory and then writes out a new file with the contents. Ensure that the two files are exactly the same - byte for byte. To do this, you may want to check the size of the files, or use the hexdump command. This command will print out the contents of a file in hex. For instance:
0000000048656c 6c 6f 20776f 726c 642120204920|Hello world! I |
000000106c 6f 766520636f 6d 7075746572732e 0a |love computers..|
00000020
%%writefile file_tools.c
#include "file_tools.h"
#include
size_t read_data(char* filename, unsigned char** data){
// When we open a file, we keep that location
// as a file pointer, so declare one.
FILE* fp;
// Open the file for reading.
fp = fopen(filename,"r");
// Go to the end of the file.
fseek(fp,0L, SEEK_END);
// ftell says where we are in the file. Since we went to the
// end, this will tell us how many bytes are in the file.
size_t file_size = ftell(fp);
// Go back to the beginning.
fseek(fp,0L, SEEK_SET);
// Now, we know how many bytes we need. Ask for that much space,
// and store the address.
*data = malloc(file_size);
// Read the data and store it in our location.
fread(*data,1, file_size, fp);
// Close the file.
fclose(fp);
// Return the number of bytes we found.
return file_size;
}
size_t write_data(char* filename, size_t data_size, unsigned char* data){
return 0;
}
%%writefile reversal.c
#include
#include
#include "file_tools.h"
int main(int argc, char** argv){
// Create a variable that will hold the address of where
// our data is loaded into.
unsigned char* data;
// Load the data into that area and get back the number of
// bytes read.
size_t file_size = read_data("./shared/audio/backwards.wav", &data);
printf("Size: %lu bytes.
", file_size);
printf("
");
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!