C新手:运行程序时出现应用程序错误 [英] New to C: Getting Application error when running program

查看:63
本文介绍了C新手:运行程序时出现应用程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C编程语言的新手,但已经在
Cobol编程了10多年。当我编译下面的代码时,

编译干净但我在Windows XP和

Win2K下都出现了应用程序错误。


#include< stdio.h>

#include< ctype.h>


char title [] =" Year End Report";


char work_buffer [51];


main()

{

strcpy (work_buffer,title);

strncenter(work_buffer,50);

printf("%s \ n",work_buffer);

strljust(work_buffer);

printf("%s \ n",work_buffer);

strrjust(work_buffer);

printf ("%s \ n",work_buffer);

}


/ *左对齐字符串* /

strljust(str)

char * str;

{

int len;


len = strlen(str);

while(isspace(str)!= 0){

memmove(str,str + 1,len);

str [len] ='''';

}

}


/ *反转一个字符串* /

strrev(str)

char * str;

{

char ch;

char * end;


end = str + strlen(str) - 1;


while(str <结束){

ch = * end;

* end-- = * str;

* str ++ = ch;

}

}


/ *右对齐字符串* /

strrjust(str)

char * str;

{

strrev(str);

strljust(str);

strrev(str);

}


/ *截断到非白色字符* /

strtrunc(str)

char * str;

{

char * end;


end = str + strlen( str) - 1;


while((* str!= 0)&&(isspace(* end)!= 0)){

* end-- = 0;

}

}


/ *用空格填充len的字符串* /

struntrunc(str,len)

char * str;

int len;

{

while(strlen(str)< len){

strcat(str,'''');

}

}


/ *居中一个字符串* /

strcenter(str)

char * str;

{

strncenter(str,strlen(st r));

}


/ *将一个字符串置于宽度范围内* /

strncenter(str,width)

char * str;

int width;

{

int non_blank_len,padding;

strtrunc(str);

strrev(str);

strtrunc(str);

non_blank_len = strlen(str) ;


padding =(width - non_blank_len)/ 2;


struntrunc(str,padding);

strrev(str);

struntrunc(str,width);

}


好​​像在struntrunc中炸毁功能,但我不能理解为什么。我知道这个程序试图取一个字符串,

将它放在中心位置,左对齐它并证明它是正确的。


关于我如何获得的任何想法这个程序要运行吗?


我正在使用Open Watcom C / C ++ 1.4,但是当我用$ 3
尝试使用OW 1.3时,我遇到了同样的问题。 />

谢谢

I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.

#include <stdio.h>
#include <ctype.h>

char title[] = "Year End Report";

char work_buffer[51];

main()
{
strcpy(work_buffer,title);
strncenter(work_buffer,50);
printf("%s\n",work_buffer);
strljust(work_buffer);
printf("%s\n",work_buffer);
strrjust(work_buffer);
printf("%s\n",work_buffer);
}

/* Left justify a string */
strljust(str)
char *str;
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
memmove (str,str + 1,len);
str[len] = '' '';
}
}

/* Reverse a string */
strrev(str)
char *str;
{
char ch;
char *end;

end = str + strlen(str) - 1;

while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}

/* Right justify a string */
strrjust(str)
char *str;
{
strrev(str);
strljust(str);
strrev(str);
}

/* Truncate to last non-white character */
strtrunc(str)
char *str;
{
char *end;

end = str + strlen(str) - 1;

while ((*str != 0) && (isspace (*end) != 0)) {
*end-- = 0;
}
}

/* Pad a string for len with spaces */
struntrunc(str,len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,'' '');
}
}

/* Center a string */
strcenter(str)
char *str;
{
strncenter (str, strlen(str));
}

/* Center a string within width */
strncenter(str,width)
char *str;
int width;
{
int non_blank_len, padding;

strtrunc (str);
strrev (str);
strtrunc (str);
non_blank_len = strlen (str);

padding = (width - non_blank_len) / 2;

struntrunc (str,padding);
strrev (str);
struntrunc (str,width);
}

It seems to be blowing up in the struntrunc function, but I can''t
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.

Any ideas on how I can get this program to run?

I am using Open Watcom C/C++ 1.4 but I had the same problem when I
tried it with OW 1.3.

Thanks

推荐答案

确保没有缓冲区溢出。尝试使用52或一些

更多字符的缓冲区。


一个想法:-)


Lothar

Be sure, there will be no buffer overrun. Try a buffer with 52 or some
more chars.

Yust a thought :-)

Lothar


2006年1月17日14:47:28 -0800,在comp.lang.c,Mike Polinske

