With the C program; Using the prototypes/declarations in the…

Question Answered step-by-step With the C program; Using the prototypes/declarations in the… With the C program;Using the prototypes/declarations in the Strings.h and stringTest.c ; compile and test a Strings.c file.Part a: Consider how to write a string to a file such that you can easily read it back in, remembering that 1. need to allocate the memory for the string 2. When begin reading the string you don’t know how long it will be.  This seems like an unsolvable problem. While there are solutions to this, we haven’t learned enough yet. In the meantime, how could you write the string out so that the program reading it in would know its length when it started?  Write the function to do this:      int fputString(FILE* pFOut, char* s); that writes the string to the file, preceded by the length of the string as an integer,      e.g. 9 I love C! Write a test driver program that reads two strings (assume less than 100 characters each) from standard in using gets() and writes them to an open file (file name from the command line arguments) using your function. Of course, you should add this as a new target in your Makefile before you get started. Part b:Using  functions from Strings module to help, write a function char* fgetString(FILE* pFIn);  that reads the string length from an open file, allocates the memory, reads the string into it, and returns it.  Now write a test driver program that opens a file (file name from the command line arguments) and reads all strings from a file and writes them, one string per line, to standard out. Of course, you should add this as a new target in your Makefile before you get started. Careful: 1. Need to make sure that the existing functions in your Strings module work correctly. If they don’t – fix ’em! 2. Don’t forget that the memory for the string will need to be free()’d when done with it! 3. may wish to run the Valgrind utility to check for potential memory leaks. /************************** stringTest.c************************/#include #include #include “Strings.h”// copies the program name (argv[0]) to a new string, prints it outint main(int argc, char* argv[]){ char* programName =(char*)malloc(sizeof(char*)*(MaxVal+1)); programName = duplicateString(argv[0]); if(programName == (char*)NULL){ fprintf(stderr,”Memory failure, terminating”); return EXIT_FAILURE; } // Did we do it? Can we print it out? printf(“%sn”, programName); getchar(); char *c = getchar; int i = 0; while( *c != ‘n’ && i < MaxVal+1){ programName[i] = *c; i++; *c = getchar(); } programName[i] = NULL; printf("%sn",programName); free(programName); return EXIT_SUCCESS;}/*****************Strings.h*******************/#ifndef STRINGS_H#define STRINGS_H// a cover function for malloc()// malloc and return memory for a string of stringsize characters// return (char*)NULL on failurechar* mallocString(int stringsize);// just a cover function for free()void freeString(char* s);// create a duplicate string of s// return it// return (char*)NULL on failure// should call mallocString(), and then strcpy()char* duplicateString(char* s);#endif Computer Science Engineering & Technology Software engineering CS 123 Share QuestionEmailCopy link Comments (0)