调用API的时候需小心处理fgets里面的'\n'换行符

本文详细阐述了fgets函数在处理输入字符串换行符时的注意事项,特别是在子进程间通信场景下如何正确处理换行符以避免错误,通过实例演示了如何在父子进程中安全地传递包含换行符的字符串。
调用API的时候需小心处理fgets里面的'\n'换行符

#include "apue.h"
#include <sys/wait.h>

int main(void)
{
  char buf[MAXLINE];
  pid_t pid;
  int status;
  printf("%%");
  while (fgets(buf, MAXLINE, stdin) != NULL)
  {
//这里的目的是如果字符串结束字符\0前面,如果有个'\n'那么就替换为'\0'
   //if (buf[strlen(buf)-1] == '\n')
     //  buf[strlen(buf)-1] = 0;
   if ((pid = fork()) < 0)
     err_sys("fork error");
   else if (pid == 0)
   {  
//如果前面不对'\n'进行处理的话,这里会报错
//: No such file or directory 

      execlp(buf, buf, (char *)0);
      err_ret("couldn't execute: %s", buf);
      exit(127);
   }
   if ((pid = waitpid(pid, &status, 0))<0)
      err_sys("waitpid error");
   printf("%%");
  }
 exit(0);
}

//x /10cxb buf


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #define MAX_RECIPES 100 #define NAME_LEN 100 #define CATEGORY_LEN 50 #define INGREDIENTS_LEN 200 #define STEPS_LEN 500 #define LIKED_BY_LEN 100 // 定义菜谱结构体 typedef struct { char name[NAME_LEN]; char category[CATEGORY_LEN]; char ingredients[INGREDIENTS_LEN]; char steps[STEPS_LEN]; char liked_by[LIKED_BY_LEN]; } Recipe; Recipe recipes[MAX_RECIPES]; // 存储所有菜谱 int recipe_count = 0; // 当前菜谱数量 // 函数声明 void add_recipe(); void edit_recipe(); void delete_recipe(); void search_recipes(); void view_all_recipes(); void main_menu(); // <<< 添加 main_menu 的函数声明 // 程序入口 int main() { SetConsoleOutputCP(936); // 设置控制台输出为 GBK SetConsoleCP(936); // 设置控制台输入为 GBK main_menu(); // 调用主菜单 return 0; } // 主菜单函数定义 void main_menu() { int choice; while (1) { printf("\n--- 菜谱管理系统 ---\n"); printf("1. 添加菜谱\n"); printf("2. 修改菜谱\n"); printf("3. 删除菜谱\n"); printf("4. 查询菜谱\n"); printf("5. 查看全部菜谱\n"); printf("6. 退出系统\n"); printf("请选择操作: "); scanf("%d", &choice); getchar(); // 清除换行符 switch (choice) { case 1: add_recipe(); break; case 2: edit_recipe(); break; case 3: delete_recipe(); break; case 4: search_recipes(); break; case 5: view_all_recipes(); break; case 6: exit(0); default: printf("无效选择,请重新输入。\n"); } } } // 添加菜谱 void add_recipe() { if (recipe_count >= MAX_RECIPES) { printf("菜谱已满,无法添加更多。\n"); return; } Recipe *r = &recipes[recipe_count++]; printf("请输入菜名: "); fgets(r->name, NAME_LEN, stdin); r->name[strcspn(r->name, "\n")] = '\0'; printf("请输入分类: "); fgets(r->category, CATEGORY_LEN, stdin); r->category[strcspn(r->category, "\n")] = '\0'; printf("请输入食材: "); fgets(r->ingredients, INGREDIENTS_LEN, stdin); r->ingredients[strcspn(r->ingredients, "\n")] = '\0'; printf("请输入制作步骤: "); fgets(r->steps, STEPS_LEN, stdin); r->steps[strcspn(r->steps, "\n")] = '\0'; printf("请输入谁喜欢吃: "); fgets(r->liked_by, LIKED_BY_LEN, stdin); r->liked_by[strcspn(r->liked_by, "\n")] = '\0'; printf("菜谱添加成功!\n"); } // 修改菜谱 void edit_recipe() { char name[NAME_LEN]; printf("请输入要修改的菜名: "); fgets(name, NAME_LEN, stdin); name[strcspn(name, "\n")] = '\0'; for (int i = 0; i < recipe_count; i++) { if (strcmp(recipes[i].name, name) == 0) { int field; printf("请选择要修改的字段:\n"); printf("1. 分类\n2. 食材\n3. 制作步骤\n4. 谁喜欢吃\n"); scanf("%d", &field); getchar(); // 清除换行符 switch (field) { case 1: printf("请输入新分类: "); fgets(recipes[i].category, CATEGORY_LEN, stdin); recipes[i].category[strcspn(recipes[i].category, "\n")] = '\0'; break; case 2: printf("请输入新食材: "); fgets(recipes[i].ingredients, INGREDIENTS_LEN, stdin); recipes[i].ingredients[strcspn(recipes[i].ingredients, "\n")] = '\0'; break; case 3: printf("请输入新制作步骤: "); fgets(recipes[i].steps, STEPS_LEN, stdin); recipes[i].steps[strcspn(recipes[i].steps, "\n")] = '\0'; break; case 4: printf("请输入新的喜好者: "); fgets(recipes[i].liked_by, LIKED_BY_LEN, stdin); recipes[i].liked_by[strcspn(recipes[i].liked_by, "\n")] = '\0'; break; default: printf("无效选择。\n"); } printf("菜谱信息已更新。\n"); return; } } printf("未找到该菜名的菜谱。\n"); } // 删除菜谱 void delete_recipe() { char name[NAME_LEN]; printf("请输入要删除的菜名: "); fgets(name, NAME_LEN, stdin); name[strcspn(name, "\n")] = '\0'; for (int i = 0; i < recipe_count; i++) { if (strcmp(recipes[i].name, name) == 0) { for (int j = i; j < recipe_count - 1; j++) { recipes[j] = recipes[j + 1]; } recipe_count--; printf("菜谱删除成功。\n"); return; } } printf("未找到该菜名的菜谱。\n"); } // 查询菜谱 void search_recipes() { int choice; printf("请选择查询方式:\n"); printf("1. 按菜名\n2. 按分类\n3. 按食材\n4. 按谁喜欢吃\n"); scanf("%d", &choice); getchar(); // 清除换行符 char keyword[NAME_LEN]; printf("请输入关键词: "); fgets(keyword, NAME_LEN, stdin); keyword[strcspn(keyword, "\n")] = '\0'; int found = 0; for (int i = 0; i < recipe_count; i++) { switch (choice) { case 1: if (strstr(recipes[i].name, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; case 2: if (strstr(recipes[i].category, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; case 3: if (strstr(recipes[i].ingredients, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; case 4: if (strstr(recipes[i].liked_by, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; } } if (!found) printf("未找到匹配的菜谱。\n"); } // 查看全部菜谱 void view_all_recipes() { if (recipe_count == 0) { printf("当前没有菜谱。\n"); return; } for (int i = 0; i < recipe_count; i++) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); } } 详细解释这段代码所用的库和函数,给出每一行代码的详细解释
07-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值