<熔点****** @ execpc.com>写道:
On 17 Jan 2006 14:47:28 -0800, in comp.lang.c , "Mike Polinske"
<mp******@execpc.com> wrote:
编译干净但我在Windows XP和Win2K下都遇到了应用程序错误。


在编译器上将警告级别调高至最大值。有几个

编码错误应该提醒你。

#include< stdio.h>
#include< ctype.h>

char title [] =" Year End Report" ;;
char work_buffer [51];


这些都不需要全局,它们可以在main里面。

main()
{
strcpy(work_buffer,标题);


你应该在这里得到你的编译器的警告 - 你需要在范围内输入strcpy()的

声明,通常由#include提供

< string.h>

strncenter(work_buffer,50);


你在这里遇到同样的问题。在main开始之前为strncenter()

放置一个原型。


另外,命名函数str后跟一个小写的一个坏主意

信。这些名称是为编译器保留的。 strCenter将是

ok tho,或str_center。

printf("%s \ n",work_buffer);
strljust(work_buffer);
printf("%s \ n",work_buffer);
strrjust(work_buffer);
printf("%s \ n",work_buffer);
}

/ *左对齐字符串* /
strljust(str)
char * str;


这种功能定义是20年过时了。正常的

现代风格是

void strladjust(char * str)


这里有两个不同之处 - 首先是你的原始函数是

隐式返回一个int,这是允许但不推荐使用;

其次,你应该在parens中包含参数类型。


另外,函数必须返回一些东西,除非它声明为void。

你的声明为返回一个int,但是没有返回任何东西。

哎呀!

{
int len;

len = strlen(str);
while(isspace(str)!= 0){


isspace()需要一个字符 - str是一个字符串。

你的意思是* str。 ?

memmove(str,str + 1,len);
str [len] ='''';
}
}

/ *反转一个字符串* /
strrev(str)
char * str;


此名称可能与内置库函数冲突。这个

实际编译了吗?

struntrunc(str,len)
char * str;
int len;
{
while(strlen(str)< len){
strcat(str,'''');
compiles clean but I get an application error both under Windows XP and
Win2K.
Turn warninglevels up to max on your compiler. There are several
coding errors that ought to have been alerted to you.
#include <stdio.h>
#include <ctype.h>

char title[] = "Year End Report";
char work_buffer[51];
no need for these to be globals, they can be inside main.
main()
{
strcpy(work_buffer,title);
you should get a warning from your compiler here - you need a
declaration of strcpy() in scope, usually provided by #include
<string.h>
strncenter(work_buffer,50);
you have the same problem here. Put a prototype for strncenter()
before the start of main.

Also, its a bad idea to name functions str followed by a lower case
letter. Such names are reserved for the compiler. strCenter would be
ok tho, or str_center.
printf("%s\n",work_buffer);
strljust(work_buffer);
printf("%s\n",work_buffer);
strrjust(work_buffer);
printf("%s\n",work_buffer);
}

/* Left justify a string */
strljust(str)
char *str;
this style of function definition is 20 years out of date. The normal
modern style is
void strladjust(char *str)

There''s two differences here - firstly your original function was
implicitly returning an int, which is permissible but deprecated ;
secondly, you should include the parameter type in the parens.

Also, a function must return something, unless its declared void.
Yours is declared as returning an int, but doesn''t return anything.
Oops!
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
isspace() expects a single character - str is a string.
Did you mean *str. ?
memmove (str,str + 1,len);
str[len] = '' '';
}
}

/* Reverse a string */
strrev(str)
char *str;
this name may conflict with a builtin library function. Did this
actually compile?
struntrunc(str,len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,'' '');




strcat要求第二个参数为a字符串,而不是字符。

strcat(str,"");


顺便说一句,人们可以更简单地完成整个操作

sprintf()


Mark McIntyre

-



strcat requires the 2nd parameter to be a string, not a char.
strcat(str, " ");

by the way, one can do this entire operation much more simply with
sprintf()

Mark McIntyre
--


Mike Polinske写道:
Mike Polinske wrote:
我是C编程语言的新手,但已经在Cobol编程了10多年。当我编译下面的代码时,它编译干净但我在Windows XP和Win2K下都遇到了应用程序错误。

#include< stdio.h>
#include< ctype.h>

char title [] =" Year End Report" ;;

char work_buffer [51];

main()


Main返回一个int,因为你没有使用可选参数,所以

最好明确说明。


int main(无效)

{
strcpy(work_buffer,title);
strncenter(work_buffer,50);
printf( "%s \ n",work_buffer);
strljust(work_buffer);
printf("%s \ n",work_buffer);
strrjust(work_buffer);
printf("%s \ n",work_buffer);


Main返回一个int,所以返回一个int!

返回0;

}

