线/自动换行程序。 [英] Line/word wrap program.

查看:61
本文介绍了线/自动换行程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是第一次尝试使用行/自动换行实用程序。似乎工作

没关系,但缺少一些检查等等。


----------------- ----------------------------------------


#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< limits.h>


#define MAX 10000

/ * wrap.c根据指定的位置插入换行符代替空格

行长。输出文件名为{filename} .wrap。采用两个参数,

文件名和行长度。 * /


/ * Todo:需要弄清楚更大的文件可能需要什么样的内存

。文件类型检查? * /


void wordwrap(FILE * ifp,FILE * ofp,char * wl)

{

int c;


char buf [MAX];


int i,空格,计数,长度;


i = space = count = 0;


length = atoi(wl);


for(i = 0; i< MAX& &((c = getc(ifp))!= EOF); ++ i)

{

buf [i] = c;

}


for(i = 0; buf [i]!=''\ 0''; ++ i)

{

if((buf [i] ==''\ n''|| buf [i] ==''\ t'')&& buf [i-1] == ''\ n'')

count = space = 0;


if(buf [i] =='''')

space = i;


++ count;


if(count == length)

{

buf [space] =''\ n'';

count = i - space;

}

}


for(i = 0; buf [i]!= EOF; ++ i)

{

c = buf [i];

putc(c,ofp);

}

}


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

{

FILE * fp1 ;

FILE * fp2;


char * prog = argv [0];

char * filename1 = argv [1] ;

char filename2 [80];

char * wl = argv [2];

int i,l;


l = strlen(argv [1]);

for(i = 0;我< 75 ||我<升; ++ i)

filename2 [i] = argv [1] [i];

strcat(filename2," .wrap");


printf("包装%s at%s \ n",filename1,wl);

printf("输出文件将.wrap添加到输入文件名。\ n ;);


if(argc!= 3)

{

printf("用法:%s:文件名, wrap length \ n",prog);

返回EXIT_FAILURE;

}

else if((fp1 = fopen(filename1,") r"))== NULL)

{

fprintf(stderr,"%s:无法打开%s \ n",prog,filename1) ;

返回EXIT_FAILURE;

}

else if((fp2 = fopen(filename2," w"))== NULL)

{

fprintf(stderr,"%s:无法打开%s \ n",prog,filename2);

返回EXIT_FAILURE;

}

其他

{

wordwrap(fp1,fp2,wl);

fclose(fp1);

fclose(fp2);

}

if(ferror(fp) 2))

{

fprintf(stderr,"%s:错误写作%s \ n",prog,argv [3]);

返回EXIT_FAILURE;

}


返回EXIT_SUCCESS;

}


------------------------------------------------ -----------


我认为这很难看,所以欢迎评论和批评。


谢谢阅读。

-

电子邮件是wtlyman at olypen dot com

解决方案



2004年8月19日星期四,name写道:


这是第一次尝试使用行/自动换行实用程序。似乎工作
好的,但缺少一些检查的东西,等等。


当没有命令行参数运行时,它会出现段错误。

使用文件名和数字参数运行,它会产生

方式不正确的结果。尝试以下每个调用


%。/ a.out test.txt

%。/ a.out test.txt 10

%。/ a.out test.txt 20

%。/ a.out test.txt 21

%。/ a.out test.txt 100 <文件''test.txt''上的
包含两行20''x'。


xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx


我在/任何/这些情况下都没有得到正确答案!


代码你张贴的确非常难看。删除一些

那些空白行,将你的tabstop减少到合理的东西(例如,4),

修复明显的bug以及你在路上发现的任何其他内容,然后

再次尝试发布。


l = strlen(argv [1]);


这是一个明显的错误。

for(i = 0; i< 75 || i< l; ++ i) filename2 [i] = argv [1] [i];
strcat(filename2," .wrap");


这是你应该在路上找到的。

fprintf(stderr,%s:错误写作%s \ n,编, argv [3]);




这就是这个。

-Arthur,
http://www.contrib.andrew.cmu.edu/~a .. .e / usenetify2.c


2004-08-19,Arthur J. O'Dwyer< aj*@nospam.andrew.cmu。 EDU>写道:


2004年8月19日星期四,name写道:


这是第一次尝试使用行/自动换行实用程序。似乎工作
好的,但缺少一些检查的东西等。
当没有命令行参数运行时,它会段错误。




是的,我得到了。

当使用文件名和数字参数运行时,它会产生错误的结果。尝试以下每个调用

%。/ a.out test.txt


这应该会给你使用响应。对我来说。

%。/ a.out test.txt 10
%。/ a.out test.txt 20
%。/ a.out test.txt 21


Hmmmmmm .....没有遇到任何空格,只是''x''和''\ n''。好吧,我认为

这给了我关于检查二进制文件的线索。我会考虑

吧。

%。/ a.out test.txt 100


得到并修复它。谢谢。

文件''test.txt''包含两行20''x'。

xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
>
在这些案件中,我没有得到正确的答案!


您应该只获得一个参数的使用响应。

您发布的代码确实非常难看。删除一些空白行,将你的tabstop减少到合理的位置(例如,4),修复明显的bug以及你在路上发现的任何其他内容,然后再尝试发布。


不是谈论演示风格,而是代码本身。感谢你输入


l = strlen(argv [1]);



这是明显的错误。




好​​吧....

for(i = 0; i< 75 || i< l; ++ i)
filename2 [i] = argv [1] [i];
strcat(filename2," .wrap");


这是你应该找到的那个。




这里没有看到问题,除了这四行组应该遵循

argc检查。我认为没有argv [1],''strlen''试图访问

一些不存在的东西,这会导致段错误。所以

参数必须存在。如果它是一个emply数组,但是...必须

想一想。谢谢。

fprintf(stderr,"%s:错误写入%s \ n",prog,argv [3] );



这就是这个。




是的,应该是''filename2''而不是''argv [ 3] ''。修好了。谢谢。


正如我所说,需要进行大量不同类型的检查,

这是下一步,我想。 br />

感谢您的阅读。

-

电子邮件是wtlyman at olypen dot com




2004年8月20日星期五,姓名写道:


2004-08-19,Arthur J. O'Dwyer< ; aj*@nospam.andrew.cmu.edu>写道:

2004年8月19日星期四,name写道:


这是第一次尝试使用行/自动换行实用程序。似乎工作好了,但缺少一些检查等等。
[...]

%。/ a.out test。 txt
这应该会给你使用响应。对我有用。




Nope;它取消引用''main''中的空指针''wl'。

%。/ a.out test.txt 10 %。/ a.out test.txt 20
%。/ a.out test.txt 21



嗯......没有遇到任何空格,只是''x''和''\ n''。好的,我认为
这给了我关于检查二进制文件的线索。我会想到
它。




我不知道你在想什么,但在我的系统上每一个

在输出数据之后转储一堆零字节。一个主要问题

我注意到在找不到这个错误时:

for(i = 0; buf [i]!= EOF; ++ i){


此处,循环条件可能永远不会评估为false。这不是导致错误输出的错误,但是这是一个错误,它可能会在某些平台上导致无限循环。

%。/ a.out test.txt 100



得到并修复它。谢谢。




Gee,这是/不同/错误?疯!继续寻找;我肯定

你会发现更多的错误...

代码你发布的确非常难看。删除一些空白行,将你的tabstop减少到合理的位置(例如,4),修复明显的bug以及你在路上发现的任何其他内容,然后再尝试发布。



不是谈论演示风格,而是代码本身。




代码本身/令人难以置信的丑陋/。你可以解决这个问题的方法是从你的代码中删除多余的空行/。并且

使用一致的规则在您的代码中放置空格/。演示

在沟通中非常重要,如果不是b $ b通信,源代码就没有了。如果我只是想要一个/ do / something的程序,我会使用二进制可执行文件来
。当我看到源代码时,我希望它可以读取。谷歌上传D.E. Knuth对这个问题的看法;我非常赞/
非常赞同他这个话题。 :)


for(i = 0; i< 75 || i< l; ++ i)
filename2 [i] = argv [1] [i ];
strcat(filename2," .wrap");


这是你应该在路上找到的。



唐''在这里看到问题[...]




假设''strlen(argv [1])> 75''并从那里开始工作。请特别注意''filename2'的大小以及''strcat''的行为。


感谢阅读。




欢迎您。


-Arthur,

为期三天的周末


Here is a first attempt at a line/word wrapping utility. Seems to work
okay, but lacks some checking stuff, etc.

---------------------------------------------------------

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

#define MAX 10000

/* wrap.c inserts newlines in place of spaces according to specified
line length. Output filename is {filename}.wrap. Takes two arguments,
filename and line length. */

/* Todo: Need to figure out what sort of memory
the larger files might need. File type checking? */

void wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
int c;

char buf[MAX];

int i, space, count, length;

i = space = count = 0;

length = atoi(wl);

for(i = 0; i < MAX && ((c=getc(ifp)) != EOF); ++i)
{
buf[i] = c;
}

for ( i = 0; buf[i] != ''\0''; ++i)
{
if ((buf[i] == ''\n'' || buf[i] == ''\t'') && buf[i-1] == ''\n'')
count = space = 0;

if ( buf[i] == '' '')
space = i;

++count;

if ( count == length )
{
buf[space] = ''\n'';
count = i - space;
}
}

for ( i = 0; buf[i] != EOF; ++i)
{
c = buf[i];
putc(c, ofp);
}
}

int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;

char *prog = argv[0];
char *filename1 = argv[1];
char filename2[80];
char *wl = argv[2];
int i, l;

l = strlen(argv[1]);
for ( i = 0; i < 75 || i < l; ++i)
filename2[i] = argv[1][i];
strcat(filename2, ".wrap");

printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");

if (argc != 3)
{
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
}
else if ((fp1 = fopen(filename1, "r")) == NULL)
{
fprintf(stderr, "%s: can''t open %s\n", prog, filename1);
return EXIT_FAILURE;
}
else if ((fp2 = fopen(filename2, "w")) == NULL)
{
fprintf(stderr, "%s: can''t open %s\n", prog, filename2);
return EXIT_FAILURE;
}
else
{
wordwrap(fp1, fp2, wl);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

-----------------------------------------------------------

I presume it''s ugly, so comments and criticisms welcome.

Thanks for reading.
--
Email is wtallman at olypen dot com

解决方案


On Thu, 19 Aug 2004, name wrote:


Here is a first attempt at a line/word wrapping utility. Seems to work
okay, but lacks some checking stuff, etc.
When run with no command-line arguments, it segfaults.
When run with a filename and a numeric argument, it produces
way-incorrect results. Try each of the following invocations

% ./a.out test.txt
% ./a.out test.txt 10
% ./a.out test.txt 20
% ./a.out test.txt 21
% ./a.out test.txt 100

on a file ''test.txt'' containing two lines of 20 ''x''s each.

xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx

I don''t get the right answer in /any/ of those cases!

The code you''ve posted is indeed incredibly ugly. Remove some of
those blank lines, reduce your tabstop to something sensible (e.g., 4),
fix the obvious bug and any more you find along the way, and then
try posting again.

l = strlen(argv[1]);
This is the obvious bug.
for ( i = 0; i < 75 || i < l; ++i)
filename2[i] = argv[1][i];
strcat(filename2, ".wrap");
This is one you should find along the way.
fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);



So is this.
-Arthur,
http://www.contrib.andrew.cmu.edu/~a...e/usenetify2.c


On 2004-08-19, Arthur J. O''Dwyer <aj*@nospam.andrew.cmu.edu> wrote:


On Thu, 19 Aug 2004, name wrote:


Here is a first attempt at a line/word wrapping utility. Seems to work
okay, but lacks some checking stuff, etc.
When run with no command-line arguments, it segfaults.



Yep, I got that.
When run with a filename and a numeric argument, it produces
way-incorrect results. Try each of the following invocations

% ./a.out test.txt
That should give you the usage response. Did for me.
% ./a.out test.txt 10
% ./a.out test.txt 20
% ./a.out test.txt 21
Hmmmmmm..... Didn''t encounter any spaces, just ''x'' and ''\n''. Okay, I think
this gives me a clue about checking for binary files as well. I''ll think on
it.
% ./a.out test.txt 100
Got that and fixed it. Thanks.
on a file ''test.txt'' containing two lines of 20 ''x''s each.

xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx

I don''t get the right answer in /any/ of those cases!
You should have gotten the usage response for only one argument.
The code you''ve posted is indeed incredibly ugly. Remove some of
those blank lines, reduce your tabstop to something sensible (e.g., 4),
fix the obvious bug and any more you find along the way, and then
try posting again.
Not talking about style of presentation, but the code itself. Thanks for
your input, though.

l = strlen(argv[1]);



This is the obvious bug.



Okay....

for ( i = 0; i < 75 || i < l; ++i)
filename2[i] = argv[1][i];
strcat(filename2, ".wrap");


This is one you should find along the way.



Don''t see the problem here, except that this four line group should follow
the argc check. I suppose that with no argv[1], ''strlen'' tries to access
something that doesn''t exist, and that would cause a segfault. So the
argument does have to exist. If it is an emply array, however... have to
think on this. Thanks.

fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);



So is this.



Yep, that should be ''filename2'' instead of ''argv[3]''. Got it fixed. Thanks.

As I said, there needs to be a lot of different kinds of checking going on,
which is the next step, I guess.

Thanks for reading.
--
Email is wtallman at olypen dot com



On Fri, 20 Aug 2004, name wrote:


On 2004-08-19, Arthur J. O''Dwyer <aj*@nospam.andrew.cmu.edu> wrote:

On Thu, 19 Aug 2004, name wrote:


Here is a first attempt at a line/word wrapping utility. Seems to work
okay, but lacks some checking stuff, etc. [...]

% ./a.out test.txt
That should give you the usage response. Did for me.



Nope; it dereferences the null pointer ''wl'' in ''main''.

% ./a.out test.txt 10
% ./a.out test.txt 20
% ./a.out test.txt 21



Hmmmmmm..... Didn''t encounter any spaces, just ''x'' and ''\n''. Okay, I think
this gives me a clue about checking for binary files as well. I''ll think on
it.



I have no idea what you''re thinking of, but on my system each of these
dumps a bunch of zero bytes after the output data. One major problem
I noticed while idly looking for the bug:
for (i = 0; buf[i] != EOF; ++i) {
The loop condition may never evaluate to false, here. This is not
the bug that is causing the incorrect output, but it''s a bug which
might cause an infinite loop on some platforms.

% ./a.out test.txt 100



Got that and fixed it. Thanks.



Gee, that was a /different/ bug? Crazy! Keep looking; I''m sure
you''ll find plenty more bugs...

The code you''ve posted is indeed incredibly ugly. Remove some of
those blank lines, reduce your tabstop to something sensible (e.g., 4),
fix the obvious bug and any more you find along the way, and then
try posting again.



Not talking about style of presentation, but the code itself.



The code itself /is incredibly ugly/. The way you can fix this
problem is to remove the extra blank lines /from your code/. And
use a consistent rule to place whitespace /in your code/. Presentation
is very important in communication, and source code is nothing if not
communication. If I just wanted a program to /do/ something, I''d
use the binary executable. When I see source code, I expect it to
be readable. Google up D.E. Knuth''s opinions on the subject; I pretty
much agree with him on this topic. :)

for ( i = 0; i < 75 || i < l; ++i)
filename2[i] = argv[1][i];
strcat(filename2, ".wrap");


This is one you should find along the way.



Don''t see the problem here [...]



Suppose ''strlen(argv[1]) > 75'' and work from there. Pay particular
attention to the size of ''filename2'' and the behavior of ''strcat''.

Thanks for reading.



You''re welcome.

-Arthur,
three-day weekend


这篇关于线/自动换行程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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