如何 "execute" Ubuntu 中的 Makefile 到 运行 从标准输入读取并输出到 stdout/stderr 的 C 程序
How to "execute" Makefile in Ubuntu to run C program that reads from stdin and outputs to stdout/stderr
我正在做一个家庭作业问题,它从 input.txt 文件中读取字符并将读取的第一个单词输出到 output.txt,第二个变成error.txt,然后第三个变成output.txt,然后依此类推,直到到达 input.txt 文件的末尾。
我应该注意这一切都是使用 Ubuntu 18.04
完成的
我得到了一个自定义 Makefile,我不得不编辑一个名为 split.c 的 C 程序,该程序需要 input.txt 通过 stdin 并输出到 stdout/stderr。我可以编写我的 C 程序并将其列在下面,但是我无法测试它是否正确,因为我不了解如何 运行 make,如何正确设置我的文件以及我的 C 程序是否正确读取并按应有的方式输出。
我已经尝试 运行在终端中输入 'make' 命令,但我收到:
make: 未指定目标且未找到 makefile。停止.
我看了无数关于Linux、制作'make files'等的文章,但我不知道我在问什么,也不知道该怎么做,所以我停滞不前.非常感谢指导!
自定义 makefile 如下所示,名称为 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)
我的 C 程序看起来像这样,叫做 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;
}
的想法是它接受输入文件,然后将每个字母打印到它各自的文件中,如果遇到 space 它会在添加下一个字符之前打印一个新行进入另一个文件。
例如:
input.txt 有文字:
"How do I do this stuff?"
output.txt 将有:
How
I
this
error.txt 将有:
do
do
stuff?
我完全认为我的 C 程序缺少代码。我写程序的时候的想法是,打印到stdout,然后如果遇到whitespace,打印一个新行,然后开始打印到stderr,重复直到到达EOF。
要么将 Makefile.dat
重命名为 Makefile
,如下所示:
mv Makefile.dat Makefile
make
或 运行 make
与 -f
选项和你的 Makefile
:
的参数
make -f Makefile.dat
确保您的所有文件(split.c
、test/input.txt
、Makefile
)都位于与 Makefile
相同的目录中,否则这将不起作用。
请注意您的 C 代码的一些问题:
if(input = " ")
错了。首先,它将一个带有 space 的字符串 " "
(这是指向 char
的指针)分配给 input
,而不是检查是否 input
是一个 space 字符。
要解决此问题,请使用:
if(input == ' ')
在这两种情况下。
我正在做一个家庭作业问题,它从 input.txt 文件中读取字符并将读取的第一个单词输出到 output.txt,第二个变成error.txt,然后第三个变成output.txt,然后依此类推,直到到达 input.txt 文件的末尾。
我应该注意这一切都是使用 Ubuntu 18.04
完成的我得到了一个自定义 Makefile,我不得不编辑一个名为 split.c 的 C 程序,该程序需要 input.txt 通过 stdin 并输出到 stdout/stderr。我可以编写我的 C 程序并将其列在下面,但是我无法测试它是否正确,因为我不了解如何 运行 make,如何正确设置我的文件以及我的 C 程序是否正确读取并按应有的方式输出。
我已经尝试 运行在终端中输入 'make' 命令,但我收到:
make: 未指定目标且未找到 makefile。停止.
我看了无数关于Linux、制作'make files'等的文章,但我不知道我在问什么,也不知道该怎么做,所以我停滞不前.非常感谢指导!
自定义 makefile 如下所示,名称为 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)
我的 C 程序看起来像这样,叫做 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;
}
的想法是它接受输入文件,然后将每个字母打印到它各自的文件中,如果遇到 space 它会在添加下一个字符之前打印一个新行进入另一个文件。
例如:
input.txt 有文字:
"How do I do this stuff?"
output.txt 将有:
How
I
this
error.txt 将有:
do
do
stuff?
我完全认为我的 C 程序缺少代码。我写程序的时候的想法是,打印到stdout,然后如果遇到whitespace,打印一个新行,然后开始打印到stderr,重复直到到达EOF。
要么将 Makefile.dat
重命名为 Makefile
,如下所示:
mv Makefile.dat Makefile
make
或 运行 make
与 -f
选项和你的 Makefile
:
make -f Makefile.dat
确保您的所有文件(split.c
、test/input.txt
、Makefile
)都位于与 Makefile
相同的目录中,否则这将不起作用。
请注意您的 C 代码的一些问题:
if(input = " ")
错了。首先,它将一个带有 space 的字符串 " "
(这是指向 char
的指针)分配给 input
,而不是检查是否 input
是一个 space 字符。
要解决此问题,请使用:
if(input == ' ')
在这两种情况下。