如何正确修改它 [英] How do I modify it correctly

查看:60
本文介绍了如何正确修改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C编写的字典。这是我写的用于修改单词的部分。我输入2个单词,例如:你好,然后我想用另一个单词修改hi,但单词hi不会改变,我的文件将有一个新单词或另一个单词将替换单词hello。



我尝试过:



I have a dictionary written in C. Here is my section I write to modify a word. I input 2 words, example: hello then I want to modify "hi" by another word but word "hi" does not change, my file will have a new word or another word will replace word "hello".

What I have tried:

Label21: system("cls");

                    fp=fopen("dictionary.dat", "rb+");
                    printf("Enter the word to modify : ");
                    fflush(stdin);
                    gets(word);
                    while((fread((char *)&dictionary, sizeof(dictionary), 1, fp))==1){
                        if(strcmp(word, dictionary.add_word)==0){
                            printf("\nWord: %s", dictionary.add_word);
                            printf("\nSpell: %s",dictionary.add_spelling);
                            printf("\nMean: %s", dictionary.add_mean);
                            printf("\n");
                            printf("Do you want to modify this record : ? (Y/N)");
                            fflush(stdin);
                            scanf("%c", &ch);
                            fseek(fp, sizeof(dictionary), 0);
                            if(ch=='Y'|| ch=='y'){
                                printf("Enter the word: \n");
                                fflush(stdin);
                                gets(dictionary.add_word);

                                printf("Enter the the spell: \n");
                                fflush(stdin);
                                gets(dictionary.add_spelling);

                                printf("Enter the the mean: \n");
                                fflush(stdin);
                                gets(dictionary.add_mean);
                                fwrite((char *)&dictionary, sizeof(dictionary), 1, fp);
                                    printf("Record Modified");
                            }
                            else
                                printf("No modifications were made");
                    fclose(fp);
                    }
                    }

                    Label22:
                       printf("\n\n1 -> Edit more words...");
                       printf("\n2 -> Back to Main Menu");
                       printf("\n3 -> Exit Program\n");
                       scanf("%d",&shift);
                       if(shift==1)
                        goto Label21;
                       if(shift==2)
                        goto Label0;
                       if(shift==3)
                        break;
                       if(shift!=1&&2&&3){
                        printf("\nWrong!<^_^>! Please enter number(1->3)! Thank you!");
                        goto Label22;
                       }

推荐答案

首先,摆脱所有标签并转到语句,并用结构化代码替换它们:循环, if ... else if else else< / code,< code> switch ,等等。然后忘记 goto 甚至存在五年 - 到那时你将很好地了解何时适当和必要的使用它。

在像这样的简单初学者代码中使用 goto 是一个结构严重的意大利面条代码,很难理解,也很难修改。



然后你可以再次测试,也许开始修复你注意到的问题。但它在很大程度上取决于你的数据(以及你没有显示的其他代码),我们无法访问它。

所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
For starters, get rid of all the labels and goto statements, and replace them with structured code: loops, if ... else if ... else</code, <code>switch, and so forth. Then forget goto even exists for five years - by which time you will have a good idea of when it is appropriate and necessary to use it.
Using goto in trivial beginner code like this is a recipe for badly structured, spaghetti code that is difficult to understand and worse to modify.

Then you can test this again, and perhaps start to fix the problems you have noticed. But it's going to depend on your data to a large extent (as well as other code your don't show) and we have no access to that.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于如何正确修改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