gpt4 book ai didi

c - 打印出当前工作目录

转载 作者:行者123 更新时间:2023-11-30 15:37:43 24 4
gpt4 key购买 nike

下面我有一个分词器,我正在尝试将其转换为 shell 程序。我才刚刚开始,所以我知道该程序未设置为接受任何 shell 命令,但我在打印当前工作目录时遇到问题。我将在下面显示我的代码:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{
pid_t pid;
char cwd[1000];
if (getcwd, sizeof(cwd) != NULL)
{
fprintf(stdout, "Current working dir: %s\n");
return 0;
}
printf("Please enter a string\n");
int ch = fgetc(stdin);
while (ch != EOF)
{
pid = fork();
if (pid == 0)
{
printf("Child Working");

}
else
printf("Child not working");
while (isspace(ch))
{

// If only 1 line of input allowed, then add
if (ch == '\n') return 0;;

ch = fgetc(stdin);
}
if (ch != EOF)
{
do
{
fputc(ch, stdout);
ch = fgetc(stdin);
}
while (ch != EOF && !isspace(ch));
fputc('\n', stdout);
}
}
return 0;
}

到目前为止我得到的输出在下面的屏幕截图中:

enter image description here

最佳答案

改变

if (getcwd, sizeof(cwd) != NULL)

至:

if (getcwd(cwd, sizeof(cwd)) != NULL)

fprintf(stdout, "Current working dir: %s\n");

至:

printf("Current working dir: %s\n", cwd);

关于c - 打印出当前工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22121479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com