void swap_int(int *p1, int *p2) {
int tmp = *p1; *p1 = *p2; *p2 = tmp;
}
int main(void) {
int a, b; scanf("%d%d", &a, &b);
swap_int(&a, &b);
printf("%d %d\n", a, b);
return 0;
}
#include <string.h>
void invert_str(char *str) {
char *left = str;
char *right = str + strlen(str) - 1;
while (left < right) {
char tmp = *left; *left = *right; *right = tmp;
++left; --right;
}
}
int main(void) {
char s[100] = "hello";
invert_str(s);
printf("%s\n", s); // olleh
return 0;
}
int str_index(const char *s, const char *t) {
if (!*t) return 0; // 空串匹配位置 0
for (int i = 0; s[i]; ++i) {
int j;
for (j = 0; t[j] && s[i+j] == t[j]; ++j);
if (!t[j]) return i; // t[j]=='\0' 表示全匹配
}
return -1;
}
關鍵修正:原提示代碼 if (j+1 == n) 錯誤,應判斷 t[j] == '\0' (即 !t[j])。
int is_palindrome(const char *str) {
const char *left = str;
const char *right = str + strlen(str) - 1;
while (left < right) {
if (*left != *right) return 0;
++left; --right;
}
return 1;
}
#include <stdio.h>
int main(void) {
FILE *fp = fopen("out.dat", "w");
if (!fp) return 0;
int ch;
while ((ch = getchar()) != '$')
fputc(ch, fp);
fclose(fp); // (2) 關閉文件
return 1;
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp = fopen("fname.dat", "r");
if (!fp) { perror("Can't open file"); exit(EXIT_FAILURE); }
long count = 0;
int ch;
while ((ch = fgetc(fp)) != EOF) // (1) 讀取字符,EOF 表示文件結束
++count;
printf("num=%ld\n", count);
fclose(fp);
return 0;
}
關鍵修正:原代碼 while (fgetc(fp)) num++ 錯誤:fgetc 返回字符值 (0-255),字符 '\0' (值 0) 會導致循環提前終止。必須與 EOF 比較。
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
char ch, fname[100];
printf("Input filename: ");
scanf("%99s", fname); // 讀取文件名
if ((fp = fopen(fname, "w")) == NULL) { // (1) 以寫方式打開
perror("Cannot open"); exit(EXIT_FAILURE);
}
printf("Enter data (end with #):\n");
getchar(); // 吸收 scanf 遺留的換行符
while ((ch = getchar()) != '#')
fputc(ch, fp); // (2) 寫入字符
fclose(fp);
return 0;
}
#include <stdio.h>
struct Rec { int a; float b; };
void recout(FILE *fp) {
struct Rec r;
size_t cnt;
while ((cnt = fread(&r, sizeof(struct Rec), 1, fp)) == 1) { // (1)&r, (2)1, (3)cnt==1, (4)break/continue
printf("%d, %f\n", r.a, r.b);
}
// 讀取失敗 (cnt==0) 可能是 EOF 或錯誤,循環自然結束
}
int main(void) {
FILE *fp = fopen("file.dat", "rb");
if (!fp) { perror("open"); return 1; }
recout(fp);
fclose(fp);
return 0;
}
關鍵修正:原代碼 do { fread...; if(...) break; } while(1) 結構易誤用 feof。標準模式:**while (fread(...) == 1) 直接以讀取成功為循環條件**。
int main(void) {
FILE *fp = fopen("l1.txt", "w");
char ch;
printf("Enter string (end #): ");
while ((ch = getchar()) != '#')
fputc(ch, fp);
fclose(fp);
return 0;
}
int main(void) {
FILE *fp = fopen("l1.txt", "r");
int ch;
while ((ch = fgetc(fp)) != EOF)
if (isalnum(ch)) putchar(ch);
fclose(fp);
return 0;
}
int main(void) {
FILE *fr = fopen("l1.txt", "r"), *fw = fopen("l2.txt", "w");
int ch;
while ((ch = fgetc(fr)) != EOF)
if (isdigit(ch)) fputc(ch, fw);
fclose(fr); fclose(fw);
return 0;
}
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
int main(void) {
FILE *fp = fopen("l6.txt", "w");
for (int deg = 0; deg <= 360; ++deg) {
double rad = deg * M_PI / 180.0;
double val = sin(rad);
fprintf(fp, "%d %f\n", deg, val);
if (val > 0) printf("%d %f\n", deg, val);
}
fclose(fp);
return 0;
}
fopen → 檢查返回值 → 循環讀寫 (對比 EOF/返回值) → fclose"r"/"w" 文本模式 (換行符轉換),"rb"/"wb" 二進制 (原始字節),結構體讀寫**必須用二進制**== 項目數ctype.h 中 isalnum/isdigit/isalpha/isspace/ispunct — 參數為 int (需轉換 unsigned char 避免 UB)math.h、-lm 鏈接、M_PI 或自定義、角度轉弧度 deg * M_PI / 180