473,563 Members | 2,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

output all a* by searching a text file

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

May 15 '07 #1
21 1789
In article <11************ **********@n59g 2000hsh.googleg roups.com>,
Umesh <fr************ ****@gmail.comw rote:
>/*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<stdli b.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]);
}
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?
--
Programming is what happens while you're busy making other plans.
May 15 '07 #2
Umesh <fr************ ****@gmail.comw rites:
/*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]

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_Keit h) 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"
May 15 '07 #3
On 15 May 2007 11:47:57 -0700, Umesh <fr************ ****@gmail.com>
wrote:
>/*program to search a* in a text file & write output in a file.*
indicated any character*/

#include<stdio .h>
#include<stdli b.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[i]=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[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
*/

Remove del for email
May 16 '07 #4
On Tue, 15 May 2007 19:46:22 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote:
>In article <11************ **********@n59g 2000hsh.googleg roups.com>,
Umesh <fr************ ****@gmail.comw rote:
>>/*program to search a* in a text file & write output in a file.*
indicated any character*/

You didn't actually ask any question.
>>
#include<stdi o.h>
#include<stdl ib.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]);
}

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
May 16 '07 #5
In article <d6************ *************** *****@4ax.com>,
Barry Schwarz <sc******@doezl .netwrote:
>On Tue, 15 May 2007 19:46:22 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote:
>>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?
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
May 16 '07 #6
/*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))!=E OF )
{
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
*/

May 16 '07 #7
On May 15, 10:50 pm, Umesh <fraternitydisp o...@gmail.comw rote:
/*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))!=E OF )
{
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
mf*****@sbcglob al.net

May 16 '07 #8
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))!=E OF )
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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 16 '07 #9
/*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))!=E OF )
{
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
*/

May 16 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2898
by: Bill Sneddon | last post by:
Can any one tell me how to output the following string? <%response.write "<tr><td><a href=""file://SERVER/mmlogs/TNAME" & yearmonth & """>"& "MYJUNK" & "</a><BR></td></tr>" %> <xsl:variable name="SERVER" select="MM_NAME" /> <xsl:variable name="TNAME" select="TOOL_NAME" />
4
2691
by: Richard Tierney | last post by:
To create help output (the response to "myprog --help", for example) I currently create a big .h file, which includes a single string, such as: static char *help_text = "\ myprog: my program\n\ loads and\n\ loads of\n\ painfully manually\n\ formatted and\n\
5
1624
by: Mal | last post by:
Hi, I have a button on a form that outputs a report to word. While it has been working well for a while, today it is not. The behaviour now is that it endlessly outputs pages to word. There are just 63 records (report pages) to output....I hit cancel when the counter was in the 10'000 range. Here is my code....any ideas??
3
2084
by: Paul H | last post by:
I have a text file that contains the following: ******************** __StartCustomerID_41 Name: Fred Smith Address: 57 Pew Road Croydon
6
4901
by: Alec MacLean | last post by:
Hi, I've created a small application for our company extranet (staff bulletins) that outputs a list of links to PDF's that are stored in a SQL table. The user clicks a link and the PDF is loaded into a new browser window. This works as expected on the test PC (using forms authentication, but no SSL) using IE. It also works as expected...
0
1086
by: mfilpot | last post by:
I am working on a prime number generator for sschool, but I jsut can't seem to figure out how to write the contents of the variable MeSSage into a new text file, Can someone help me? the script is: '========================================================================== ' ' VBScript Source File -- Created with SAPIEN Technologies...
4
5325
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given the Identifier. The logical way would be to read the record once and put it in an STL container such as vector and then use lower_bound to search for...
19
2627
by: Chad | last post by:
Okay, let's say I have an exotic os that limits how much goes to stdout. When I go like.. #include <stdio.h> int main(void) { int i=0; for(i=0; i< 10; i++) { printf("a \n");
4
2214
by: ume$h | last post by:
/*program to search a* in a text file & write output in a file.* indicated any character. IT IS WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING LIKE umesh*** OR 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? */ #include<stdio.h>...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.