换行符和 “Note that your output did not end in a newline”

本文讨论了在使用Java解决USACO题目时遇到的输出换行符问题,通过使用System.getProperty(line.separator)成功解决了跨平台换行符不一致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己没事儿的时候在弄 USACO 的题,用java写

结果都正确就是总提醒 结果需要另起一行 “Note that your output did not end in a newline”

上网搜需加上out.write("\r\n");

本机可以换行,但提交代码后,显示结果依然错误,没有换行而是变成了"@"

后来经搜索明白了,不同系统的换行符也不一样

所以应该通过System.getProperty("line.separator") 来获取当前系统的换行符

在文末加一句 out.write(System.getProperty("line.separator"));就成功通过了!

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> #include <signal.h> #define MAX_INPUT_SIZE 1024 #define MAX_ARGS 64 void execute_command(char **args); /* 执行命令 */ int main() { char input[MAX_INPUT_SIZE] = {0}; /* 存储用户输入缓冲区 */ char *args[MAX_ARGS] = {NULL}; /* 命令参数 */ char *token = NULL; /* 分割字符串 */ int arg_count = 0; /* 参数计数器 */ while (1) { printf("myshell > "); fflush(stdout); /* 获取输入, 处理 EOF */ if (fgets(input, sizeof(input), stdin) == NULL) { break; } /* 移除换行符 */ input[strcspn(input, "\n")] = '\0'; /* 处理 exit 命令 */ if (strcmp(input, "exit") == 0) { printf("Exiting shell...\n"); break; } /* 解析命令参数 */ token = strtok(input, " "); while (token != NULL && arg_count < MAX_ARGS - 1) { args[arg_count++] = token; token = strtok(NULL, " "); } args[arg_count] = NULL; /* 执行命令 */ execute_command(args); } return 0; } /* 执行命令函数 */ void execute_command(char **args) { /* 检查是否为内置命令 */ if (strcmp(args[0], "stop") == 0) { printf("Error: 'stop' is not a command. It should appear in program output.\n"); return; } pid_t pid = 0; int pipefd[2] = {0}; if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0)/* 子进程 */ { /* 关闭读取端 */ close(pipefd[0]); /* 重定向输出到管道 */ dup2(pipefd[1], STDOUT_FILENO); close(pipefd[1]); /* 执行命令 */ execvp(args[0], args); /* 执行失败 */ fprintf(stderr, "myshell: %s: command not found\n", args[0]); exit(EXIT_FAILURE); } else /* 父进程 */ { close(pipefd[1]); /* 关闭写入端 */ char buffer[MAX_INPUT_SIZE] = {0}; /* 输出缓冲区 */ ssize_t bytes_read = 0; /* 读取字节数 */ ssize_t total_printed = 0; /* 已打印字节数 */ char *stop_pos = strstr(buffer, "stop"); /* 检测输出中是否包含 stop */ /* 读取子进程输出 */ while ((bytes_read = read(pipefd[0], buffer, sizeof(buffer)-1)) > 0) { /* 确保字符串终止 */ buffer[bytes_read] = '\0'; /* 检测输出中是否包含 stop */ if (stop_pos) { /* 1、打印 stop 之前的内容 */ size_t keep_len = stop_pos - buffer; if (keep_len > 0) { printf("%.*s", (int)keep_len, buffer); fflush(stdout); } /* 2、打印stop本身 */ printf("stop"); fflush(stdout); /* 3、终止子进程并打印消息 */ kill(pid, SIGTERM); printf("\nReceived 'stop' in output. Terminating child process.\n"); fflush(stdout); /* 跳过后续处理 */ continue; } else /* 未检测到 stop 时正常打印输出 */ { printf("%s", buffer); fflush(stdout); } } close(pipefd[0]); /* 关闭读取端 */ waitpid(pid, NULL, 0); /* 等待子进程结束 */ } } 这是我的主要功能程序 #include <stdio.h> int main() { for (int i = 0; i < 5; i++) { printf("count: %d\n", i); if (i == 3) { printf("stop"); } } printf("This line should not be printed.\n"); return 0; } 这是我的测试程序 现在运行了./main后,输入以下内容: myshell > ./te myshell: ./te: command not found myshell > ./test count: 0 count: 1 count: 2 count: 3 stop Received 'stop' in output. Terminating child process. myshell > cat test.c cat: cat: No such file or directory cat: cat: No such file or directory #include <stdio.h> int main() { for (int i = 0; i < 5; i++) { printf("count: %d\n", i); if (i == 3) { printf("stop Received 'stop' in output. Terminating child process. myshell > 为什么会输出两个cat再输出test.c中的内容
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值