From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 29_outname/Makefile | 12 ++++++++++++ 29_outname/README | 18 ++++++++++++++++++ 29_outname/grade.txt | 19 +++++++++++++++++++ 29_outname/outname.c | 11 +++++++++++ 29_outname/outname.h | 6 ++++++ 29_outname/outname_test.c | 18 ++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 29_outname/Makefile create mode 100644 29_outname/README create mode 100644 29_outname/grade.txt create mode 100644 29_outname/outname.c create mode 100644 29_outname/outname.h create mode 100644 29_outname/outname_test.c (limited to '29_outname') diff --git a/29_outname/Makefile b/29_outname/Makefile new file mode 100644 index 0000000..7cdf5f1 --- /dev/null +++ b/29_outname/Makefile @@ -0,0 +1,12 @@ +CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 +OBJS=outname.o outname_test.o +PROGRAM=outname_test + +$(PROGRAM): $(OBJS) + gcc $(CFLAGS) -o $@ $(OBJS) + +%.o: %.c outname.h + gcc -c $(CFLAGS) $< + +clean: + rm -f $(OBJS) $(PROGRAM) *~ diff --git a/29_outname/README b/29_outname/README new file mode 100644 index 0000000..735972e --- /dev/null +++ b/29_outname/README @@ -0,0 +1,18 @@ +In a future assignment, you are going to write a program which reads +some input, does some manipulations, and produces output in another file. +Right now, you are going to write a function which will be useful to you +in that assignment, which takes the input filename, and produces the output +file name. The output file name should be the same as the input file name +with ".counts" on the end. So, for example, if the input filename (inputName) +is "input.txt", your function should return "input.txt.counts". + +Note that your function needs to use malloc to allocate memory to hold +the string that it will return as its answer. + +Your function should have the following signature: + + char * computeOutputFileName(const char * inputName) + +and you should write it in the outname.c file. +You can make and test with the main function found in outname_test.c. +As always, be sure your program valgrinds cleanly. diff --git a/29_outname/grade.txt b/29_outname/grade.txt new file mode 100644 index 0000000..12148a6 --- /dev/null +++ b/29_outname/grade.txt @@ -0,0 +1,19 @@ +Grading at Tue 14 Dec 2021 02:54:10 AM UTC +Attempting to compile: +################################################# +testcase1: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase2: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase3: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean + +Overall Grade: A diff --git a/29_outname/outname.c b/29_outname/outname.c new file mode 100644 index 0000000..7cc4346 --- /dev/null +++ b/29_outname/outname.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include "outname.h" + +char * computeOutputFileName(const char * inputName) { + char * outFileName = malloc((strlen(inputName) + 8) * sizeof(*outFileName)); + strcpy(outFileName, inputName); + strcat(outFileName, ".counts"); + return outFileName; +} diff --git a/29_outname/outname.h b/29_outname/outname.h new file mode 100644 index 0000000..acd1f03 --- /dev/null +++ b/29_outname/outname.h @@ -0,0 +1,6 @@ +#ifndef __OUTNAME_H__ +#define __OUTNAME_H__ + +char * computeOutputFileName(const char * inputName); + +#endif diff --git a/29_outname/outname_test.c b/29_outname/outname_test.c new file mode 100644 index 0000000..6a9493a --- /dev/null +++ b/29_outname/outname_test.c @@ -0,0 +1,18 @@ +#include "outname.h" +#include +#include + + +#define NUM_TESTS 3 +int main(void) { + char * testNames[NUM_TESTS] = {"input.txt", + "anotherTestFileName.txt", + "somethingelse"}; + + for (int i = 0; i < NUM_TESTS; i++) { + char * outName = computeOutputFileName(testNames[i]); + printf("'%s' => '%s'\n", testNames[i], outName); + free(outName); + } + return EXIT_SUCCESS; +} -- cgit v1.2.3