output all a* by searching a text file

U

Umesh

/*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=getc(f))!=EOF)
if(c[1]=='a'&& c[2]!=' ')
fprintf(fp,"%c\n",c);
}
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
*/
 
W

Walter Roberson

/*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(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=getc(f))!=EOF)
if(c[1]=='a'&& c[2]!=' ')
fprintf(fp,"%c\n",c);
}


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);
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
*/

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?
 
K

Keith Thompson

Umesh said:
/*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=getc(f))!=EOF)
if(c[1]=='a'&& c[2]!=' ')
fprintf(fp,"%c\n",c);
}
fclose(f);
fclose(fp);
return 0;
}

[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 = getc(f)) != EOF)
if (c[1] == 'a' && c[2] != ' ')
fprintf(fp, "%c\n", c);
}
fclose(f);
fclose(fp);
return 0;
}
 
B

Barry Schwarz

/*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];

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=getc(f))!=EOF)


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] is 'a', then this invokes undefined behavior by attempting to
evaluate the non-existent c[2].
fprintf(fp,"%c\n",c);
}
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
*/



Remove del for email
 
B

Barry Schwarz

/*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(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=getc(f))!=EOF)
if(c[1]=='a'&& c[2]!=' ')
fprintf(fp,"%c\n",c);
}


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


It is very difficult to give c[2] a value since it doesn't exist.
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);
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
*/

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?


Remove del for email
 
W

Walter Roberson

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
It is very difficult to give c[2] a value since it doesn't exist.

Good point ;-) But does it make the behaviour any more undefined?
 
U

Umesh

/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#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,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )
{
if(c=='a'&& ch!=' ')
fprintf(fp,"%c%c\n",c,ch);
}
fclose(f);
fclose(fp);
return 0;
}
/* INPUT
abc
abc
abd
ap
OUTPUT
ab
ab
ab
ap
*/
 
M

Mark F. Haigh

/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#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,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )
{
if(c=='a'&& ch!=' ')
fprintf(fp,"%c%c\n",c,ch);}

fclose(f);
fclose(fp);
return 0;}
From your behavior here, you appear to be an idiot. You don't format
your code at all, and you don't ask any sensible, respectful, non-
homework questions.

You've already been given much more help than you deserve.

Mark F. Haigh
(e-mail address removed)
 
R

Richard Heathfield

Umesh said:
/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#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,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )

Working? Well, on my system, it doesn't even compile, of course, but if
we remove the dependency on mythical C99-conforming compilers, and if
we hack the filenames into something a bit more sensible, you're still
faced with the problem that you have the wrong types for c and ch, and
you are assuming that the opening of the output file works, which it
need not (when it failed on my system, the program dumped core).

And you still haven't learned about indentation.

None of the above crits is new to you. If you are unwilling to learn,
why do you bother to post queries here?
 
U

Umesh

/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#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");
if(fp==NULL)
{
puts("Error opening file");
exit(0);
}
char c,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )
{
if(c=='a'&& ch!=' ')
fprintf(fp,"%c%c\n",c,ch);
}
fclose(f);
fclose(fp);
return 0;
}
/* INPUT
abc
abc
abd
ap
OUTPUT
ab
ab
ab
ap
*/
 
W

Walter Roberson

/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#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,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )
{
if(c=='a'&& ch!=' ')

What if ch is \n or \t, and what if it is a non-alphabetic character?

What should happen if there is 'a' followed by 'a' followed by
something else? You aren't going to catch the second 'a' followed by
whatever, but the weak specifications you posted imply that you
need to.
 
R

Richard Heathfield

Umesh said:
/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
char c,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )

getc returns int, so c and ch have the wrong types. I already pointed
that out less than an hour ago. Into the bozo bin with you.
 
D

Default User

Umesh said:
/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING

As you seem to be unwillingly to listen to good advice, your posts are
a waste of time.


*plonk*



Brian
 
B

Barry Schwarz

Suppose I want to find all words in a text file which starts with 'a'
and ends with 'z' i.e a*z
where * denotes a string of characters. How can I do it?

First, you stop top posting.

Second, you trim your quotes to only the relevant portions.

Then you might start with some really basic issues, such as:

How do you recognize the start of a word?

How do you recognize the end of a word?

Given the answers to the above, is reading one character at a
time still a desirable approach compared to reading a line at a time?

Will a word span two or more lines?

Are you going to be case sensitive?

