执行编译二进制文件时出现分段错误 [英] Segmentation fault when compiled binary is executed

查看:107
本文介绍了执行编译二进制文件时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的代码很短。基本上我想在++模式下打开一个名为

指令的文件,然后将%s类型的数据写入其中,然后从同一个文件读取
并打印到屏幕。代码编译但是当我执行二进制文件并输入一些字符串以期望程序打印

它们为我屏幕时,会出现分段错误。任何人的想法?


#include< stdio.h>

#include< stdlib.h>


char * add;

char * show;

FILE *指令;


main()

{

scanf("%s"& add);

instruction = fopen(" instruction"," a ++") ;

fwrite(add,sizeof(add),1,instruction);

fscanf(指令,%s,& show);

printf("%s",show);

fcloseall();

}

解决方案

您应该首先为添加和显示分配内存。顺便说一句,你应该

关闭文件,然后用读模式打开它,打印出来的字符串




nae zot bba la说:

这里的代码很短。基本上我想在++模式下打开一个名为
指令的文件


没有这样的模式。您可能会想到a +。或者a + b。

然后将%s类型的数据写入其中,


但是没有这种类型。您可能会想到由第一个空字符终止的字符串序列,也称为字符串。 -

这不是数据类型,而是数据格式。

然后从同一个文件中读取
并打印到屏幕。代码编译但是当我执行二进制文件并输入一些字符串以期望程序打印它们以供我筛选时,会发生分段错误。有没有想法?

#include< stdio.h>
#include< stdlib.h>

char * add;
char * show;
FILE *指令;

main()


旁白:当不带参数时,main()的首选形式是:


int main(无效)


但这不是你的段错的原因。
{
scanf("%s"& add);




除了使用%s和scanf所涉及的危险之外,相当
除了你似乎忽略了scanf返回的非常有用的

值之外,还有一个问题是scanf不仅仅需要
需要char *而不是char **作为其参数,但也要求

指针指向足够的存储空间以接收

问题中的数据。这几乎可以肯定是你的seg错误的原因。好吧,无论如何,这是/ this / seg错误的原因是
。一旦你修好了,我完全期望你能得到其他人。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


thx。所以基本上就像


add = malloc(sizeof(a));

show = malloc(sizeof(s));


Hi, very short code here. Basically I want to open a file called
instruction in a++ mode and then write data of %s type to it, then read
from the same file and print to screen. The code compiles but when I
execute the binary and enter some string to expect the program to print
them for me to screen, segmentation fault occurs. Any ideas anyone?

#include <stdio.h>
#include <stdlib.h>

char *add;
char *show;
FILE *instruction;

main()
{
scanf("%s",&add);
instruction=fopen("instruction","a++");
fwrite(add,sizeof(add),1,instruction);
fscanf(instruction,"%s",&show);
printf("%s",show);
fcloseall();
}

解决方案

You should allocate memory for add and show first. BTW, you should
close the file and then open it with read mode to get the string
printed out.


nae zot bba la said:

Hi, very short code here. Basically I want to open a file called
instruction in a++ mode
There is no such mode. You may be thinking of "a+" or "a+b".
and then write data of %s type to it,
But there is no such type. You may be thinking of "sequence of char
terminated by the first null character", which is also known as "string" -
and this is not a data type but a data format.
then read
from the same file and print to screen. The code compiles but when I
execute the binary and enter some string to expect the program to print
them for me to screen, segmentation fault occurs. Any ideas anyone?

#include <stdio.h>
#include <stdlib.h>

char *add;
char *show;
FILE *instruction;

main()
Aside: the preferred form of main(), when taking no arguments, is:

int main(void)

but this is not the cause of your seg fault.
{
scanf("%s",&add);



Quite apart from the hazards involved in using %s with scanf, and quite
apart from the fact that you appear to be ignoring an extremely useful
value returned by scanf, there is also the problem that scanf not only
requires char * rather than char ** as its argument but also requires that
pointer to be pointing to sufficient storage to receive the data in
question. This is almost certainly the cause of your seg fault. Well, it''s
the cause of /this/ seg fault, anyway. Once you''ve fixed it, I fully expect
you to get others.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


thx. so basically somethign like

add=malloc(sizeof(a));
show=malloc(sizeof(s));


这篇关于执行编译二进制文件时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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