gpt4 book ai didi

c - 如何在 C 语言中的 shell 中追加到文件末尾?

转载 作者:行者123 更新时间:2023-11-30 19:41:05 24 4
gpt4 key购买 nike

我正在尝试用 C 语言编写用于 I/O 重定向的 shell 程序。输入和输出工作正常,但我还必须使用“>>”将输出 append 到文件末尾,但它不起作用。每当我在命令行中使用 >> 时,它只会覆盖文件的输出。而且运行程序时我还收到警告:警告:多字符字符常量。如果有人能在正确的方向上帮助我解决这个问题,我将非常感激。

        // Check for redirected input
input = redirect_input(args, &input_filename);

switch(input) {
case -1:
printf("Syntax error!\n");
continue;
break;
case 0:
break;
case 1:
printf("Redirecting input from: %s\n", input_filename);
break;
}

// Check for redirected output
output = redirect_output(args, &output_filename);

switch(output) {
case -1:
printf("Syntax error!\n");
continue;
break;
case 0:
break;
case 1:
printf("Redirecting output to: %s\n", output_filename);
break;
}

// Check for redirected append output
append = redirect_append(args, &append_filename);

switch(append) {
case -1:
printf("Syntax error!\n");
continue;
break;
case 0:
break;
case 1:
printf("Redirecting output to: %s\n", output_filename);
break;
}



// Do the command
do_command(args, block,
input, input_filename,
output, output_filename,
append, append_filename);
}
}


/*
* Do the command
*/
int do_command(char **args, int block,
int input, char *input_filename,
int output, char *output_filename,
int append, char *append_filename) {

int result;
pid_t child_id;
int status;

// Fork the child process
child_id = fork();

// Check for errors in fork()
switch(child_id) {
case EAGAIN:
perror("Error EAGAIN: ");
return;
case ENOMEM:
perror("Error ENOMEM: ");
return;
}

if(child_id == 0) {

// Set up redirection in the child process
if(input)
freopen(input_filename, "r", stdin);

if(output)
freopen(output_filename, "w+", stdout);

if (append)
freopen(append_filename, "a", stdout);

// Execute the command
result = execvp(args[0], args);

exit(-1);
}

// Wait for the child process to complete, if necessary
if(block) {
printf("Waiting for child, pid = %d\n", child_id);
result = waitpid(child_id, &status, 0);
}
}

/*
* Check for input redirection
*/
int redirect_input(char **args, char **input_filename) {
int i;
int j;

for(i = 0; args[i] != NULL; i++) {

// Look for the <
if(args[i][0] == '<') {
//free(args[i]);

// Read the filename
if(args[i+1] != NULL) {
*input_filename = args[i+1];
} else {
return -1;
}

// Adjust the rest of the arguments in the array
for(j = i; args[j-1] != NULL; j++) {
args[j] = args[j+2];
}

return 1;
}
}

return 0;
}

/*
* Check for output redirection
*/
int redirect_output(char **args, char **output_filename) {
int i;
int j;

for(i = 0; args[i] != NULL; i++) {

// Look for the >
if(args[i][0] == '>') {
//free(args[i]);

// Get the filename
if(args[i+1] != NULL) {
*output_filename = args[i+1];
} else {
return -1;
}

// Adjust the rest of the arguments in the array
for(j = i; args[j-1] != NULL; j++) {
args[j] = args[j+2];
}

return 1;
}
}

return 0;
}

/*
* Check for append redirection
*/
int redirect_append(char **args, char **append_filename) {
int i;
int j;

for(i = 0; args[i] != NULL; i++) {

// Look for the >>
if(args[i][0] == '>' && args[i][1] == '>') {
//free(args[i]);

// Read the filename
if(args[i+2] != NULL) {
*append_filename = args[i+2];
} else {
return -1;
}

// Adjust the rest of the arguments in the array
for(j = i; args[j-1] != NULL; j++) {
args[j] = args[j+2];
}

return 1;
}
}

return 0;
}

最佳答案

if(args[i][0] == '>>') {

应该是:

if(args[i][0] == '>' && args[i][1] == '>') {

这是“多字符常量”警告以及输出重定向问题的根源。

关于c - 如何在 C 语言中的 shell 中追加到文件末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33943874/

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