新人求助如何读写使得两个字符串读取不会发生前者吃掉后者的一部分字符

生风虎 2014-12-03 09:45:59
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

#define TITLE "电子英汉词典" /*标题文字*/
#define MAX 30 /*非例句的空间上限*/
#define MAX1 80 /*例句的空间上限*/


#define MENU_MAIN_COUNT 8/* 主菜单的选项个数*/
#define MENU_SEARCH_COUNT 4 /*查询子菜单的选项个数 */
#define MENU_CALC_COUNT 3 /*统计子菜单的选项个数 */

typedef struct dictionary{
char english_word[MAX]; /*英文单词*/
char character_word[MAX]; /*英文单词词性*/
char soundmark_word[MAX]; /*英文单词音标*/
char chinese_word[MAX]; /*中文注释*/
char english_phrase[MAX]; /*英文短语*/
char example_sentence[MAX1]; /*英文例句*/
struct dictionary *next; /*指向下一个单词的指针*/
}dic;

dic *head=NULL;

char menu_main[] =
"|\n"
"| 1 词条录入\n"
"| 2 信息显示\n"
"| 3 退出程序\n"
"| 4 信息保存\n"
"| 5 文件装载\n"
"| 6 单词全拼查询\n"
"| 7 单词注释查询\n"
"| 8 词条删除\n"



"|\n";

void print_menu_tile(char *title){
printf("=======================================\n");
printf("| %s\n", title);
printf("---------------------------------------\n");
}

void create_dic_by_input(dic *pNewDic){
int len;
printf(">请输入单词信息(注:最大长度是30个字符):\n");
printf("英文单词:"); scanf("%s",pNewDic->english_word);
printf("单词词性:"); scanf("%s",pNewDic->character_word);
printf("单词音标:"); scanf("%s",pNewDic->soundmark_word);
printf("单词注释:"); scanf("%s",pNewDic->chinese_word);
printf("单词短语:");
getchar();
gets(pNewDic->english_phrase);
len=strlen(pNewDic->english_phrase);
printf("你输入的英文短语长度为:%d,内容为:%s\n",len,pNewDic->english_phrase);
printf(">请输入例句信息(注:最大长度是80个字符):\n");
printf("单词例句:");
gets(pNewDic->example_sentence);
len=strlen(pNewDic->example_sentence);
printf("你输入的英文句子长度为:%d,内容为:%s\n",len,pNewDic->example_sentence);
}

dic* get_last_dictionary(dic *p){
if(p->next == NULL){
return p;
}
else{
get_last_dictionary(p->next);
}
}

void input1(){
void print_menu_main();
char continue_input = 'N';
dic *pLastDic = NULL;
dic *pNewDic = (dic *)malloc(sizeof(dic));
pNewDic->next = NULL;
create_dic_by_input(pNewDic);
if(head == NULL){
head = pNewDic;
}
else{
pLastDic = get_last_dictionary(head);
pLastDic->next = pNewDic;
}
printf("继续输入单词信息?(Y 继续, N 返回菜单)");
//getchar()
continue_input = getchar();
if (continue_input == 'n' || continue_input == 'N'){
print_menu_main();
}
else{
input1();
}
}

void print_table_buttom(){
printf("*-------------------------------------------------------------------------------*\n");
}

void print_table_out(dic *p){
//printf("*------------------------------*\n");
//printf("| 英文单词 |\n");
//printf("*------------------------------*\n");
printf("英文单词:%-30s\n",p->english_word);
//printf("*------------------------------*\n");
//printf("| 单词词性 |\n");
//printf("*------------------------------*\n");
printf("单词词性:%-30s\n",p->character_word);
//printf("*------------------------------*\n");
//printf("| 单词音标 |\n");
//printf("*------------------------------*\n");
printf("单词音标:%-30s\n",p->soundmark_word);
//printf("*------------------------------*\n");
//printf("| 单词注释 |\n");
//printf("*------------------------------*\n");
printf("单词注释:%-30s\n",p->chinese_word);
printf("单词短语:%-30s\n",p->english_phrase);
//printf("*------------------------------*\n");
//printf("| 单词例句 |\n");
//printf("*-------------------------------------------------------------------------------*\n");
printf("单词例句:%-s\n",p->example_sentence);

}

