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

生风虎 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();
}

...全文
140 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
  • 打赏
  • 举报
回复
没有明白你的问题,你再具体点陈述下什么症状
  • 打赏
  • 举报
回复
你要告诉计算机,你什么时候将前一个字符串读完了,它什么时候才能去读第二个字符串

69,371

社区成员

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

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