通过搜索文本文件输出所有a * [英] output all a* by searching a text file

查看:68
本文介绍了通过搜索文本文件输出所有a *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *用于在文本文件中搜索*的程序&将输出写入文件。*

表示任何字符* /


#include< stdio.h>

#include< stdlib.h>

int main(无效)

{

FILE * f,* fp;

f = fopen(" c:/1.txt"," r");

if(f == NULL){puts(" Error opening file"); exit(0);}

fp = fopen(" c:/2.txt"," w");

char c [2];

for(int i = 1; i< 3; ++ i)

{

while((c [i] = getc(f))!= EOF)

if(c [1] ==''a''&& c [2]!='''')

fprintf(fp,"% c \ n",c [i]);

}

fclose(f);

fclose(fp);

返回0;

}

/ *

INPUT c:/1.txt

abc

abc

abd

ap


输出c:/2.txt

a

a

a

a


必需的输出c:/2.txt

ab

ab

ab

ap
* /

/*program to search a* in a text file & write output in a file.*
indicated any character*/

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL) { puts("Error opening file");exit(0);}
fp=fopen("c:/2.txt","w");
char c[2];
for(int i=1;i<3;++i)
{
while((c[i]=getc(f))!=EOF)
if(c[1]==''a''&& c[2]!='' '')
fprintf(fp,"%c\n",c[i]);
}
fclose(f);
fclose(fp);
return 0;
}
/*
INPUT c:/1.txt
abc
abc
abd
ap

OUTPUT c:/2.txt
a
a
a
a

REQUIRED OUTPUT c:/2.txt
ab
ab
ab
ap
*/

推荐答案

文章< 11 ************* *********@n59g2000hsh.googlegroups .com> ;,

Umesh< fr **************** @ gmail.comwrote:
In article <11**********************@n59g2000hsh.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:

> / *用于在文本文件中搜索*的程序&在一个文件中写出输出。*
表示任何字符* /
>/*program to search a* in a text file & write output in a file.*
indicated any character*/



你实际上并没有问过任何问题。

You didn''t actually ask any question.


>
#include< stdio.h>
#include< stdlib.h>
int main(无效)
{
FILE * f,* fp;
f = fopen(" c:/1.txt"" r");
if(f == NULL){puts(" Error opening file") ;);退出(0);}
fp = fopen(" c:/2.txt"," w");
char c [2];
for(int i = 1; i< 3; ++ i)

{

while((c [i] = getc(f))!= EOF)

if(c [1] ==''a''&& c [2]!='''')

fprintf(fp,"%c \ n",c [i]);

}
>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL) { puts("Error opening file");exit(0);}
fp=fopen("c:/2.txt","w");
char c[2];
for(int i=1;i<3;++i)
{
while((c[i]=getc(f))!=EOF)
if(c[1]==''a''&& c[2]!='' '')
fprintf(fp,"%c\n",c[i]);
}



考虑当我是1时会发生什么。你只读过单个

字符,进入c [1],但你比较c [2]到''''即使

c [2]还没有给出一个值。无论c [2]发生在
的价值是多少,有可能它不是''',所以如果c [1]是''a'',那么if()会

成功,你会在一条线上单独打印出c [1]。


你还没有被打破,所以一会儿会继续,

仍然是我1,所以你会把下一个字符读入c [1],

将它与''''进行比较,如果是确实碰巧是''a'',你会再次比较未初始化的c [2]到'''',可能会成功。


如果你看一下,你会看到你不会突破这个

循环直到EOF,所以这个循环将继续扫描文件

找到所有'' a'',将它们自行打印出来,并继续



一旦最终达到EOF,则会终止并且

for循环的下一次迭代将执行,i = 2.

Thi时间,c [2]会被读入,但由于我们已经点击EOF

我们知道我们将再次获得EOF,导致while循环

到终止。 for循环将终止,并且你将继续使用程序的其余部分。

Consider what happens when i is 1. You''ve only read in the single
character, into c[1], but you then compare c[2] to '' '' even though
c[2] has not been given a value yet. Whatever value c[2] happens to
have, chances are that it isn''t '' '', so if c[1] is ''a'', the if() will
succeed and you would print out c[1] on a line by itself.

You haven''t broken out of the while yet, so the while would continue,
still with i being 1, so you would read the next character into c[1],
compare it to ''a'', and if it did happen to be an ''a'', you would
again compare the uninitialized c[2] to '' '', probably succeeding.

If you have a look, you will see that you will not break out of this
loop until EOF, so this loop will keep scanning through the file
finding all ''a'', printing them out on a line by themselves, and
continuing on.

Once EOF is finally reached, the while would terminate and the
next iteration of the for loop would execute, with i = 2.
This time, c[2] would be read into, but since we already hit EOF
we know that we are going to get EOF again, causing the while loop
to terminate. The for loop would terminate, and you''d
continue on with the rest of the program.


> fclose(f) ;
fclose(fp);
返回0;
}
/ *
INPUT c:/1.txt
abc
abc <输出页面:/2.txt

/>所需产出c:/2.txt
ab
ab
ab
ap
* /
>fclose(f);
fclose(fp);
return 0;
}
/*
INPUT c:/1.txt
abc
abc
abd
ap

OUTPUT c:/2.txt
a
a
a
a

REQUIRED OUTPUT c:/2.txt
ab
ab
ab
ap
*/



你的规格不清楚:如果

你在一条线的中间遇到''a',应该会发生什么?


由当你写for(/)
for()循环时,你有什么目的?究竟是什么需要执行

