#include <stdio.h>
#include <string.h>
int main(void) {
/* Case 1: formatted output/input via snprintf and sscanf (%d, %f, %s) */
char fmt_buf[64];
int testInteger = 5;
int a = 0;
float b = 0.0f;
char token[32] = {0};
int num = 0;
int n1 = sscanf("10 3.14", "%d %f", &a, &b);
int n2 = sscanf("bluesky 123 extra", "%31s %d", token, &num);
snprintf(fmt_buf, sizeof(fmt_buf), "Number=%d|%d:%.2f|%s:%d", testInteger, a, b, token, num);
int c1_pass = (n1 == 2 && n2 == 2 && strcmp(fmt_buf, "Number=5|10:3.14|bluesky:123") == 0);
/* Case 2: single-char stream I/O (fputc/fgetc mirrors putchar/getchar) */
const char *char_path = "/tmp/c_io_char_lab.txt";
FILE *fp = fopen(char_path, "w");
int first_ch = EOF;
int put_ret = EOF;
if (fp != NULL) {
put_ret = fputc('b', fp);
fputs("luesky\n", fp);
fclose(fp);
}
fp = fopen(char_path, "r");
if (fp != NULL) {
first_ch = fgetc(fp); /* reads only a single char */
fclose(fp);
}
remove(char_path);
int c2_pass = (put_ret == 'b' && first_ch == 'b');
/* Case 3: fputs (no newline) vs newline-appended line & fgets bounded read */
const char *str_path = "/tmp/c_io_str_lab.txt";
fp = fopen(str_path, "w");
int fputs_ok = 0;
if (fp != NULL) {
fputs_ok = (fputs("Hello, ", fp) >= 0 && fputs("World!", fp) >= 0);
fclose(fp);
}
char line_no_nl[32] = {0};
char trunc_buf[6] = {0};
fp = fopen(str_path, "r");
if (fp != NULL) {
if (fgets(line_no_nl, (int)sizeof(line_no_nl), fp) == NULL) {
line_no_nl[0] = '\0';
}
rewind(fp);
if (fgets(trunc_buf, (int)sizeof(trunc_buf), fp) == NULL) {
trunc_buf[0] = '\0';
}
fclose(fp);
}
remove(str_path);
int c3_pass = (fputs_ok && strcmp(line_no_nl, "Hello, World!") == 0 &&
strcmp(trunc_buf, "Hello") == 0);
/* Case 4: fopen/fprintf/fgets/fclose full file I/O cycle + standard streams non-null */
const char *file_path = "/tmp/c_io_example_lab.txt";
int close_w = EOF, close_r = EOF;
char read_buf[64] = {0};
fp = fopen(file_path, "w");
if (fp != NULL) {
fprintf(fp, "Hello, world!\n");
close_w = fclose(fp);
}
fp = fopen(file_path, "r");
if (fp != NULL) {
if (fgets(read_buf, (int)sizeof(read_buf), fp) == NULL) {
read_buf[0] = '\0';
}
close_r = fclose(fp);
}
remove(file_path);
int streams_ok = (stdin != NULL && stdout != NULL && stderr != NULL);
int has_nl = (strcmp(read_buf, "Hello, world!\n") == 0);
int c4_pass = (streams_ok && close_w == 0 && close_r == 0 && has_nl);
printf("{\n");
printf(" \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
printf(" \"cases\": [\n");
printf(" {\"name\": \"formatted_printf_scanf\", \"expected\": \"Number=5|10:3.14|bluesky:123\", \"actual\": \"%s\", \"passed\": %s},\n",
fmt_buf, c1_pass ? "true" : "false");
printf(" {\"name\": \"single_char_io\", \"expected\": 98, \"actual\": %d, \"passed\": %s},\n",
first_ch, c2_pass ? "true" : "false");
printf(" {\"name\": \"fputs_and_bounded_fgets\", \"expected\": \"Hello, World!|Hello\", \"actual\": \"%s|%s\", \"passed\": %s},\n",
line_no_nl, trunc_buf, c3_pass ? "true" : "false");
printf(" {\"name\": \"file_stream_lifecycle\", \"expected\": {\"streams\": true, \"closed\": 0, \"matched_line\": true}, \"actual\": {\"streams\": %s, \"closed\": %d, \"matched_line\": %s}, \"passed\": %s}\n",
streams_ok ? "true" : "false", close_w | close_r, has_nl ? "true" : "false", c4_pass ? "true" : "false");
printf(" ]\n");
printf("}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。
入口文件:projects/c-input-output-lab/explore-input-output.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-input-output.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3225.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip