显示功能不起作用 [英] Display function is not working

查看:103
本文介绍了显示功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于制作学生数据库。我被允许在学校做这个。

我面临的唯一问题是,我的显示功能不起作用。我已经执行了代码,并且在输出中显示功能未正确显示。即使在到达最后一行之后,它仍显示直到性别显示相同的单词然后它终止。

我能理解的是文件指针没有达到EOF或者它是否达到了feof函数无法检测到它。请帮忙。

谢谢!



我尝试了什么:



The below code was used to make a Student Database. I was given to make this in school.
The only problem that I am facing here is that, my display function is not working. I had executed the code and at output for the display function is not coming properly. Even after reaching the last line it still shows till gender showing the same word and then it terminates.
What I could understand by this was that the file pointer is not reaching EOF or if it is reaching feof function is unable to detect it. Please help.
Thank you!

What I have tried:

void
display ()
{
  ptr = fopen ("student.txt", "r");
  rewind (ptr);
  char temp[50];

  while (!feof (ptr))
    {
      printf ("Roll Number: ");
      fscanf (ptr, " %[^\n]", temp);
      printf ("%s\n", temp);

      printf ("Name: ");
      fscanf (ptr, " %[^\n]", temp);
      printf ("%s\n", temp);

      printf ("Address: ");
      fscanf (ptr, " %[^\n]", temp);
      printf ("%s\n", temp);

      printf ("Gender: ");
      fscanf (ptr, " %[^\n]", temp);
      if (temp[0] == 'm' || temp[0] == 'M')
        {
          printf ("Male\n");
          printf ("Father's Name: ");
          fscanf (ptr, " %[^\n]", temp);
          printf ("%s\n", temp);
          printf ("Father's Email Id: ");
          fscanf (ptr, " %[^\n]", temp);
          printf ("%s\n", temp);
        }

      if (temp[0] == 'f' || temp[0] == 'F')
        {
          printf ("Female\n");
          printf ("Mother's Name: ");
          fscanf (ptr, " %[^\n]", temp);
          printf ("%s\n", temp);
          printf ("Mother's mobile number: ");
          fscanf (ptr, " %[^\n]", temp);
          printf ("%s\n", temp);
        }
      printf ("\n\n");

    }
  fclose (ptr);
}

推荐答案

正如其他人所说:不要大喊大叫。使用所有大写字母被认为是在互联网上大喊大叫,粗鲁(使用所有小写字母被认为是幼稚的)。如果你想被认真对待,请使用适当的大写。



其次,代码很差:有很多重复的代码,很多重复,变量名称是为快速打字而不是可读性而设计的,它没有文档记录,......你应该改进很多东西。



但是你注意到的问题是它没有不会遇到EOF:它会,但你并不总是为它辩护。

你有几个循环只有它们才会退出从数据库中读取'\ n':如果最后一行未正确终止,则代码将失败。

既然你没有告诉我们发生了什么,那就可能是问题所在。我们无法分辨,因为我们没有您的数据文件,而且我(对于一个)无论如何都不会在我的机器上运行该代码。



所以它取决于你。

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


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
As the others have said: DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

Secondly, that code is pretty poor: there is a lot of repeated code, a lot of repetition, the variable names are designed for quick typing instead of readbility, it's undocumented, ... lots of things you should improve.

But the problem you've noticed is that it doesn't encounter EOF: it will, but you aren't always testign for it.
You have several while loops which only ever exit when they read a '\n' from the DB: so if the last line isn't terminated correctly then your code will fail.
Since you don't tell us what does happen, it's possible that that is the problem. We can't tell, because we don't have your data file, and I (for one) wouldn't run that code on my machines anyway.

So, its going to be up to you.
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天全站免登陆