void output1(){
void print_menu_main();
dic *p = head;
while(p != NULL) {
print_table_out(p);
p = p->next;
}
print_table_buttom();
printf("按任意键返回菜单...\n");
getch();
print_menu_main();
}

void clear1(dic *p){
if(p==NULL){
return;
}

if(p->next ==NULL){
free(p);
}
else{
clear1(p->next);
free(p);
p = NULL;
}
}

void save1(){
void print_menu_main();
FILE *fp;
char filename[100];//这是一个输入文件名的字符数组
dic *p= head;
printf("请输入文件名:");
scanf("%s", filename);
fp=fopen(filename, "w+");
while(p != NULL){
fprintf(fp, "%s %s %s %s %s %s\n",p->english_word, p->character_word, p->soundmark_word, p->chinese_word,p->english_phrase,p->example_sentence);
//fgets(p->english_phrase,30,fp);
//fgets(p->example_sentence,80,fp);
p = p->next;
}
fclose(fp);
printf("保存成功!\n按任意键返回菜单..\n");
getchar();
getchar();
print_menu_main();
}

void exit1(){
clear1(head); /*释放整个链表*/
printf("链表已经释放!\n");
exit(0);
}

void load1(){
void print_menu_main();
void clear1(dic *p);
FILE *fp;
char filename1[100];
clear1(head); /*释放整个链表*/
int a;
dic *p= head, *q;
printf("请输入文件名:");
scanf("%s", filename1);
fp=fopen(filename1, "r");
dic *pLastDic = NULL;
printf("请输入单词短语的字符长度:\n");
scanf("%d",&a);
while(!feof(fp)){
dic *pNewDic = (dic *)malloc(sizeof(dic));
pNewDic->next = NULL;
fscanf(fp, "%s %s %s %s",pNewDic->english_word, pNewDic->character_word, pNewDic->soundmark_word, pNewDic->chinese_word);
//fread(p,sizeof(struct dictionary),1,fp);
//fread(pNewDic->example_sentence,sizeof(pNewDic->example_sentence),1,fp);
//getchar();
fgets(pNewDic->english_phrase,a,fp);
fgets(pNewDic->example_sentence,80,fp);//此处一定要注意到fgets函数的第二个参数:也就是英文例句的最大存储长度。
if(head == NULL){
head = pNewDic;
}
else{
pLastDic = get_last_dictionary(head);
pLastDic->next = pNewDic;
}
}
q = head;
while(q->next->next != NULL){
q = q->next;
}
free(q->next);
q->next = NULL;
fclose(fp);
printf("装载成功!\n按任意键返回菜单..\n");
getchar();
getchar();
print_menu_main();
}

void search1(){
void print_menu_main();
char english_word[30];
dic *p=head;
printf("请输入要查找的英文单词的全拼:\n");
scanf("%s",p->english_word);
while(strcmp(p->english_word,english_word)!=0&&p->next!=NULL){
p=p->next;
}
if(strcmp(p->english_word,english_word)==1){
printf("该单词的信息为:");
printf("英文单词:%-30s\n",p->english_word);
printf("单词词性:%-30s\n",p->character_word);
printf("单词音标:%-30s\n",p->soundmark_word);
printf("单词注释:%-30s\n",p->chinese_word);
printf("单词短语:%-30s\n",p->english_phrase);
printf("单词例句:%-s\n",p->example_sentence);
}
else
printf("查无此词!!");
printf("按任意键返回菜单...\n");
getchar();
getchar();
print_menu_main();
}

void search2(){
void print_menu_main();
char chinese_word[30];
dic *p=head;
printf("请输入要查找的英文单词的注释:\n");
scanf("%s",p->chinese_word);
while(strcmp(p->chinese_word,chinese_word)!=0&&p->next!=NULL){
p=p->next;
}
if(strcmp(p->chinese_word,chinese_word)==1){
printf("该单词的信息为:");
printf("英文单词:%-30s\n",p->english_word);
printf("单词词性:%-30s\n",p->character_word);
printf("单词音标:%-30s\n",p->soundmark_word);
printf("单词注释:%-30s\n",p->chinese_word);
printf("单词短语:%-30s\n",p->english_phrase);
printf("单词例句:%-s\n",p->example_sentence);
}
else
printf("查无此词!!");
printf("按任意键返回菜单...\n");
getchar();
getchar();
print_menu_main();
}