a固定次数?

-

编程就是在你忙的时候会发生什么制定其他计划。

Your specifications are unclear: what is supposed to happen if
you encounter an ''a'' in the middle of a line?

By the way, what purpose did you have in mind when you wrote
the for() loop? What exactly is it that needs to be executed
a fixed number of times?
--
Programming is what happens while you''re busy making other plans.


Umesh< fr **************** @ gmail.comwrites:
Umesh <fr****************@gmail.comwrites:

/ *用于在文本文件中搜索*的程序&将输出写入文件。*

表示任何字符* /


#include< stdio.h>

#include< stdlib.h>

int main(无效)

{

FILE * f,* fp;

f = fopen(" c:/1.txt"," r");

if(f == NULL){puts(" Error opening file"); exit(0);}

fp = fopen(" c:/2.txt"," w");

char c [2];

for(int i = 1; i< 3; ++ i)

{

while((c [i] = getc(f))!= EOF)

if(c [1] ==''a''&& c [2]!='''')

fprintf(fp,"% c \ n",c [i]);

}

fclose(f);

fclose(fp);

返回0;

}
/*program to search a* in a text file & write output in a file.*
indicated any character*/

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL) { puts("Error opening file");exit(0);}
fp=fopen("c:/2.txt","w");
char c[2];
for(int i=1;i<3;++i)
{
while((c[i]=getc(f))!=EOF)
if(c[1]==''a''&& c[2]!='' '')
fprintf(fp,"%c\n",c[i]);
}
fclose(f);
fclose(fp);
return 0;
}



[snip]


明智使用白色空间会使你的代码*更容易*更多

阅读。


这里'再次是你的程序。我已将其重新格式化为易读性,但

我没有做任何其他更改。


/ *程序在文本文件中搜索* &安培;将输出写入文件。*

表示任何字符* /


#include< stdio.h>

#include < stdlib.h>

int main(无效)

{

FILE * f,* fp;

f = fopen(" c:/1.txt"," r");

if(f == NULL)

{

puts(错误打开文件);

退出(0);

}

fp = fopen(" c: /2.txt" ;," w");

char c [2];

for(int i = 1; i< 3; ++ i)

{

而((c [i] = getc(f))!= EOF)

if(c [1] ==' 'a''&& c [2]!='''')

fprintf(fp,"%c\ n",c [i]);

}

fclose(f);

fclose(fp);

返回0;

}


-

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

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

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

- Antony Jay和Jonathan Lynn,是部长

[snip]

Judicious use of white space would make your code *much* easier to
read.

Here''s your program again. I''ve reformatted it for legibility, but
I''ve made no other changes.

/* program to search a* in a text file & write output in a file.*
indicated any character */

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *f, *fp;
f = fopen("c:/1.txt", "r");
if (f == NULL)
{
puts("Error opening file");
exit(0);
}
fp = fopen("c:/2.txt", "w");
char c[2];
for (int i=1; i<3; ++i)
{
while ((c[i] = getc(f)) != EOF)
if (c[1] == ''a'' && c[2] != '' '')
fprintf(fp, "%c\n", c[i]);
}
fclose(f);
fclose(fp);
return 0;
}

--
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"


2007年5月15日11 :47:57 -0700,Umesh< fr **************** @ gmail.com>

写道:
On 15 May 2007 11:47:57 -0700, Umesh <fr****************@gmail.com>
wrote:

> / *用于在文本文件中搜索*的程序&将输出写入文件。*
表示任何字符* /

#include< stdio.h>
#include< stdlib.h>
int main(void )
{* FILE * f,* fp;
f = fopen(" c:/1.txt",r);
if(f == NULL){puts(" Error opening file"); exit(0);}
fp = fopen(" c:/2.txt" ;,w");
char c [ 2];
>/*program to search a* in a text file & write output in a file.*
indicated any character*/

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL) { puts("Error opening file");exit(0);}
fp=fopen("c:/2.txt","w");
char c[2];



定义需要出现在语句之前,除非你有一个C99

编译器。 c:c [0]和c [1]只有两个元素。

Definitions need to appear before statements unless you have a C99
compiler. There are only two elements of c: c[0] and c[1].


> for(int i = 1; i< 3; ++ i )

{

while((c [i] = getc(f))!= EOF)
>for(int i=1;i<3;++i)
{
while((c[i]=getc(f))!=EOF)



如果我超过1,则通过尝试

来评估数组中不存在的元素来调用未定义的行为。

If i ever exceeds one, this invokes undefined behavior by attempting
to evaluate a non-existent element of an array.


if( c [1] ==''a''&& c [2]!='''')
if(c[1]==''a''&& c[2]!='' '')



如果c [1]是'' a'',然后通过尝试

来评估不存在的c [2]来调用未定义的行为。

If c[1] is ''a'', then this invokes undefined behavior by attempting to
evaluate the non-existent c[2].


fprintf(fp, %c\ n,c [i]);

}
fclose(f);
fclose(fp);
返回0; <输入c:/1.txt
abc
abc
abd


输出c:/2.txt
a





需要输出c:/2.txt
ab ab
ab
ap
* /
fprintf(fp,"%c\n",c[i]);
}
fclose(f);
fclose(fp);
return 0;
}
/*
INPUT c:/1.txt
abc
abc
abd
ap

OUTPUT c:/2.txt
a
a
a
a

REQUIRED OUTPUT c:/2.txt
ab
ab
ab
ap
*/



删除电子邮件的del l


Remove del for email


这篇关于通过搜索文本文件输出所有a *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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