I'm working on a homework problem that reads characters from an input.txt file and outputs the first word read into an output.txt, the second into an error.txt, then the third to output.txt again, and so on until it reaches the end of the input.txt file.
I should note this is all done using Ubuntu 18.04
I was given a custom Makefile and had to edit a C program called split.c which would take the input.txt through the stdin and output to stdout/stderr. I can write my C program and have it listed below, however I can't test if it's correct because I do not understand how to run make, how to set up my files correctly and if my C program is correctly reading and outputing as it should.
I have tried running the 'make' command in the terminal but I receive:
make: No targets specified and no makefile found. Stop.
I have looked at countless articles on Linux, making 'make files', etc. but I don't know what I'm being asked or what to do so I am at a stand still. Guidance is greatly appreciated!
The custom makefile looks like this and is called Makefile.dat:
CC=gcc
CFLAGS=-Wall
EXEC=split
SRC=$(EXEC).c
TEXT_DIR=test
TEST_INPUT=$(TEST_DIR)/input.txt
OUT=$(TEST_DIR)/stdout.txt
ERR=$(TEST_DIR)/stderr.txt
EXP_OUT=$(TEST_DIR)/output.txt
EXP_ERR=$(TEST_DIR)/error.txt
TEST_REQS=$(TEST_INPUT) $(EXP_OUT) $(EXP_ERR)
DIFF=diff -bBqa
all: $(EXEC)
$(EXEC): $(SRC)
$(CC) $(CFLAGS) $(SRC) -o $(EXEC)
.PHONY: test
test: $(TEST_REQS) $(EXEC)
./$(EXEC) < $(TEST_INPUT) > $(OUT) 2> $(ERR)
$(DIFF) $(EXP_OUT) $(OUT)
$(DIFF) $(EXP_ERR) $(ERR)
echo TEST PASSED!
.PHONY: clean
clean:
$(RM) $(EXEC)
And my C program looks like this and is called split.c:
#include <stdio.h>
int main(){
int input;
// keep getting characters until end-of-file
while((input = fgetc(stdin)) != EOF){
// prints to stdout
printf(stdout, "%d", input);
if(input = " ")
printf("\n"); // encounters whitespace so print new line
// prints to stderr
printf(stderr, "%d", input);
if(input = " ")
printf("\n"); // encounters whitespace so print new line
}
return 0;
}
With the idea being that it takes the input file, then it'll print each letter into it's respective file, and if it encounters a space it'll print a new line before adding the next character into the other file.
For example:
input.txt has the text:
"How do I do this stuff?"
output.txt will have:
How
I
this
error.txt will have:
do
do
stuff?
I fully expect that my C program is missing code. My thinking when writing the program was, print to stdout, then if whitespace is encounterd, print a new line, then begin printing to stderr, and repeat until EOF is reached.
Either rename Makefile.dat
to Makefile
, like this:
mv Makefile.dat Makefile
make
or run make
with the -f
option and an argument of your Makefile
:
make -f Makefile.dat
Make sure all your files (split.c
, test/input.txt
, Makefile
) are present in the same directory as the Makefile
, otherwise this won't work.
Note some issues with your C code:
if(input = " ")
is wrong. First of all, it's assigning a string with a space in it " "
(which is a pointer to char
) to input
, instead of checking if input
is a space character.
To fix the issue, use this:
if(input == ' ')
in both cases.