我的代码片段自动退出,返回值为0。 [英] My piece of code automatically exits with return value of 0.

查看:67
本文介绍了我的代码片段自动退出,返回值为0。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使此功能正常工作,使用此代码编译的项目没有任何问题,但是当我执行此功能时,它会要求新的记录名称而不是打印:

我们的记录有已成功添加

按任意回车进入主菜单!!!

并退出返回0.即使功能无效。请嘿嘿。

p.s.如果我使用scanf(%s,text)而不是gets(text),函数会起作用,但是在输入中非常冷的空格之后会跳过单词。 (%[^ \ n]不像gets(文本)那样工作。

  void  makerec()
{

system( cls );
FILE * fp;
char name [ 20 ];
printf( 输入新记录的名称:);
scanf(< span class =code-string> %s,name);
fp = fopen(name, w +);
printf( < span class =code-string>输入您的记录:\ n);
char text [ 1000 ];
得到(文本);
int length = strlen(text);
for ( int i = 0 ;我<长度; i ++)
{
fputc(text [i],fp);
}
fclose(fp);
printf( 您的记录已成功添加\ n \\ n \\ n);
printf( 按任意回车进入主菜单!!!);
getch();
main();
}

解决方案

有几个问题/问题。

为什么这个函数调用主要()?这是不寻常的。

当你说它返回0时,你的意思是当程序退出时?所有程序都返回0,除非你用exit(-2)指定不同的例子。



我知道许多(特别是较旧的)C书使用 scanf 获取和类似的控制台输入功能,或 fscanf fgets 等。用于文件输入。 请承诺我永远不会再使用它们



原因是所谓的缓冲区溢出。您有没有想过如果有人输入 50 个字符而不是 20 会发生什么?在高级语言中,输入将被截断,或者名称缓冲区将被调整大小以容纳数据。 C是一种中级语言,在这种情况下意味着你​​可以获得更多的权力来控制内存中实际发生的事情。通过过度填充缓冲区,用户可以输入机器代码,并通过一些操作可以执行他们的代码。



如果你看看大约75%的安全漏洞过去15年,他们就是因为缓冲区溢出漏洞就像这样。



我意识到这只是一个学习的小程序,但请注意我作为程序员的建议现在23年来打破这种习惯。至少,将此行更改为:



 scanf(%20s,姓名); 





更好的是,阅读这些安全版本函数,要求你指定缓冲区的大小。


我看到这里发生了什么。



Youi不需要从你的makerec函数调用main()。我想你正试图让菜单再次出现。



你的主程序需要循环,直到你要求选项7.给你一个退出(0)调用选项7然后在main中的代码周围添加一个无限循环。



更改



 system(cls); 
printf(******************************************* * \ n);





to



 for(;;)
{
system(cls);
printf(******************************************* * \ n);





然后在返回0之前添加右括号;



} 
返回0;
}





变为



} 
}
返回0;
}





*编辑*你应该按照上面关于安全版本的说明进行操作 - 我已经投了赞成票。


Can''t make this function work correctly, project with this code compiles with no problems, but when i execute this function it asks for a new record name and than prints:
our record has been succesfully added
Press any Enter to go to the main menu!!!
and than exits returning 0. even though function is void. please heeelp.
p.s. function works if i use scanf ("%s", text) instead of gets (text), buts skips words after very firs white space in input. (%[^\n] doesn''t work just like gets(text).

void makerec()
{

    system ("cls");
    FILE *fp;
    char name[20];
    printf("Enter the name of new record:");
    scanf ("%s", name);
    fp = fopen (name, "w+");
    printf ("Input your record bellow:\n");
    char text[1000];
    gets(text);
    int length = strlen (text);
    for (int i = 0; i < length; i++)
    {
        fputc(text[i], fp);
    }
    fclose (fp);
    printf("Your record has been succesfully added\n\n");
    printf("Press any Enter to go to the main menu!!!");
    getch();
    main();
} 

解决方案

There are a few questions/issues.
Why is this function calling main()? That''s unusual.
When you say it is returning 0, do you mean when the program exits? All programs return 0 unless you specify differently with exit(-2) for example.

I understand that many (especially older) C books use scanf, gets and similar functions for console input, or fscanf, fgets etc. for file input. PLEASE PROMISE ME YOU WILL NEVER USE THEM AGAIN.

The reason is what is called a buffer overflow. Have you ever wondered what would happen if someone typed in 50 characters instead of 20? In a high-level language the input would either be truncated or the name buffer would be resized to accomodate the data. C is a middle-level language, in this case meaning you get more power to control what actually happens in memory. By overfilling your buffer, a user could input machine code, and with some manipulation could execute their code.

If you look at about 75% of all security vulnerabilities in the last 15 years, they are due to buffer overflow exploits just like this.

I realize this is just a little program for learning, but please heed my advice as a programmer of 23 years to break this habit now. At a minimum, change this line to:

scanf ("%20s", name);



Even better, read about the safe version of these functions, which require that you specify the size of the buffer.


I see what is happening here.

Youi don''t need to call main() from your makerec function. I think you are trying to make the menu appear again.

Your main routine needs to loop until you ask for option 7. Given that you have an exit(0) call for option 7 already then add an infinite loop aroundthe code in main.

Change

system ("cls"); 
printf ("********************************************\n");



to

for(;;) 
{
    system ("cls"); 
    printf ("********************************************\n");



then add closing brace before the return 0;

 } 
return 0; 
}



becomes

 } 
}
return 0; 
}



*EDIT* And you should follow the instructions above about the safe versions - which I''ve upvoted.


这篇关于我的代码片段自动退出,返回值为0。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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