void (* menu_main_func[])()=
{
input1,
output1,
exit1,
save1,
load1,
search1,
search2,
/*update_record,
delete_record,
calculate,*/

};

void print_menu_main(){
int selected = 0;
system("cls");
print_menu_tile(TITLE);
printf(menu_main);
printf("=======================================\n");

while(!(selected >= 1 && selected <= MENU_MAIN_COUNT)){
printf(">请选择:");
scanf("%d",&selected);
if(selected >= 1 && selected <= MENU_MAIN_COUNT){
break;
}
printf("\n>输入错误!(注:请选择 1 - %d)\n", MENU_MAIN_COUNT);
}
menu_main_func[selected-1]();
}
int main()
{
print_menu_main();
}

...全文
141 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
生风虎 2014-12-05
  • 打赏
  • 举报
回复
赵四老师这个办法可以,我已经修改了读入文件部分:
fprintf(fp, "%s %s %s %s\n",p->english_word, p->character_word, p->soundmark_word, p->chinese_word);
fprintf(fp,"%s\n",p->english_phrase);
fprintf(fp,"%s\n",p->example_sentence);文件中基本信息在一行,凡是带空格的字符串单独一行
li4c 2014-12-04
  • 打赏
  • 举报
回复
l楼主能说清楚点吗?不知道是什么意思
赵4老师 2014-12-04
  • 打赏
  • 举报
回复
比如 scanf("%s",pNewDic->english_word); 改为 scanf("%29s",pNewDic->english_word);
一根烂笔头 2014-12-04
  • 打赏
  • 举报
回复
没有明白你的问题,你再具体点陈述下什么症状
  • 打赏
  • 举报
回复
你要告诉计算机,你什么时候将前一个字符串读完了,它什么时候才能去读第二个字符串
课程目标:学习Java语言中字符相关的知识、字符编码常识和正则表达式的使用,并完成案例前导课程:《Java工程师必学系列课程》前4部课程内容:本课程是《Java工程师必学系列课程》的第5部分,主要讲解Java语言中字符相关知识、字符编码常识和正则表达式的使用。本课程涉及的主要内容可以分为四部分:一、String、StringBuffer和StringBuilder类基本常识、基本原理和使用技巧二、字符编码常识三、Java语言正则表达式的详细语法和使用技巧四、实战案例课程说明:在开发Java程序的过程中,最常用的类莫过于字符相关的类。可以毫不夸张的说,任何一个Java程序,都离不开对字符保存和处理。很多学员对字符的理解只是处于比较粗浅的阶段。殊不知,如果对字符处理的不好,会影响到软件的运行效率。本课程专门讲解字符相关的知识,将从字符的存储方式、底层的运行方式等各方面深入讲解其中的原理和技巧。此外,对字符进行更高级的处理,又要用到正则表达式的相关知识。正则表达式广泛应用于各种与字符处理相关的场合。它是一套独立的语言系统,经过几十年的完善和发展,现在已经非常的强大,并且形成了国际标准。各种高级编程语言,都实现了自己的表达式引擎。本课程详细的讲解了Java语言中正则表达式的语法和使用技巧。掌握了正则表达式,对编程水平的提高有非常大的帮助!同时,本课程在最后一部分,安排了非常精彩的、完整的实战案例,通过实战的形式切实帮助学员提高解决具体问题的能力!预期效果:认真学习完本课程,学员可以掌握字符处理及正则表达式相关的系统知识,并能提高实际的编码水平。环境配置要求:学习本课程需安装JDK1.8或更高版本的JDK,以便程序能正确运行,建议使用IntelliJ IDEA 2019.1.2或更高版本的开发工具。    因有合作协议约束,《穆哥学堂》只提供PDF版本的课件!

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