473,698 Members | 2,466 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
21 1807

"Umesh" wrote:

<snippage>
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:
Umesh wrote:

/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#include<stdio. h>
#include<stdlib .h>
int main(void)
>To start with, I converted it to valid C90 code:

#include<stdio .h>
#include<stdli b.h>

int main(void) {
FILE *f, *fp;
char c, ch;

f = fopen("c:/1.txt", "r");
if (f == NULL) {
puts("Error opening file"); exit(0);
}
fp = fopen("c:/2.txt", "w");
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;
}
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;t helazy 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.
May 20 '07 #21
"osmium" <r1********@com cast.netwrites:
[...]
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. 8-)}

--
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 20 '07 #22

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

Similar topics

2
2909
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
2699
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
1626
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
2089
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
4911
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 on the production server when using FireFox. The production server environment is using forms...
0
1088
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 PrimalScript 4.0 '
4
5342
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 a given identifier. But for some strange reason i'm asked to not use a container but instead...
19
2637
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
2219
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> #include<stdlib.h>
0
8678
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2333
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.