Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #104752 > unrolled thread
| Started by | fred flintstone <fred@server.invalid> |
|---|---|
| First post | 2017-02-26 04:14 -0800 |
| Last post | 2017-02-28 08:46 -0800 |
| Articles | 20 on this page of 86 — 21 participants |
Back to article view | Back to comp.lang.c
writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-26 04:14 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-26 04:55 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-26 14:28 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 03:58 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:11 +0000
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 13:33 +0000
Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 17:12 +0000
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 17:59 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:05 +0100
Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 18:37 +0000
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 11:40 -0800
Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:03 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 05:33 -0800
Re: writing a c program to substitute text "D. Aaron Sawyer" <aaron@110.net> - 2017-03-01 10:32 -0400
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 06:59 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 15:24 +0000
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:47 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 11:05 +0000
Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 13:33 +0000
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:10 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:07 +0100
Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 18:23 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 14:46 +0100
Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:27 -0500
Re: writing a c program to substitute text GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-27 15:28 +0100
Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:38 -0500
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 15:44 +0100
Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 10:36 -0500
Re: writing a c program to substitute text Anglican <h.toms@qmul.ac.uk> - 2017-02-27 15:50 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 17:13 +0100
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 16:30 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:08 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:20 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:24 -0800
Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:00 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:22 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:11 +0100
Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-02-28 15:41 -0800
solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:25 -0800
Re: solved Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-03-02 11:28 +0100
Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 10:54 +0000
Re: solved Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-03-02 15:28 +0100
Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 14:51 +0000
Re: solved Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-03-02 14:18 -0800
Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 23:55 +0000
Re: solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-14 18:32 -0700
Re: writing a c program to substitute text Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-17 07:04 -0700
Re: writing a c program to substitute text jladasky@itu.edu - 2017-02-27 13:48 -0800
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 14:16 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 08:47 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 01:59 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 11:39 +0100
Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-28 12:03 +0000
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:33 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:43 +0100
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:59 +0000
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 04:26 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 23:29 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 15:10 -0800
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 06:42 -0800
Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-26 16:54 +0000
[OT] "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 00:56 +0800
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 13:36 -0800
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 14:53 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 00:53 +0100
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 17:30 -0800
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 17:44 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 01:46 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 09:29 +0100
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 02:12 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 13:11 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 04:41 -0800
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 05:04 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:25 +0000
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:56 -0800
Re: writing a c program to substitute text "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 01:00 +0800
Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-26 18:55 -0500
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 04:20 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:38 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:33 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 19:38 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 11:40 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 21:03 +0100
Re: gcc optimization levels Noob <root@127.0.0.1> - 2017-02-28 22:55 +0100
Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-28 11:43 -0500
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:46 -0800
Page 2 of 5 — ← Prev page 1 [2] 3 4 5 Next page →
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-02-27 19:07 +0100 |
| Message-ID | <o91ppu$ld3$2@dont-email.me> |
| In reply to | #104892 |
On 27/02/17 18:10, Keith Thompson wrote: > jt@toerring.de (Jens Thoms Toerring) writes: > [...] >> A const char * like this is a special case: >> >> const char * txt = "bar"; >> >> Is a pointer to an array of chars that resides (often in >> write-only) memory and that is ininitialized ith the four >> chars 'b', 'a', 'r' and '\0'. > > I'm pretty sure you meant "read-only", not "write-only". Maybe he is working with a Signetics 25120 chip :-)
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2017-02-27 18:23 +0000 |
| Message-ID | <ehj94lFk69lU1@mid.uni-berlin.de> |
| In reply to | #104904 |
David Brown <david.brown@hesbynett.no> wrote: > On 27/02/17 18:10, Keith Thompson wrote: > > jt@toerring.de (Jens Thoms Toerring) writes: > > [...] > >> A const char * like this is a special case: > >> > >> const char * txt = "bar"; > >> > >> Is a pointer to an array of chars that resides (often in > >> write-only) memory and that is ininitialized ith the four > >> chars 'b', 'a', 'r' and '\0'. > > > > I'm pretty sure you meant "read-only", not "write-only". > Maybe he is working with a Signetics 25120 chip :-) ;-) -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-02-27 14:46 +0100 |
| Message-ID | <o91aft$qcq$1@dont-email.me> |
| In reply to | #104841 |
On 27/02/17 12:58, fred flintstone wrote:
> On 02/26/2017 05:28 AM, David Brown wrote:
>
>>
>> I cannot fathom what you are trying to say or do here. I know fine what
>> the commands above do, but don't understand why - nor does "3DBESNUB
>> type things" or "gonzaga-beating cougar" make any sense.
>>
>> In C, strings are between double quotes, not single quotes (they are for
>> character literals). Writing something like
>>
>> const char *s = "Arrow";
>>
>> is perfectly safe.
>>
>> If you have a C question to ask, feel free and many people here will try
>> to answer it. But please, make it comprehensible and say what you are
>> actually trying to do - it makes it a lot easier to help. Perhaps give
>> an example of the code you are trying to write, and then say where you
>> are having trouble.
>>
>
> As James Kuyper correctly links downthread, 3DBESNUB was the
> problem-solving acronym that my entire cohort at BYU had to use to get
> full credit on any physics problem. The first three letters are for
> diagrams, but they are in short supply in usenet.
>
So it is a strange acronym used within a department of a particular
university? This is an international group for C programming - it is
ridiculous to assume that anyone would know that, and entirely
unreasonable to expect people to google it or bother to read the webpage
that James found. For that matter, it is ridiculous to assume that
anyone would know what BYU stands for (some university somewhere, as far
as I can see).
When you want help, give /relevant/ information, keep /irrelevant/
information to a minimum, and minimise the effort other people will need
in order to help you.
> It seems I need code to describe the type of code I want. I honestly
> thought I had to come up with a number that wouldn't be smaller in bytes
> than whatever I'm storing in a string.
>
> Let me cough up some source and address a specification:
Good plan.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int version;
> int c;
I recommend defining your variables when you are ready to initialise and
use them, rather than putting them all at the start of the function.
It's a style choice, but C has allowed mixed statements and declarations
for nearly 20 years even though some people like old habits.
> FILE *fptr, *gptr;
> const char *executable1 = "arrow";
> const char *executable2 = "mahershala";
> const char *base = "toolchain";
> const char *ext = "sh";
These look fine, and are a perfectly good way to express constant
strings in a safe way.
You might also consider using:
const char * const executable1 = "arrow";
The first "const" tells the compiler that you will never modify the
characters pointed at by "executable1", while the second "const" tells
the compiler you will never modify the pointer itself.
> fptr = fopen("toolchain3.sh","r");
>
> if(fptr == NULL)
> {
> printf("fopen no bueno");
You will probably want "printf("fopen no bueno\n");" - printf does not
add a newline automatically. (Perhaps that's a difference from the
equivalent perl function.)
> exit(1);
> }
> gptr = fopen("toolchain4.sh","w");
>
> if(gptr == NULL)
> {
> printf("gopen no bueno");
> exit(1);
> }
>
> while (1) {
Your indent is wrong here. Consistent indentation makes it far easier
to see what your code is doing.
> c = fgetc(fptr);
>
> if (c == EOF)
> break;
> else
> putc(c, gptr);
> }
>
>
> fclose(fptr);
> fclose(gptr);
>
> return 0;
> }
>
> I need to concatenate 'toolchain3.sh' from its constituent parts and
> then increment it as the filename of gptr.
C is a much poorer language for such things than perl. Any C code for
this is going to be ugly in comparison.
But here is a way to do it:
const char * const base = "toolchain";
const char * const ext = "sh";
int partNo = 3;
// size is +1 for terminating 0,
// and +1 for the maximum number of digits in partNo
char fname[strlen(base) + strlen(ext) + 1 + 1];
snprintf(fname, sizeof fname, "%s%i%s", base, partNo, ext);
FILE * fptr = fopen("toolchain3.sh","r");
partNo++;
snprintf(fname, sizeof fname, "%s%i%s", base, partNo, ext);
FILE * gptr = fopen("toolchain4.sh","w");
You can make your "fname" array bigger if you want - especially if
"partNo" can have more than one digit. If you don't know the size of
partNo, you might want to check it and give an error if it exceeds the
bounds you have set.
But by using snprintf with a limit of "sizeof fname" for the buffer
size, you will not get any overflows. The worst that could happen is
that the filename will be truncated.
It is the nature of C strings that you first allocate a buffer of a
given size, then you fit the string into it. You can check the output
of snprintf (which returns the number of characters that the string
wants to have) to see if your buffer was big enough.
>
> This lacks the logic for a substitution but sets the stage for it. In
> the newly-created .sh file, executable2 shall be substituted for
> executable1. Again, for reference, this is our file:
>
> echo 'after preprocessor your program is' >text3.txt
> gcc -E arrow.c >>text3.txt
> echo 'after compilation avec warnings, output is' >>text3.txt
> gcc -Wall -Wextra -o arrow arrow.c >>text3.txt
> ./arrow >>text3.txt
> echo 'source listing is' >>text3.txt
> cat arrow.c >>text3.txt
>
> Also, I need a way of determining the natural number that follows
> 'text'. This is to be substituted by the number appending 'toolchain',
> so that the new number is uniform.
>
I'll let others answer that one.
> Alright, I hope I am clearer now.
Indeed, your C question was a great deal clearer, and I hope I've been
able to help with it.
> The BYU cougars did beat undefeated
> Gonzaga in college hoops.
I've figured out that BYU is a university of some sort. So "BYU
cougars" is either animals from your zoology department, the older women
in your class, or some sort of sports team. I'm hoping it is the last
option!
I'm guessing that "Gonzaga" is another college, or another team of some
sort. And "college hoops" would have to be a game or sports competition?
> It was a news event and this is a newsgroup,
It is a /C/ newsgroup, not a sports channel.
> where we do a lot of talking about C. It's also basketball season.
Ah, basketball.
You do know that in most of the world, it's just something that kids
play for fun - not a serious sport, or something with a "season"? And
this is an international newsgroup, not an American one. In most of the
world, when one school team beats another school team, the only people
interested are the people at the school and their parents. Also, given
that this is a /C/ programming newsgroup, I suspect that people here are
a good deal less interested in sports than the average person regardless
of their country.
>
> How important are substitutions, especially when they are errant?
Substitutions? Are you talking about variable substitutions in
programming, or as a sporting term?
> Did
> you watch the Oscars?
No. I like a good movie, but am totally uninterested in prize
ceremonies. (I fully appreciate that Hollywood wants to celebrate its
best films and people like this - and if it motivates them to make
better films, then great.)
Also, it was during the middle of the night for me.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2017-02-27 09:27 -0500 |
| Message-ID | <o91cs3$3p3$1@dont-email.me> |
| In reply to | #104852 |
On 02/27/2017 08:46 AM, David Brown wrote: ... > ... For that matter, it is ridiculous to assume that > anyone would know what BYU stands for (some university somewhere, as far > as I can see). It's not quite as ridiculous as it seems (though it is still ridiculous). BYU (Brigham Young University in Utah) is fairly well known here in the US. It's understandable that a BYU student might overestimate how well-known it is in the rest of the world.
[toc] | [prev] | [next] | [standalone]
| From | GOTHIER Nathan <nathan.gothier@gmail.com> |
|---|---|
| Date | 2017-02-27 15:28 +0100 |
| Message-ID | <20170227152820.52891cc80fbe1c12dee37f48@gmail.com> |
| In reply to | #104857 |
On Mon, 27 Feb 2017 09:27:02 -0500 James Kuyper <jameskuyper@verizon.net> wrote: > It's not quite as ridiculous as it seems (though it is still > ridiculous). BYU (Brigham Young University in Utah) is fairly well known > here in the US. It's understandable that a BYU student might > overestimate how well-known it is in the rest of the world. The US are not the center of the universe. There is life outside US...
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2017-02-27 09:38 -0500 |
| Message-ID | <o91dhn$6ec$1@dont-email.me> |
| In reply to | #104858 |
On 02/27/2017 09:28 AM, GOTHIER Nathan wrote: > On Mon, 27 Feb 2017 09:27:02 -0500 > James Kuyper <jameskuyper@verizon.net> wrote: > >> It's not quite as ridiculous as it seems (though it is still >> ridiculous). BYU (Brigham Young University in Utah) is fairly well known >> here in the US. It's understandable that a BYU student might >> overestimate how well-known it is in the rest of the world. > > The US are not the center of the universe. There is life outside US... <sarcasm> No kidding? When I wrote about "the rest of world", I thought I was talking about a fictional location! Amazing! </sarcasm> I wrote nothing to justify receiving such a rebuke.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-02-27 15:44 +0100 |
| Message-ID | <o91dte$7q7$1@dont-email.me> |
| In reply to | #104857 |
On 27/02/17 15:27, James Kuyper wrote: > On 02/27/2017 08:46 AM, David Brown wrote: > ... >> ... For that matter, it is ridiculous to assume that >> anyone would know what BYU stands for (some university somewhere, as far >> as I can see). > > It's not quite as ridiculous as it seems (though it is still > ridiculous). BYU (Brigham Young University in Utah) is fairly well known > here in the US. It's understandable that a BYU student might > overestimate how well-known it is in the rest of the world. > I'll accept your word for that - I am certainly no expert in how well-known it might be in the USA! I believe I might have heard of Brigham Young University - is that the one tied to the Mormons? I can't think of /why/ I might have heard of it - I will give it the benefit of the doubt, and assume I have read of some scientific or technical breakthrough from there. Few universities are well enough known by their initials that you could reasonably refer to them without further explanation - MIT and UCLA are a couple that spring to mind. Would you know what I was referring to by OU? I am sure that "Oxford University" would need no further introduction. However, I expect that when I were a lad, long ago at university, I also thought the whole world knew what OUCL was...
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2017-02-27 10:36 -0500 |
| Message-ID | <o91gtu$j57$1@dont-email.me> |
| In reply to | #104861 |
On 02/27/2017 09:44 AM, David Brown wrote: ... > I believe I might have heard of Brigham Young University - is that the > one tied to the Mormons? Correct - Brigham Young was one of the most famous Mormon leaders, and BYU is run by the Mormon church. > Few universities are well enough known by their initials that you could > reasonably refer to them without further explanation - MIT and UCLA are > a couple that spring to mind. Would you know what I was referring to by > OU? I am sure that "Oxford University" would need no further introduction. > > However, I expect that when I were a lad, long ago at university, I also > thought the whole world knew what OUCL was... Though Caltech is world famous, it's famous mostly among scientists, and I would not automatically expect it to be recognized by CIT even within that community. CIT has many alternative meanings, including 8 different educational institutions. Outside that community, even the full name is not sufficient - I've known people (including some residents of Los Angeles!) who confused California Institute of Technology with California Polytechnic State University, which my brother attended, or California State Polytechnic University, which my first girlfriend attend, both of which are also known as Cal Poly. I've met several people who thought I was referring to cosmetology when I mentioned that I was studying cosmology.
[toc] | [prev] | [next] | [standalone]
| From | Anglican <h.toms@qmul.ac.uk> |
|---|---|
| Date | 2017-02-27 15:50 +0000 |
| Message-ID | <o91hpb$l96$1@dont-email.me> |
| In reply to | #104861 |
On 27/02/17 14:44, David Brown wrote: > > Few universities are well enough known by their initials that you could > reasonably refer to them without further explanation - MIT and UCLA are > a couple that spring to mind. Would you know what I was referring to by > OU? I am sure that "Oxford University" would need no further introduction. > When I saw OU I thought Open University, does Oxford use that also? H
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-02-27 17:13 +0100 |
| Message-ID | <o91j46$rtn$1@dont-email.me> |
| In reply to | #104878 |
On 27/02/17 16:50, Anglican wrote: > On 27/02/17 14:44, David Brown wrote: > >> >> Few universities are well enough known by their initials that you could >> reasonably refer to them without further explanation - MIT and UCLA are >> a couple that spring to mind. Would you know what I was referring to by >> OU? I am sure that "Oxford University" would need no further >> introduction. >> > > When I saw OU I thought Open University, does Oxford use that also? > There are many universities that can be abbreviated to OU, and I expect most will use the abbreviation when the context is obvious. Despite having gone to Oxford, I also associate OU primarily with the Open University. But I don't think it is as well-known outside the UK as Oxford is. (UK here being the United Kingdom, rather than the University of Kentucky :-) )
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2017-02-27 16:30 -0800 |
| Message-ID | <e88b2c5c-e4dc-4302-a1dd-ae1158e2d139@googlegroups.com> |
| In reply to | #104878 |
On Monday, February 27, 2017 at 3:51:02 PM UTC, Anglican wrote:
> On 27/02/17 14:44, David Brown wrote:
>
> >
> > Few universities are well enough known by their initials that you could
> > reasonably refer to them without further explanation - MIT and UCLA are
> > a couple that spring to mind. Would you know what I was referring to by
> > OU? I am sure that "Oxford University" would need no further introduction.
> >
>
> When I saw OU I thought Open University, does Oxford use that also?
>
No, but OUCA ("owkah") is Oxford University Conservative Association, for
example. But "Oxford University" or "The University of Oxford" is usually
just "Oxford" outside of Oxford and "the university" within. I don't
think there's an official name and if there is it's recent and the
university far predates it.
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-27 07:08 -0800 |
| Message-ID | <453bb014-9f77-4418-8293-644148b256ee@googlegroups.com> |
| In reply to | #104857 |
W dniu poniedziałek, 27 lutego 2017 15:27:10 UTC+1 użytkownik James Kuyper napisał: > On 02/27/2017 08:46 AM, David Brown wrote: > ... > > ... For that matter, it is ridiculous to assume that > > anyone would know what BYU stands for (some university somewhere, as far > > as I can see). > > It's not quite as ridiculous as it seems (though it is still > ridiculous). BYU (Brigham Young University in Utah) is fairly well known > here in the US. It's understandable that a BYU student might > overestimate how well-known it is in the rest of the world. i versy slightly know what is utah not to say what is BYU many my friends for example im sure totally dont know what is c, im sure if i ask them they would say "c? first heard, what is that?"
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-27 07:20 -0800 |
| Message-ID | <12877beb-68a1-4a4d-8e77-825e6b2bb506@googlegroups.com> |
| In reply to | #104865 |
W dniu poniedziałek, 27 lutego 2017 16:08:29 UTC+1 użytkownik fir napisał: > W dniu poniedziałek, 27 lutego 2017 15:27:10 UTC+1 użytkownik James Kuyper napisał: > > On 02/27/2017 08:46 AM, David Brown wrote: > > ... > > > ... For that matter, it is ridiculous to assume that > > > anyone would know what BYU stands for (some university somewhere, as far > > > as I can see). > > > > It's not quite as ridiculous as it seems (though it is still > > ridiculous). BYU (Brigham Young University in Utah) is fairly well known > > here in the US. It's understandable that a BYU student might > > overestimate how well-known it is in the rest of the world. > > i versy slightly know what is utah > not to say what is BYU > well if thinking the one thing i know about utah is famous utah teapot > many my friends for example im sure totally dont know what is c, im sure if i ask them they would say "c? first heard, what is that?" in reality they would not say thet coz they dont talk english ;c in reality it would be more like negative head shaking and goin to other things - they seem not to be interested in c (all know what is windows though for example) this is somewhat interesting as when i say "i am not 'computer specialist'/'computer man', my main domain of work is c language" (sometimes i talk im a programmer but even programmer is to wide) they totally dont know what im takin about [more strange interestingly many programmers probably dont know what is c, they seem to know so many on c as on algol prolog or cobol ]
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-27 07:24 -0800 |
| Message-ID | <37ad6e6d-03d1-438f-b7ab-9b4f252e1454@googlegroups.com> |
| In reply to | #104869 |
> is c, they seem to know so many on c as on algol prolog or cobol ] errata: *as I know on algol prolog or cobol
[toc] | [prev] | [next] | [standalone]
| From | Manfred <noname@invalid.add> |
|---|---|
| Date | 2017-02-27 16:00 +0100 |
| Message-ID | <o91eup$1di5$1@gioia.aioe.org> |
| In reply to | #104852 |
On 2/27/2017 2:46 PM, David Brown wrote: > Also, given > that this is a /C/ programming newsgroup, I suspect that people here are > a good deal less interested in sports than the average person regardless > of their country. Your suspicion might be somewhat overdone, here. AFAICT C programming in quite compatible with sports interest (not necessarily with US sports only, though)
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2017-02-27 09:22 -0800 |
| Message-ID | <lno9xnft0u.fsf@kst-u.example.com> |
| In reply to | #104852 |
David Brown <david.brown@hesbynett.no> writes:
> On 27/02/17 12:58, fred flintstone wrote:
[...]
>> if(fptr == NULL)
>> {
>> printf("fopen no bueno");
>
> You will probably want "printf("fopen no bueno\n");" - printf does not
> add a newline automatically. (Perhaps that's a difference from the
> equivalent perl function.)
No, Perl's printf works like C's. But the above would be even better
written as:
fprintf(stderr, "fopen no bueno\n");
(I might use fputs(), which doesn't require a format string, but there's
something to be said for using the *printf functions consistently.)
>> exit(1);
exit(EXIT_FAILURE) is clearer and more portable.
[...]
> But by using snprintf with a limit of "sizeof fname" for the buffer
> size, you will not get any overflows. The worst that could happen is
> that the filename will be truncated.
Truncating the file name might be an unacceptable error. snprintf()
with a limit on the size of the target array avoids overflowing
the array, but it doesn't avoid problems caused by the fact that
you have an incorrect file name.
If you need to construct a file name from substrings, allocate
enough memory to hold the entire file name. If you can't allocate
the memory, it's usually best to give up rather than creating
a file with a shortened name. (If you're that short on memory,
you're likely to run into other problems very quickly.)
snprintf(NULL, 0, format_string, ...) is a good way to determine how
much memory you're going to need. (asprintf() is a non-standard
GNU extension that malloc()s and initializes a chunk of memory to
hold the result.)
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-02-27 19:11 +0100 |
| Message-ID | <o91q0v$nvh$1@dont-email.me> |
| In reply to | #104896 |
On 27/02/17 18:22, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 27/02/17 12:58, fred flintstone wrote:
> [...]
>>> if(fptr == NULL)
>>> {
>>> printf("fopen no bueno");
>>
>> You will probably want "printf("fopen no bueno\n");" - printf does not
>> add a newline automatically. (Perhaps that's a difference from the
>> equivalent perl function.)
>
> No, Perl's printf works like C's.
OK. I am not very familiar with Perl.
> But the above would be even better
> written as:
>
> fprintf(stderr, "fopen no bueno\n");
>
> (I might use fputs(), which doesn't require a format string, but there's
> something to be said for using the *printf functions consistently.)
gcc will turn something like that into fputs anyway - you can use
*printf functions consistently and still get the most efficient code.
(Not that optimal efficiency is particularly important for such error
handling.)
>
>>> exit(1);
>
> exit(EXIT_FAILURE) is clearer and more portable.
>
> [...]
>
>> But by using snprintf with a limit of "sizeof fname" for the buffer
>> size, you will not get any overflows. The worst that could happen is
>> that the filename will be truncated.
>
> Truncating the file name might be an unacceptable error. snprintf()
> with a limit on the size of the target array avoids overflowing
> the array, but it doesn't avoid problems caused by the fact that
> you have an incorrect file name.
Yes, it is up to the coder to decide what is important with error
handling. Truncation is "safe" in the sense of not having any undefined
behaviours or overruns, but as has been noted, "safe" depends on the
purpose and the application.
>
> If you need to construct a file name from substrings, allocate
> enough memory to hold the entire file name. If you can't allocate
> the memory, it's usually best to give up rather than creating
> a file with a shortened name. (If you're that short on memory,
> you're likely to run into other problems very quickly.)
>
> snprintf(NULL, 0, format_string, ...) is a good way to determine how
> much memory you're going to need. (asprintf() is a non-standard
> GNU extension that malloc()s and initializes a chunk of memory to
> hold the result.)
>
> [...]
>
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2017-02-28 15:41 -0800 |
| Message-ID | <aa13d6d7-7cdf-4001-84fa-782ae8764b83@googlegroups.com> |
| In reply to | #104841 |
On Monday, February 27, 2017 at 5:58:44 AM UTC-6, fred flintstone wrote:
> On 02/26/2017 05:28 AM, David Brown wrote:
>
> >
> > I cannot fathom what you are trying to say or do here. I know fine what
> > the commands above do, but don't understand why - nor does "3DBESNUB
> > type things" or "gonzaga-beating cougar" make any sense.
> >
> > In C, strings are between double quotes, not single quotes (they are for
> > character literals). Writing something like
> >
> > const char *s = "Arrow";
> >
> > is perfectly safe.
> >
> > If you have a C question to ask, feel free and many people here will try
> > to answer it. But please, make it comprehensible and say what you are
> > actually trying to do - it makes it a lot easier to help. Perhaps give
> > an example of the code you are trying to write, and then say where you
> > are having trouble.
> >
>
> As James Kuyper correctly links downthread, 3DBESNUB was the
> problem-solving acronym that my entire cohort at BYU had to use to get
> full credit on any physics problem. The first three letters are for
> diagrams, but they are in short supply in usenet.
>
> It seems I need code to describe the type of code I want. I honestly
> thought I had to come up with a number that wouldn't be smaller in bytes
> than whatever I'm storing in a string.
>
> Let me cough up some source and address a specification:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int version;
> int c;
> FILE *fptr, *gptr;
> const char *executable1 = "arrow";
> const char *executable2 = "mahershala";
> const char *base = "toolchain";
> const char *ext = "sh";
> fptr = fopen("toolchain3.sh","r");
>
> if(fptr == NULL)
> {
> printf("fopen no bueno");
> exit(1);
> }
> gptr = fopen("toolchain4.sh","w");
>
> if(gptr == NULL)
> {
> printf("gopen no bueno");
> exit(1);
> }
>
> while (1) {
> c = fgetc(fptr);
>
> if (c == EOF)
> break;
> else
> putc(c, gptr);
> }
>
>
> fclose(fptr);
> fclose(gptr);
>
> return 0;
> }
>
> I need to concatenate 'toolchain3.sh' from its constituent parts and
> then increment it as the filename of gptr.
>
> This lacks the logic for a substitution but sets the stage for it. In
> the newly-created .sh file, executable2 shall be substituted for
> executable1. Again, for reference, this is our file:
>
> echo 'after preprocessor your program is' >text3.txt
> gcc -E arrow.c >>text3.txt
> echo 'after compilation avec warnings, output is' >>text3.txt
> gcc -Wall -Wextra -o arrow arrow.c >>text3.txt
> ./arrow >>text3.txt
> echo 'source listing is' >>text3.txt
> cat arrow.c >>text3.txt
>
> Also, I need a way of determining the natural number that follows
> 'text'. This is to be substituted by the number appending 'toolchain',
> so that the new number is uniform.
>
> Alright, I hope I am clearer now.
Not really. I had to read this three times to understand what you want,
which is:
Replace all occurrences of <sym1> in toolchain<N>.sh
with <sym2> and write the result to toolchain<N+1>.sh.
That's the kind of problem statement that most of us are looking for.
So, you need to do the following:
1. Parse input file name to separate out the ordinal from the rest
of the name;
2. Create a new file name with the incremented ordinal;
3. Tokenize the contents of the input file;
4. Write each token as-is to the output file, *unless* it's the
symbol you want to substitute, in which case you write the
substitute symbol.
If you *know* your filename is always going to be of the form
"toolchain<N>.sh", something like this should work:
int ord;
if ( sscanf( inputFileName, "toolchain%d.sh", &ord ) == 1 )
{
sprintf( outputFileName, "toolchain%d.sh", ord + 1 );
}
else
{
fprintf( stderr, "could not parse input file name\n" );
}
By "tokenizing" the input, I just mean breaking it into a sequence of
strings (which is not *really* tokenization, but we're not going to worry
about that right now); that will make it easier to match your target string,
rather than trying to maintain state for every character.
Here's a pass I tossed off the top of my head - it ain't professional-
quality code by *any* stretch of the imagination, but it should give you
some flavor of what you're looking for.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/**
* We're using a dynamic, resizable buffer to read strings. If we
* encounter an input string that's longer than the current buffer
* size, attempt to double it.
*/
int expand( char **buf, size_t *bufsize )
{
char *tmp = realloc( *buf, *bufsize * 2 );
if ( tmp )
{
*bufsize *= 2;
*buf = tmp;
}
return tmp != NULL;
}
/**
* Read the next token from the input stream. In this program,
* tokens are either sequences of alphanumeric characters, or individual
* non-alphanumeric characters. Both are saved as strings to buf.
*
* For example, the input ">>text3.txt" would be tokenized as ">", ">",
* "text3", ".", "txt".
*/
int getNextSym( FILE *stream, char **buf, size_t *bufsize )
{
size_t charsRead = 0;
int c;
while ( ( c = fgetc( stream ) ) != EOF )
{
if ( !isalpha( c ) && !isdigit( c ) )
{
if ( charsRead > 0 )
{
/**
* If we've read a sequence of alphanumeric characters,
* we *don't* want this non-alphanumeric character to be
* part of the string (IOW, we treat all non-alphanumerics
* as string delimiters). Push the current character back
* on to the input stream, terminate the buffer, and return.
*/
ungetc( c, stream );
(*buf)[charsRead] = 0;
return 1;
}
else
{
/**
* All non-alphanumeric characters are returned in their own
* 1-character strings.
*/
(*buf)[0] = c;
(*buf)[1] = 0;
return 1;
}
}
else
{
/**
* Current character is alphanumeric, so we want to
* append it to the buffer, extending the buffer if
* necessary.
*/
if ( charsRead < *bufsize || expand( buf, bufsize ) )
{
(*buf)[charsRead++] = c;
}
else if ( charsRead == *bufsize )
{
/**
* Buffer expansion failed; push the latest character back on
* to the input stream, terminate the buffer, and return a failure
* code. It's up to the caller to decide what to do next.
*/
ungetc( c, stream );
(*buf)[charsRead-1] = 0;
return 0;
}
}
}
if ( charsRead > 0 )
{
/**
* We hit EOF while reading an alphanumeric sequence; terminate
* the buffer.
*/
(*buf)[charsRead] = 0;
return 1;
}
return 0;
}
/**
* Parse the sequence number out of the file name. This assumes the
* file name is of the form "toolchain<N>.sh".
*/
int getOrdinal( const char *filename )
{
int ord;
if ( sscanf( filename, "toolchain%d.sh", &ord ) == 1 )
return ord;
return -1;
}
int main( int argc, char **argv )
{
// arg1 = filename
// arg2 = symbol to replace
// arg3 = replacement symbol
if ( argc < 4 )
{
fprintf( stderr, "USAGE: %s toolchain<N>.sh <search-sym> <replace-sym>\n", argv[0] );
exit( EXIT_FAILURE );
}
char *filename = argv[1];
char *sym1 = argv[2];
char *sym2 = argv[3];
int ordinal = getOrdinal( filename );
if ( ordinal < 0 )
{
fprintf( stderr, "could not parse out ordinal from %s\n", filename );
exit( EXIT_FAILURE );
}
/**
* 32-bit integer is ~10 decimal digits, round up to 11, add 1 for the
* 0 terminator. You'll obviously want to change that for 64-bit int.
*/
char ofilename[ sizeof "toolchain" + sizeof ".sh" + 12 ];
if ( !sprintf( ofilename, "toolchain%d.sh", ordinal + 1 ) )
{
fprintf( stderr, "problem while building output file name toolchain%d.sh", ordinal + 1 );
exit( EXIT_FAILURE );
}
FILE *input = fopen( filename, "r" );
if ( !input )
{
fprintf( stderr, "could not open %s\n", filename );
exit( EXIT_FAILURE );
}
FILE *output = fopen( ofilename, "w" );
if ( !output )
{
fprintf( stderr, "could not create %s\n", ofilename );
exit( EXIT_FAILURE );
}
size_t bufsize = 80; // initial buffer size
char *buffer = malloc( bufsize );
/**
* Meat of the program - read the next symbol from the input file.
* If it matches our search symbol, write the replacement symbol
* to the output file - otherwise, copy the input symbol to the
* output file.
while ( getNextSym( input, &buffer, &bufsize ) )
{
if ( strcmp( buffer, sym1 ) == 0 )
fprintf( output, "%s", sym2 );
else
fprintf( output, "%s", buffer );
}
free( buffer );
fclose( input );
fclose( output );
return 0;
}
Initial unit testing seems to work:
> [fbgo448@n9dvap997]~/prototypes/subs: cat toolchain3.sh
> echo 'after preprocessor your program is' >text3.txt
> gcc -E arrow.c >>text3.txt
> echo 'after compilation avec warnings, output is ' >>text3.txt
> gcc -Wall -Wextra -o arrow arrow.c >>text3.txt
> ./arrow >>text3.txt
> echo 'source listing is ' >> text3.txt
> cat arrow.c >>text3.txt
>
> [fbgo448@n9dvap997]~/prototypes/subs: ./subs toolchain3.sh arrow mahershela
> [fbgo448@n9dvap997]~/prototypes/subs: cat toolchain4.sh
> echo 'after preprocessor your program is' >text3.txt
> gcc -E mahershela.c >>text3.txt
> echo 'after compilation avec warnings, output is ' >>text3.txt
> gcc -Wall -Wextra -o mahershela mahershela.c >>text3.txt
> ./mahershela >>text3.txt
> echo 'source listing is ' >> text3.txt
> cat mahershela.c >>text3.txt
>
Is that what you're going for?
Of course, the *right* answer is to use a language that makes this easy,
like sed, awk, Perl =twitch=, whatever.
> The BYU cougars did beat undefeated
> Gonzaga in college hoops. It was a news event and this is a newsgroup,
> where we do a lot of talking about C. It's also basketball season.
>
That's...not how this works.
> How important are substitutions, especially when they are errant?
If the code isn't doing what you want, that's generally a bad thing.
[toc] | [prev] | [next] | [standalone]
| From | fred flintstone <fred@server.invalid> |
|---|---|
| Date | 2017-03-02 01:25 -0800 |
| Subject | solved Re: writing a c program to substitute text |
| Message-ID | <ehq6ohF195iU1@mid.individual.net> |
| In reply to | #105183 |
On 02/28/2017 03:41 PM, John Bode wrote:
[OP reproduces respondent's source with only the addition of closing a
comment in main]
[hence big snip]
Thank you, john, for taking the time to write this. I'm happy with the
output and must say that this counts as getting around the bases. Maybe
I haven't been inclusive in sports talk to describe the problem space. I
could try to put it in cricket terms:
I squiffed one into the crease and got on the stump. Then, with your
turn as batman, you got us though all the wickets, and the score shall
reflect the maidens over-bowled.
If there's anyone who has a problem with my english on threads where I'm
OP, I need you to jump in a Loch. I won't take your killfiling me
personally in the slightest.
Are games important to C? They must be if John van Neumann is to be
believed. , I won't apologize for talking about them in what appears to
be a forum about logic as per the marquis.
Now, this is a nice result. It shows yet another way to make a string.
It's well documented and readable by the likes of me. I hope bode
doesn't mind that I used his name to drive the process:
$ cat bode1.sh
echo 'after preprocessor your program is' >text1.txt
gcc -E bode1.c >>text1.txt
echo 'after compilation avec warnings, output is' >>text1.txt
gcc -Wall -Wextra -o bode1 bode1.c >>text1.txt
./bode1 toolchain3.sh arrow mahershala>>text1.txt
echo 'source listing is' >>text1.txt
cat bode1.c >>text1.txt
$
If you do mind being associated with either me or this source, I now
have the software tools such as to change that in short time. This is
good enough that I want to put it on my C stuff with github. Since you
wrote it from scratch, it's my custom to offer to attribute. As this is
also a place where anonymity is valued and preserved, I could call it
jehosophat as well.
I did make a super-duper minor change to your source; maybe you might
take a final look at it before it goes out where applications ask for
packages. This is a capability I don't want to be without henceforth.
The proof is in the pudding:
$ cat toolchain4.sh
echo 'after preprocessor your program is' >text3.txt
gcc -E mahershala.c >>text3.txt
echo 'after compilation avec warnings, output is' >>text3.txt
gcc -Wall -Wextra -o mahershala mahershala.c >>text3.txt
./mahershala >>text3.txt
echo 'source listing is' >>text3.txt
cat mahershala.c >>text3.txt
$
Here we have file creation exactly as I laid out. The final part of the
output is the source listing:
# 11 "bode1.c"
int expand( char **buf, size_t *bufsize )
{
char *tmp = realloc( *buf, *bufsize * 2 );
if ( tmp )
{
*bufsize *= 2;
*buf = tmp;
}
return tmp !=
# 19 "bode1.c" 3 4
((void *)0)
# 19 "bode1.c"
;
}
# 30 "bode1.c"
int getNextSym( FILE *stream, char **buf, size_t *bufsize )
{
size_t charsRead = 0;
int c;
while ( ( c = fgetc( stream ) ) !=
# 35 "bode1.c" 3 4
(-1)
# 35 "bode1.c"
)
{
if ( !
# 37 "bode1.c" 3 4
((*__ctype_b_loc ())[(int) ((
# 37 "bode1.c"
c
# 37 "bode1.c" 3 4
))] & (unsigned short int) _ISalpha)
# 37 "bode1.c"
&& !
# 37 "bode1.c" 3 4
((*__ctype_b_loc ())[(int) ((
# 37 "bode1.c"
c
# 37 "bode1.c" 3 4
))] & (unsigned short int) _ISdigit)
# 37 "bode1.c"
)
{
if ( charsRead > 0 )
{
ungetc( c, stream );
(*buf)[charsRead] = 0;
return 1;
}
else
{
(*buf)[0] = c;
(*buf)[1] = 0;
return 1;
}
}
else
{
if ( charsRead < *bufsize || expand( buf, bufsize ) )
{
(*buf)[charsRead++] = c;
}
else if ( charsRead == *bufsize )
{
ungetc( c, stream );
(*buf)[charsRead-1] = 0;
return 0;
}
}
}
if ( charsRead > 0 )
{
(*buf)[charsRead] = 0;
return 1;
}
return 0;
}
int getOrdinal( const char *filename )
{
int ord;
if ( sscanf( filename, "toolchain%d.sh", &ord ) == 1 )
return ord;
return -1;
}
int main( int argc, char **argv )
{
if ( argc < 4 )
{
fprintf(
# 121 "bode1.c" 3 4
stderr
# 121 "bode1.c"
, "USAGE: %s toolchain<N>.sh <search-sym>
<replace-sym>\n", argv[0] );
exit(
# 122 "bode1.c" 3 4
1
# 122 "bode1.c"
);
}
char *filename = argv[1];
char *sym1 = argv[2];
char *sym2 = argv[3];
int ordinal = getOrdinal( filename );
if ( ordinal < 0 )
{
fprintf(
# 132 "bode1.c" 3 4
stderr
# 132 "bode1.c"
, "could not parse out ordinal from %s\n", filename );
exit(
# 133 "bode1.c" 3 4
1
# 133 "bode1.c"
);
}
char ofilename[ sizeof "toolchain" + sizeof ".sh" + 12 ];
if ( !sprintf( ofilename, "toolchain%d.sh", ordinal + 1 ) )
{
fprintf(
# 143 "bode1.c" 3 4
stderr
# 143 "bode1.c"
, "problem while building output file name
toolchain%d.sh", ordinal + 1 );
exit(
# 144 "bode1.c" 3 4
1
# 144 "bode1.c"
);
}
FILE *input = fopen( filename, "r" );
if ( !input )
{
fprintf(
# 150 "bode1.c" 3 4
stderr
# 150 "bode1.c"
, "could not open %s\n", filename );
exit(
# 151 "bode1.c" 3 4
1
# 151 "bode1.c"
);
}
FILE *output = fopen( ofilename, "w" );
if ( !output )
{
fprintf(
# 157 "bode1.c" 3 4
stderr
# 157 "bode1.c"
, "could not create %s\n", ofilename );
exit(
# 158 "bode1.c" 3 4
1
# 158 "bode1.c"
);
}
size_t bufsize = 80;
char *buffer = malloc( bufsize );
while ( getNextSym( input, &buffer, &bufsize ) )
{
if ( strcmp( buffer, sym1 ) == 0 )
fprintf( output, "%s", sym2 );
else
fprintf( output, "%s", buffer );
}
free( buffer );
fclose( input );
fclose( output );
return 0;
}
after compilation avec warnings, output is
source listing is
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/**
* We're using a dynamic, resizable buffer to read strings. If we
* encounter an input string that's longer than the current buffer
* size, attempt to double it.
*/
int expand( char **buf, size_t *bufsize )
{
char *tmp = realloc( *buf, *bufsize * 2 );
if ( tmp )
{
*bufsize *= 2;
*buf = tmp;
}
return tmp != NULL;
}
/**
* Read the next token from the input stream. In this program,
* tokens are either sequences of alphanumeric characters, or individual
* non-alphanumeric characters. Both are saved as strings to buf.
*
* For example, the input ">>text3.txt" would be tokenized as ">", ">",
* "text3", ".", "txt".
*/
int getNextSym( FILE *stream, char **buf, size_t *bufsize )
{
size_t charsRead = 0;
int c;
while ( ( c = fgetc( stream ) ) != EOF )
{
if ( !isalpha( c ) && !isdigit( c ) )
{
if ( charsRead > 0 )
{
/**
* If we've read a sequence of alphanumeric characters,
* we *don't* want this non-alphanumeric character to be
* part of the string (IOW, we treat all non-alphanumerics
* as string delimiters). Push the current character back
* on to the input stream, terminate the buffer, and return.
*/
ungetc( c, stream );
(*buf)[charsRead] = 0;
return 1;
}
else
{
/**
* All non-alphanumeric characters are returned in their own
* 1-character strings.
*/
(*buf)[0] = c;
(*buf)[1] = 0;
return 1;
}
}
else
{
/**
* Current character is alphanumeric, so we want to
* append it to the buffer, extending the buffer if
* necessary.
*/
if ( charsRead < *bufsize || expand( buf, bufsize ) )
{
(*buf)[charsRead++] = c;
}
else if ( charsRead == *bufsize )
{
/**
* Buffer expansion failed; push the latest character back on
* to the input stream, terminate the buffer, and return a failure
* code. It's up to the caller to decide what to do next.
*/
ungetc( c, stream );
(*buf)[charsRead-1] = 0;
return 0;
}
}
}
if ( charsRead > 0 )
{
/**
* We hit EOF while reading an alphanumeric sequence; terminate
* the buffer.
*/
(*buf)[charsRead] = 0;
return 1;
}
return 0;
}
/**
* Parse the sequence number out of the file name. This assumes the
* file name is of the form "toolchain<N>.sh".
*/
int getOrdinal( const char *filename )
{
int ord;
if ( sscanf( filename, "toolchain%d.sh", &ord ) == 1 )
return ord;
return -1;
}
int main( int argc, char **argv )
{
// arg1 = filename
// arg2 = symbol to replace
// arg3 = replacement symbol
if ( argc < 4 )
{
fprintf( stderr, "USAGE: %s toolchain<N>.sh <search-sym>
<replace-sym>\n", argv[0] );
exit( EXIT_FAILURE );
}
char *filename = argv[1];
char *sym1 = argv[2];
char *sym2 = argv[3];
int ordinal = getOrdinal( filename );
if ( ordinal < 0 )
{
fprintf( stderr, "could not parse out ordinal from %s\n", filename );
exit( EXIT_FAILURE );
}
/**
* 32-bit integer is ~10 decimal digits, round up to 11, add 1 for the
* 0 terminator. You'll obviously want to change that for 64-bit int.
*/
char ofilename[ sizeof "toolchain" + sizeof ".sh" + 12 ];
if ( !sprintf( ofilename, "toolchain%d.sh", ordinal + 1 ) )
{
fprintf( stderr, "problem while building output file name
toolchain%d.sh", ordinal + 1 );
exit( EXIT_FAILURE );
}
FILE *input = fopen( filename, "r" );
if ( !input )
{
fprintf( stderr, "could not open %s\n", filename );
exit( EXIT_FAILURE );
}
FILE *output = fopen( ofilename, "w" );
if ( !output )
{
fprintf( stderr, "could not create %s\n", ofilename );
exit( EXIT_FAILURE );
}
size_t bufsize = 80; // initial buffer size
char *buffer = malloc( bufsize );
/**
* Meat of the program - read the next symbol from the input file.
* If it matches our search symbol, write the replacement symbol
* to the output file - otherwise, copy the input symbol to the
* output file. **/
while ( getNextSym( input, &buffer, &bufsize ) )
{
if ( strcmp( buffer, sym1 ) == 0 )
fprintf( output, "%s", sym2 );
else
fprintf( output, "%s", buffer );
}
free( buffer );
fclose( input );
fclose( output );
return 0;
}
We seem to have ended up with malloc for the business end of it. I
haven't worked through the logic of getNextSym yet, but there's not a
lot with the way he's got the problem broken up.
Anyways, thanks all for constructive responses,
--
fred
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2017-03-02 11:28 +0100 |
| Subject | Re: solved Re: writing a c program to substitute text |
| Message-ID | <o98s0i$jl7$1@dont-email.me> |
| In reply to | #105313 |
On 02/03/17 10:25, fred flintstone wrote: > On 02/28/2017 03:41 PM, John Bode wrote: > > [OP reproduces respondent's source with only the addition of closing a > comment in main] > [hence big snip] > > Thank you, john, for taking the time to write this. I'm happy with the > output and must say that this counts as getting around the bases. Maybe > I haven't been inclusive in sports talk to describe the problem space. I > could try to put it in cricket terms: > > I squiffed one into the crease and got on the stump. Then, with your > turn as batman, you got us though all the wickets, and the score shall > reflect the maidens over-bowled. > > If there's anyone who has a problem with my english on threads where I'm > OP, I need you to jump in a Loch. I won't take your killfiling me > personally in the slightest. > > Are games important to C? They must be if John van Neumann is to be > believed. , I won't apologize for talking about them in what appears to > be a forum about logic as per the marquis. Game theory, which John /von/ Neumann (he was Hungarian, not Dutch) worked on, has no relation whatsoever to sports. It also has no relation to C. > > Now, this is a nice result. It shows yet another way to make a string. > It's well documented and readable by the likes of me. I hope bode > doesn't mind that I used his name to drive the process: > Well, you've lost me as a potential helper for C - and I think you have already alienated a number of others here. Maybe John Bode will help you again in the future, but maybe your disrespect will chase him away too. Certainly /I/ would not appreciate be referred to as "brown", having my name used as a program name, or being treated like a student drinking buddy. There is nothing wrong with using analogies if they illustrate a relevant point, and nothing wrong with using the occasional common idiom. But your "sports talk" is gibberish - it is meaningless and annoying, and distracts from what you are trying to say. You have been asked to stop, and you actively refuse to cooperate and communicate in a manner that suits the group. Learn that different kinds of language and different ways of expressing yourself are appropriate in different places. It is /you/ that looses out from this - not the rest of the group. It is /you/ who comes looking for help and advice, but will be ignored by many of the group experts. I hope you can understand this, and change your attitude somewhat. A learner asking questions about C is always a positive thing for this group - a chatty kid who insists on his right to say what he wants, how he wants, when he wants and where he wants is not a positive thing anywhere.
[toc] | [prev] | [next] | [standalone]
Page 2 of 5 — ← Prev page 1 [2] 3 4 5 Next page →
Back to top | Article view | comp.lang.c
csiph-web