臭名昭着的^ Z问题 [英] The infamous ^Z problem

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

问题描述

我已经在常见问题解答和谷歌上冲了大约一个星期而且还没有完全找到这个问题。


我有一个定期更改的文件,每隔一段时间

^ Zs将出现在文件中,原因我不想进入。我需要

摆脱那些^ Z'并且需要通过C代码来完成它,因为它是我唯一可以处理的工具

文件大小。


所以我编写了一些代码,在一个平台上试用了 - 它工作得很好,

它没有那么好用在另一个,我试图理解为什么。我是b $ b尽我所能代码标准,但也许这就是我失败的地方。


#include< stdio.h>

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

{

FILE * infile,* outfile;

int c ; / *从FAQ * /

中选择了如果((infile = fopen(argv [1]," rb")== NULL)/ *选择二进制部分
来自这个谷歌群组的
* / b $ b $

printf(无法打开文件\ n);

退出( 1);

}

if((outfile = fopen(" Clean_file"," w +"))== NULL)

{

printf(无法打开输出文件\ n);

退出(1);

}

while((c = fgetc(infile))!= EOF)

{

if(c == 0x1a)/ *这就是我'遇到问题* /

/ * if(c ==''\'0x1a'')这会因编译器错误而失败 - 多于一个

字符定义为键入char * /

{

c =''_''; / *用无害的东西替换坏控制字符* /

}

fputs(c,outfile);

}

fclose(infile);

fclose(outfile);

}

是的,这是一个非常原始的代码,不在我进入并优化处理输入

文件的方式之前,我更感兴趣的是获得

基础知识。这在xlC和HP的ANSI C编译器上编译。


在第一个处理^ Z的if语句中,程序没有检测到

。文件中的控制字符,在第二个语句中编译器

抱怨语法。如果我将c设置为类型转换字符,它会找到控件

个字符,替换它们,但随后将EOF字符吹走并核实

该文件。


我怀疑它是我定义c == \0x1a的方式是

在这里引导我误入歧途。我无法在

上找到任何良好的一致文档,如何在c代码或字符串/字符中表示十六进制或八进制

操作。

解决方案

在文章< Mq *************************** ***@comcast.com>,

Eigenvector< m4 ******** @ yahoo.comwrote:


if(c == 0x1a)/ *这就是我遇到问题的地方* /



/ * if(c ==''\ 0x1a' ')这会因编译错误而失败 - 为char * /
类型定义多个
字符。
不要使用值周围的单引号。 ..

0x1a是一个int而不是一个char

c也是一个int。


HTH

-

Mitch

te ************ @ sand-hill.freeserve.co.uk
www.sand-hill.freeserve.co.uk/terminal_crazy


Terminal Crazy< Te * ***********@sand-hill.freeserve.co.ukwrites:


文章< Mq ******* ***********************@comcast.com>,

Eigenvector< m4 ******** @ yahoo.comwrote:


> if(c == 0x1a)/ *这是我遇到问题的地方* /


/ * if(c ==''\ 0x1a'')这会因编译错误而失败 - 为char * / 字符/>

不要使用值周围的单引号...

0x1a是一个int而不是一个字符

c是一个int也。



你在字符中使用错误的十六进制转义语法

literal。你想要''\ x1a'',而不是''\ 0x1a''。 0x1a(没有引号

标记)也可以工作,但''\ x1a''更清楚你用一个角色处理

。 />

-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


5月23日,12日:34 pm,Eigenvector < m44_mas ... @ yahoo.comwrote:


if((infile = fopen(argv [1]," rb")== NULL)/ *从这个谷歌小组中挑选了二元组件

* / b / b
{

printf(无法打开文件\ n);

退出(1);

}

if((outfile = fopen(" Clean_file"," w +"))= = NULL)



开放模式应该是w + b。你想以同样的方式写它



if(c == 0x1a)/ *这是我遇到问题的地方* /

/ * if(c ==''\ 0x1a'')这会因编译错误而失败 - 为char * /
类型定义的多个

字符



这个常数中有四个字符:''\''',''x'',

''1''和''a''。我想你的意思是''\\ \\ x1a'',a虽然

未注释的代码也是正确的并且做同样的事情。


是的,它是一个非常原始的代码,但我''我更有兴趣在进入之前获得

基础知识并优化处理输入的方式

文件。这将编译在xlC和HP的ANSI C编译器上。



出于兴趣,您是如何计划优化

的呢? (我想你会发现在一个区块内阅读

一次不会获得任何东西)。


In第一个if语句处理^ Z,程序没有检测到文件中的控制字符,在第二个语句中编译器

抱怨语法。如果我将c设置为类型转换字符,它会找到控件

个字符,替换它们,但随后将EOF字符吹走并且核对文件的



您发布的代码似乎不可能
找到0x1a字符。必须有一些其他的

问题,例如这不是你真正的代码,或者

非二进制输出正在调整。


我找不到任何关于确切的一致文档

如何在c代码或字符串/字符中表示十六进制或八进制

操作。



尝试使用C标准,或C编程语言

by Kernighan&里奇。


I''ve been surfing the FAQ and Google for about a week and haven''t quite
figured out this one.

I have a file that changes on a periodic basis and every once and a while
^Zs will appear in the file for reasons I don''t want to get into. I need to
get rid of those ^Z''s and need to do it via a C code as it is the only tool
available to me that can handle the file size.

So I cooked up some code, tried it out on one platform - and it works great,
it doesn''t work so great on another and I am trying to understand why. I
did my best to code standard but perhaps that is where I''m failing.

#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *infile, *outfile;
int c; /*picked that up from the FAQ */
if ( (infile = fopen(argv[1], "rb") == NULL) /*picked the binary part up
from this google group*/
{
printf("Cannot open file\n");
exit(1);
}
if ( (outfile = fopen("Clean_file", "w+")) == NULL)
{
printf("Cannot open output file\n");
exit(1);
}
while ((c=fgetc(infile)) != EOF )
{
if(c == 0x1a) /* This is where I''m having a problem */
/* if(c == ''\0x1a'') This fails with compiler error - more than one
character defined for type char */
{
c=''_''; /*replace bad control char with something innocuous */
}
fputs(c,outfile);
}
fclose(infile);
fclose(outfile);
}
Yeah it''s a pretty primitive code, but I''m more interested in getting the
basics working before I go in and optimize the way it handles the input
file. This compiles on xlC and HP''s ANSI C compilers.

In the first if statement dealing with the ^Z, the program doesn''t detect
the control characters in the file, in the second statement the compiler
complains about syntax. If I set c as typecast char, it finds the control
characters, replaces them, but then blows away the EOF character and nukes
the file.

I have the suspicion that its the way I''m defining the c==\0x1a that is
leading my astray here. I can''t find any good consistent documentation on
exactly how to represent hex or octal in c code or string/character
operations.

解决方案

In article <Mq******************************@comcast.com>,
Eigenvector <m4********@yahoo.comwrote:

if(c == 0x1a) /* This is where I''m having a problem */

/* if(c == ''\0x1a'') This fails with compiler error - more than one
character defined for type char */

don''t use the single quotes around the value...
0x1a is an int not a char
c is an int also.

HTH

--
Mitch

te************@sand-hill.freeserve.co.uk
www.sand-hill.freeserve.co.uk/terminal_crazy


Terminal Crazy <Te************@sand-hill.freeserve.co.ukwrites:

In article <Mq******************************@comcast.com>,
Eigenvector <m4********@yahoo.comwrote:

>if(c == 0x1a) /* This is where I''m having a problem */


/* if(c == ''\0x1a'') This fails with compiler error - more than one
character defined for type char */

don''t use the single quotes around the value...
0x1a is an int not a char
c is an int also.

You''re using the wrong syntax for a hexadecimal escape in a character
literal. You want ''\x1a'', not ''\0x1a''. 0x1a (without the quotation
marks) will also work, but ''\x1a'' makes it clearer that you''re dealing
with a character.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


On May 23, 12:34 pm, "Eigenvector" <m44_mas...@yahoo.comwrote:

if ( (infile = fopen(argv[1], "rb") == NULL) /*picked the binary part up
from this google group*/
{
printf("Cannot open file\n");
exit(1);
}
if ( (outfile = fopen("Clean_file", "w+")) == NULL)

Open mode should be "w+b". You want to write it the
same way you read it.

if(c == 0x1a) /* This is where I''m having a problem */
/* if(c == ''\0x1a'') This fails with compiler error - more than one
character defined for type char */

There are four characters in that constant: ''\0'', ''x'',
''1'', and ''a''. I think you mean ''\x1a'', although the
uncommented code is also correct and does the same thing.

Yeah it''s a pretty primitive code, but I''m more interested in getting the
basics working before I go in and optimize the way it handles the input
file. This compiles on xlC and HP''s ANSI C compilers.

Out of interest, how were you planning on optimising
this? (I think you''ll find that reading in a block
at a time won''t gain you anything).

In the first if statement dealing with the ^Z, the program doesn''t detect
the control characters in the file, in the second statement the compiler
complains about syntax. If I set c as typecast char, it finds the control
characters, replaces them, but then blows away the EOF character and nukes
the file.

It doesn''t seem possible that your posted code won''t
find the 0x1a characters. There must be some other
problem, e.g. this isn''t your real code, or the
non-binary output is munging up.

I can''t find any good consistent documentation on exactly
how to represent hex or octal in c code or string/character
operations.

Try the C Standard, or "The C Programming Language"
by Kernighan & Ritchie.


这篇关于臭名昭着的^ Z问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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