Or you might google for a regular expression library

(snip 60 irrelevant lines)


Remove del for email
 
U

Umesh

/*thx,this one works but how to prevent it from giving duplicate
results? e.g. i don't want 'ab' more than once as output.*/

#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[3];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]!=' ')
{
c[2]='\0';
fprintf(fp,"%s\n",c);
}
fclose(f);
fclose(fp);
return 0;
}
 
K

Keith Thompson

Umesh said:
/*thx,this one works but how to prevent it from giving duplicate
results? e.g. i don't want 'ab' more than once as output.*/

#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[3];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]!=' ')
{
c[2]='\0';
fprintf(fp,"%s\n",c);
}
fclose(f);
fclose(fp);
return 0;
}

Umesh, you need to learn how to post to Usenet.

The above is in response to a response to your question about how to
find all words in a text file which start with 'a' and end with 'z'.
The program you posted here doesn't attempt to do that. Since you
didn't provide any context, I can't tell just what you're trying to
do. Please read <http://cfaj.freeshell.org/google/>. Without knowing
what your code is supposed to do, I don't know how you expect anyone
to comment on it.

Also, your code is nearly illegible due to the poor use of whitespace
and the complete lack of indentation. Several people have pointed
this out to you before, and you've ignored our advice. At times,
you've posted code with single-column indentation, which is hardly
better than none. I suggest 4 columns. If you're not willing to do
it yourself, at least run your code through some tool like "indent"
before posting it.

One last time, here's your program above with added whitespace and
indentation. I've made no other changes, but it's *much* easier to
read. Perhaps now someone else will take a look at it.

#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[3];
while ((c[0] = getc(f)) != EOF)
if(c[0] == 'a' && (c[1] = getc(f)) != EOF && c[1] != ' ')
{
c[2] = '\0';
fprintf(fp, "%s\n", c);
}
fclose(f);
fclose(fp);
return 0;
}

If you're not going to accept the advice you've been given repeatedly,
I'm not going to waste any more time offering it.
 
C

CBFalconer

Umesh said:
Suppose I want to find all words in a text file which starts with
'a' and ends with 'z' i.e a*z where * denotes a string of
characters. How can I do it?

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
O

osmium

Umesh said:
/*thx,this one works but how to prevent it from giving duplicate
results? e.g. i don't want 'ab' more than once as output.*/

#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[3];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]!=' ')
{
c[2]='\0';
fprintf(fp,"%s\n",c);
}
fclose(f);
fclose(fp);
return 0;
}

You seem unusually resistant to learning. I have ignored this and similar
threads until this morning. You have been told repeatedly to indent your
code and provide context. After umpteen days on the froup, you still are
not doing either one.

This thread seems to be about finding 'a's followed by 'z's in a word. The
header says "This one works" WTF does "works" mean? What exactly, does
this program *do*? I don't see any 'z's. Has the subject changed? Then I
see you changing your identity and posting on comp.lang.c++. Do you think
we are all blind? Stupid? Can't read? Why change your identity?

I have no idea whatsoever what your current problem is/are or even what you
are trying to do. Does the word "state" mean anything to you in this
context? To find a 'z' following an 'a' in this word, you have to know that

a) an 'a' has occurred, and
b) you are still in the same word.

Remembering past events is called "state".

Note that the end of a word changes the state. There are other things that
can change the state also.
 
O

osmium

:

Suppose I want to find all words in a text file which starts with 'a'
and ends with 'z' i.e a*z
where * denotes a string of characters. How can I do it?
CBFalconer wrote:

I modified CBF's cleaned up version of your code so it wouldn't write on the
root of C: and ran the program. It didn't work for me. file 1 was

The qu)ick.bro:wn fox-jum"ped,ov~er;thelazy dog's:baxck.

and the ouput, file 2, was:
ax

What did I do wrong?

I suggest you look up two things, the *type* of the value returned by getc()
and "short circuited evaluation" (WRT boolean expressions).

Also, just before main() insert the following line:

/* This program computes the 13th term of the Malinkovsy formulation of the
Reiman hypothesis. */

If that is wrong in some way, correct it.
 
K

Keith Thompson

osmium said:
Also, just before main() insert the following line:

/* This program computes the 13th term of the Malinkovsy formulation of the
Reiman hypothesis. */

If that is wrong in some way, correct it.

Well, for starters you misspelled "Riemann", but I suspect that wasn't
your point. :cool:}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top