Write several functions in c /** * This function is responsible for reading an i
ID: 2989529 • Letter: W
Question
Write several functions in c
/**
* This function is responsible for reading an image file and placing the pixel data
* from the file into the array image_data
* The function automatically determines whether the image data is gray scale or
* rgb and sizes image_data accordingly
*
* @param char* file_name - The name of the file to be opened
* @param char** image_data - A pointer to an array of pixel data
* @param int* width - The width of the read image in pixels
* @param int* height - The height of the read image in pixels
*
* @return int - The size of the allocated image_data array, otherwise, -1
*/
int read_image(char* file_name, char** image_data, int* width, int* height);
/**
* This function is responsible for writing image data to a file
* The function automatically determines whether the image data is gray scale or rgb
*
* @param char* file_name - The name of the file to be created
* @param int width - The width of the image in pixels
* @param int height - The height of the image in pixels
* @param char* image_data - The image data array to be written to file
* @param int data_length - The length of the image data array
*
* @return int - the numeric value 0 on success,
* -1 if the image file could not be opened for writing
*/
int write_image(char* file_name, int width, int height, char* image_data, int data_length);
/**
* This function is responsible for embedding a message into an array of
* image data and placing the resulting array into encoded
*
* @param char* image_data - The image data array which will be used
* to embed the message
6* @param int data_length - The length of the image data array
* @param char* message - The message to be encoded
* @param char** encoded - A new array containing the image data
* embedded with message
*
* @return int - the numeric value 0 on success,
* -1 if the message is too large for the image data array
*/
int embed(char* image_data, int data_length, char* message, char** encoded);
/**
* This function is responsible for extracting the potential message
* from the supplied image data
*
* @param char* image_data - The image data array from which we extract the message
* @param int data_length - The length of the image data array
* @param char** message - The extracted message array
*
* @return int - the numeric value 0 on success
*/
int extract(char* image_data, int data_length, char** message);
/**
* This function is responsible for rotating each alphabetic character in the message
* k times to hide the meaning of the message
*
* @param char* message - The message which the rotation will be applied to
* (null terminated string)
*
* @param int k - The number of rotations to be applied
*
* @return int - A count of the number of characters modified
*/
int rot_k(char* message, int k);
/**
* This function attempts to look for a rotated message by brute-force
* until a known word is found or the word could not be detected
*
* @param char* encoded - The potentially rotated message
* @param char* known_word - The word that will be searched for while rotating
* the encoded message
* @param char** message - A new array containing the detected message
*
* @return int - the k value needed to remove the rotation on success,
* -1 if no message was detected
*/
int crack(char* encoded, char* known_word, char** message);
Explanation / Answer
All file functions need <stdio.h> to work properly.
The first thing you need to know about is file pointers. File pointers are like any other pointer, but they point to a file. (Kind of obvious). You define a file pointer as follows:
In order to make the file pointer point to a file you use the fopen function. The function works as follows:
fopen returns a file pointer. It returns NULL if the file does not exist.
fopen takes the first argument as the filename to open. It needs to be a string.
The second argument is the mode argument
Mode specifies what you want to do with the file.
Some modes are:
These modes will open files in text mode. Files opened in text mode have some bytes filtered out. If you want to open binary files use binary mode by adding a "b" to the mode. For example:
There are three input and output streams that are automatically open whenever your program starts. These are stdin, stdout and stderr.
These file pointers work as follows:
To read a character from a file, you use fgetc. It is like getchar, but for any file, not just stdin.
It works like this:
fgetc returns the character that is read from the file as an integer.
fgetc takes the file pointer as its only input.
It will automatically increment the pointer to read the next character.
fputc allows you to write a character to a file:
fputc takes an unsigned char as the first argument and the file pointer as the second argument.
fputc returns EOF when it reaches the end of file. EOF is a constant defined in <stdio.h>
You can also use fprintf and fscanf. They work like printf and scanf, except the file pointer is the first argument. They work like this:
In order to close the file again, you must use fclose. It looks like this:
fclose closes the file that filepointer points to.
For example, if you want to print the contents of data.txt the code could look something like this:
There are also fputs and fgets. fputs is simple, similar to puts. Unlike puts, it does not automatically append a newline to supplied string. It writes a line to a file like so:
fgets is a special function in C. It is regarded as the best function in standard C to reliably accept input. Functions like scanf have undefined behavior when given erroneous input. Programs that use scanf or gets can have buffer overflows and be susceptible to exploits! scanf can be nice for those just starting to learn C, but it should never be used in real-world code.
fgets usage looks like this:
The middle argument is the beauty of fgets. It is the limit of characters that fgets will store in the char array. Usually sizeof(firstargument) is a good idea, unless you are allocating memory with malloc or a similar function.
fgets reads as much as it can. If the input goes over the limit, it will stop. The next file-reading function will continue where fgets left off. (Many times, fgets again, in a loop)
fgets does not discriminate. It reads spaces and newlines with the rest of the input.
fgets returns NULL when nothing can be read. (end of file)
Here's an example very similar to the one above, but instead of fgetc, it uses fgets and fputs:
That's it! please rate my answer friend...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.