段故障 [英] seg fault

查看:73
本文介绍了段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将这个程序放在一起似乎做了我想做的事情而没有添加

错误检查。如果我只是运行程序,它会产生一个

分段错误。如果我添加适当的值说43.56就没关系了。

有人看到了什么问题吗?我有一个c99编译器。谢谢。


#include< stdio.h>

#include< stdlib.h>


int main(int argc,char * argv []){

FILE * fp;

double x;

x = strtod(argv [1 ],NULL);

if(argc!= 2){

fprintf(stderr,usage error \\\
);

退出(EXIT_FAILURE);

}

fp = fopen(" data"," a");

ftell(fp) ;

fseek(fp,sizeof(double),SEEK_CUR);

fprintf(fp,"%。2f \ n",x);

fclose(fp);

}

I have put together this program that seems to do what I want without
error checking added yet. If I just run the program it produces a
segmentation fault. If I add the proper value say 43.56 it''s fine. Does
anyone see what''s wrong ? I have a c99 compiler. Thanks.

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

int main ( int argc, char *argv[] ) {
FILE*fp;
double x;
x=strtod(argv[1],NULL);
if (argc !=2) {
fprintf(stderr,"usage error\n");
exit(EXIT_FAILURE);
}
fp=fopen( "data", "a");
ftell(fp);
fseek(fp,sizeof(double),SEEK_CUR);
fprintf(fp,"%.2f\n",x);
fclose(fp);
}

推荐答案

" Bill Cunningham" < no **** @ nspam.com在消息新闻中写道:
"Bill Cunningham" <no****@nspam.comwrote in message news:

我把这个程序放在一起似乎做了我想做的事情没有

错误检查已添加。如果我只是运行程序,它会产生一个

分段错误。如果我添加适当的值说43.56就没关系了。

有人看到了什么问题吗?我有一个c99编译器。谢谢。


#include< stdio.h>

#include< stdlib.h>


int main(int argc,char * argv []){

FILE * fp;

double x;

x = strtod(argv [1 ],NULL);
I have put together this program that seems to do what I want without
error checking added yet. If I just run the program it produces a
segmentation fault. If I add the proper value say 43.56 it''s fine. Does
anyone see what''s wrong ? I have a c99 compiler. Thanks.

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

int main ( int argc, char *argv[] ) {
FILE*fp;
double x;
x=strtod(argv[1],NULL);



如果argc为1,则将空指针传递给strtod,这很可能导致

段错误。

If argc is 1 this passes a null pointer to strtod, which could well cause a
segfault.


>

if(argc!= 2){

fprintf(stderr," usage error) \ n");

退出(EXIT_FAILURE);

}

fp = fopen(" data"," a") ;
>
if (argc !=2) {
fprintf(stderr,"usage error\n");
exit(EXIT_FAILURE);
}
fp=fopen( "data", "a");



您还需要在此处检查fp是否为null。

You also need to check fp for nullness here.


>

ftell(fp);

fseek(fp,sizeof(double),SEEK_CUR) ;

fprintf(fp,"%。2f \ n",x);

fclose(fp);

}

>
ftell(fp);
fseek(fp,sizeof(double),SEEK_CUR);
fprintf(fp,"%.2f\n",x);
fclose(fp);
}



-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm




" Malcolm McLean" < re ******* @ btinternet.com写了留言

新闻:7u *********************** ******* @ bt.com ...

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:7u******************************@bt.com...

" Bill Cunningham" < no **** @ nspam.com在消息新闻中写道:
"Bill Cunningham" <no****@nspam.comwrote in message news:

>我把这个程序放在一起似乎做了我想做的事情而没有添加错误检查。如果我只是运行该程序,它会产生一个
分段错误。如果我添加适当的值说43.56就没关系了。有没有人看到什么是错的?我有一个c99编译器。谢谢。

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

int main(int argc,char * argv []) {
FILE * fp;
double x;
x = strtod(argv [1],NULL);
> I have put together this program that seems to do what I want without
error checking added yet. If I just run the program it produces a
segmentation fault. If I add the proper value say 43.56 it''s fine. Does
anyone see what''s wrong ? I have a c99 compiler. Thanks.

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

int main ( int argc, char *argv[] ) {
FILE*fp;
double x;
x=strtod(argv[1],NULL);



如果是argc是1这传递一个空指针到strtod,这很可能导致

a段错误。

If argc is 1 this passes a null pointer to strtod, which could well cause
a segfault.



这可能就在这里。只运行程序argv [0]将执行

,不给出任何参数,就像我不会是NULL一样。

This may be where it is. Just running the program argv[0] would be
execution, giving no arguments like I didn''t would be NULL.


>>
if(argc!= 2){
fprintf(stderr,"使用错误\ n");
退出(EXIT_FAILURE);
}
fp = fopen(" data"," a");
>>
if (argc !=2) {
fprintf(stderr,"usage error\n");
exit(EXIT_FAILURE);
}
fp=fopen( "data", "a");



您还需要在此处检查fp是否为null。

You also need to check fp for nullness here.


>>
ftell(fp);
fseek(fp,sizeof(double),SEEK_CUR);
fprintf (fp,%。2f \ n,x);
fclose(fp);
}

>>
ftell(fp);
fseek(fp,sizeof(double),SEEK_CUR);
fprintf(fp,"%.2f\n",x);
fclose(fp);
}






Bill Cunningham写道:
Bill Cunningham wrote:

我把这个似乎做我想要的程序放在一起

没有

错误检查已添加。如果我只是运行程序,它会产生一个

分段错误。如果我添加适当的值说43.56就没关系了。

有谁看到什么是错的?我有一个c99编译器。谢谢。


#include< stdio.h>

#include< stdlib.h>


int main(int argc,char * argv []){

FILE * fp;

double x;

x = strtod(argv [1 ],NULL);

if(argc!= 2){

fprintf(stderr,usage error \\\
);

退出(EXIT_FAILURE);

}
I have put together this program that seems to do what I want
without
error checking added yet. If I just run the program it produces a
segmentation fault. If I add the proper value say 43.56 it''s fine.
Does anyone see what''s wrong ? I have a c99 compiler. Thanks.

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

int main ( int argc, char *argv[] ) {
FILE*fp;
double x;
x=strtod(argv[1],NULL);
if (argc !=2) {
fprintf(stderr,"usage error\n");
exit(EXIT_FAILURE);
}



此测试应在使用strtod进行转换之前进行。否则

strtod传递一个空指针,这就是导致分段

错误的原因。

This test should come before the conversion with strtod. Otherwise
strtod is passed a null pointer, which is what causes the segmentation
fault.


fp = fopen(data,a);

ftell(fp);
fp=fopen( "data", "a");
ftell(fp);



调用ftell并丢弃退货的目的是什么?

What is the purpose of calling ftell and discarding the return?


fseek(fp,sizeof(双),SEEK_CUR);
fseek(fp,sizeof(double),SEEK_CUR);



当您在追加模式下打开文件时,文件位置指示符已经位于文件可能包含的任何内容之后。你现在想要寻找超出文件末尾的sizeof(双)字节的


将返回错误。你希望通过什么来实现你想要做什么呢?

如此?

When you open a file in the append mode the file position indicator is
already positioned after any contents the file may have. You are now
trying to seek sizeof(double) bytes beyond the end of the file, which
will return an error. Exactly what do you hope to accomplish by doing
so?


fprintf(fp,"%。2f \ n",x );

fclose(fp);

}
fprintf(fp,"%.2f\n",x);
fclose(fp);
}


这篇关于段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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