/ *左对齐字符串* /
strljust(str)
char * str;


替换您正在学习的任何资源。这种风格在1989年的
中出现了。使用原型样式


strljust(char * str)


另外,在main之前添加一个原型定义,以便验证参数所需的编译器是




此外,函数名称以str开头,后跟小写信是

保留,所以你应该选择一个不同的名字。可能是str_ljust。

{
int len;

len = strlen(str);
while(isspace(str)!= 0){


为什么不简单地

而(isspace(str))



memmove(str, str + 1,len);
str [len] ='''';


C数组的索引是0而不是1,所以这是你的问题。你是

覆盖字符串的空终止而不是最后的

非空字符。

}


另外,为什么不弄清楚你需要多远才能移动它然后一次性移动它所有的

方式?我知道我们说不要优化,直到你知道你有一个

的问题,但是没有必要过分使它明显地使用它b / b
效率低下!

}

/ *反转一个字符串* /
strrev(str)
char * str;


和以前一样的评论。

{
char ch;
char * end;

结束= str + strlen(str) - 1;

while(str< end){
ch = * end;
* end-- = * str;
* str ++ = ch;
}

/ *右对齐字符串* /
strrjust(str)
char * str;


相同的评论。

{
strrev(str);
strljust(str);
strrev(str) ;
}
/ *截断到非白色字符* /
strtrunc(str)
char * str;


再次发表评论。


< snip未选中附加代码>

似乎爆炸了struntrunc函数,但我不能理解为什么。我知道这个程序试图取一个字符串,然后将它放在中心位置,左对齐并正确地证明它。

关于如何让这个程序运行的任何想法?


首先修复我(以及其他任何响应者)指出的内容,

然后查看代码以查找任何其他类似错误。我已经给了你一两分钟的错误,你可能已经犯了一两次错误

不止一次。

我我正在使用Open Watcom C / C ++ 1.4但是当我用OW 1.3尝试它时我遇到了同样的问题。
I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.

#include <stdio.h>
#include <ctype.h>

char title[] = "Year End Report";

char work_buffer[51];

main()
Main returns an int, and as you are not using the optional parameters it
is better to say so explicitly.

int main(void)
{
strcpy(work_buffer,title);
strncenter(work_buffer,50);
printf("%s\n",work_buffer);
strljust(work_buffer);
printf("%s\n",work_buffer);
strrjust(work_buffer);
printf("%s\n",work_buffer);
Main returns an int, so return an int!
return 0;
}

/* Left justify a string */
strljust(str)
char *str;
Replace whatever resource you are learning from. That style went out in
1989. Use the prototype style instead

strljust(char *str)

Also, add a prototype definition before main so that the compiler is
required to validate the parameters.

Also, function names starting str followed by a lower case letter are
reserved, so you should pick a different name. Possibly str_ljust.
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
Why not simply
while (isspace(str))
?
memmove (str,str + 1,len);
str[len] = '' '';
C arrays are indexed from 0 not from 1, so this is your problem. You are
overwriting the null termination of the string instead of the last
non-null character.
}
Also, why not work out how far you need to move it then move it all the
way in one go? I know we say don''t optimise until you know you have a
problem, but there is no need to go overboard and make it obviously
inefficient!
}

/* Reverse a string */
strrev(str)
char *str;
Same comments as before.
{
char ch;
char *end;

end = str + strlen(str) - 1;

while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}

/* Right justify a string */
strrjust(str)
char *str;
Same comments.
{
strrev(str);
strljust(str);
strrev(str);
}

/* Truncate to last non-white character */
strtrunc(str)
char *str;
Same comments again.

<snip unchecked additional code>
It seems to be blowing up in the struntrunc function, but I can''t
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.

Any ideas on how I can get this program to run?
First fix everything I (and anyone else who responds) have pointed out,
then go through the code looking for any other similar errors. I''ve
you''ve made a given class of mistakes once yoy may well have made it
more than once.
I am using Open Watcom C/C++ 1.4 but I had the same problem when I
tried it with OW 1.3.




在comp.lang.c上编译器是无关紧要的,因为我们只处理

标准C.然而,你的问题看起来与标准C相比,而不是编译器,所以它完全适用于comp中的主题。 lang.c

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,这是真实的,我读了它。



On comp.lang.c the compiler is irrelevant since we only deal with
standard C. However, your problem looks to be with standard C rather
than the compiler, so it is perfectly on topic in comp.lang.c
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于C新手:运行程序时出现应用程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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