Reversing Input

J

jjmacias2007

Hey I'm writing be cause I am requiring some help on this program I
need to write. I was able to look at some sample code done back in
Dec. somewhat similar to what I need to do. The thing with that code
is that it reads only one line of input data reverses it and also adds
on the original line. I tried using while(gets(line)!=NULL) to make
the program read all the lines of input data but it didn't work. The
program requires that I input data from stdin out put it in the same
exact pattern but reversed and give an ending number of lines and
length of file. Does any body have any suggestions?
 
V

Vladimir S. Oka

(e-mail address removed) opined:
Hey I'm writing be cause I am requiring some help on this program I
need to write. I was able to look at some sample code done back in
Dec. somewhat similar to what I need to do. The thing with that code
is that it reads only one line of input data reverses it and also
adds
on the original line. I tried using while(gets(line)!=NULL) to make
the program read all the lines of input data but it didn't work. The
program requires that I input data from stdin out put it in the same
exact pattern but reversed and give an ending number of lines and
length of file. Does any body have any suggestions?

Yes.

Since you're already using Google to access Usenet, why not also use
its fine search functionality, and search this very newsgroup. All of
the questions you have asked above have been answered, and discussed
in detail here in the past month or two.

Reading:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

before posting here again, is also a /very/ good idea.

--
BR, Vladimir

An envious girl named McMeanus
Was jealous of her lover's big penis.
It was small consolation
That the rest of the nation
Of women were with her in weeness.
 
P

Pedro Graca

[...]
I tried using while(gets(line)!=NULL) to make

Don't use gets().
Never use gets().
gets() is evil.
Never use gets(). /* oh! I'm repeating myself */

If your teacher tells you to use gets(), respectfully tell her she's
wrong and fgets() should be used instead.
Does any body have any suggestions?

Do *not* use gets()
 
S

sonu

Then why gets() is there?









Pedro said:
[...]
I tried using while(gets(line)!=NULL) to make

Don't use gets().
Never use gets().
gets() is evil.
Never use gets(). /* oh! I'm repeating myself */

If your teacher tells you to use gets(), respectfully tell her she's
wrong and fgets() should be used instead.
Does any body have any suggestions?

Do *not* use gets()
 
V

Vladimir S. Oka

sonu wrote:

Don't top post. Corrected here...
Pedro said:
[...]
I tried using while(gets(line)!=NULL) to make

Don't use gets().
Never use gets().
gets() is evil.
Never use gets(). /* oh! I'm repeating myself */

If your teacher tells you to use gets(), respectfully tell her she's
wrong and fgets() should be used instead.
Does any body have any suggestions?

Do *not* use gets()

Then why gets() is there?

Presumably not to break (inherently unsafe, if it uses `gets()`) legacy
code. There have been many, and long, discussions about that recently.
Search Google Groups if you're really interested.
 
M

Mark McIntyre

Then why gets() is there?

Because when the language was first designed, it was found useful by
the *highly expert* people using it, in the *tightly controlled*
environment they used.

Nowadays its there to avoid breaking legacy code, even though its
impossible to use safely by anyone other than an expert operating in a
completely controlled environment.

Mark McIntyre
 
C

CBFalconer

sonu said:
Then why gets() is there?

Don't top-post. gets is present for the same reason that dogs
leave smelly messes that stick to the bottom of your shoes, and can
be tracked about.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
F

Flash Gordon

CBFalconer said:
Don't top-post. gets is present for the same reason that dogs
leave smelly messes that stick to the bottom of your shoes, and can
be tracked about.

That sonu asked this also shows why gets should be removed from the
standard.
 
K

Keith Thompson

Mark McIntyre said:
Because when the language was first designed, it was found useful by
the *highly expert* people using it, in the *tightly controlled*
environment they used.

I'm not convinced that the old environments were that tightly
controlled. I suspect people back then were just more willing to
accept the risk of a buffer overflow, and didn't care as much about
the possibility of a user typing (or a file containing) hundreds of
characters on a single line. In the absence of networking, the
consequences of a buffer overflow were unlikely to be all that severe.

In any case, it's universally agreed that gets() should not be used
(except possibly in some very obscure circumstances), and ther seems
to be a strong majority in favor of removing it from the standard.
 
M

Mark McIntyre

I'm not convinced that the old environments were that tightly
controlled.

I was thinking of the *original* implementation, ie a half dozen guys
working on a single machine with total control over what they did.

Mark McIntyre
 
J

jaysome

Mark said:
Because when the language was first designed, it was found useful by
the *highly expert* people using it, in the *tightly controlled*
environment they used.

Nowadays its there to avoid breaking legacy code, even though its
impossible to use safely by anyone other than an expert operating in a
completely controlled environment.

Mark McIntyre

When I write quick-and-dirty programs that no one other than myself will
use, gets() can be handy. In my favorite hosted environment, sometimes
this happens: The console window, in which my program outputs its text,
opens, but closes immediately, before I have a chance to read my program
output.

In this case, adding something like the following:

{
char a[9];
gets(a);
}

cuts to the chase (i.e., console stays around waiting for your input)
faster than any other technique.

After I've perused the output and possibly copied it to a text file, I
can just hit Enter and be done. Or I can hold down the 'u' key for
minutes and then press Enter and see what undefined behavior is like. I
always do the latter, because I know that my implementations and OSes
that I use would never do anything like format my hard drive or smoke my
video card. And this is acceptable behavior to me.

On the other hand, one should never use gets() in, for example, a
program that is to be distributed to others. The reason is simple--the
inherent undefined behavior will inevitably lead to unacceptable
behavior, arguably promptly voiced by the users of said program.

(I once came across some sample code from Apple that used gets() with a
really huge buffer (something like 32 KB). That's one of the extremes
that people will go to in order to try to use gets(). I recall running
the program and holding a key down for minutes just to see what would
happen ... funny how I used to be a Mac addict :^)

Mr. MacIntyre is entirely correct when he says that you have to be an
expert to be able to legitimately use gets(). When esteemed regulars say
that gets() is dangerous, they mean it. And when they say to never use
gets(), they mean it. Kinda Sorta.
 
R

Richard Bos

jaysome said:
When I write quick-and-dirty programs that no one other than myself will
use, gets() can be handy.

The problem is ensuring that noone else _will_ use it, even
accidentally. Nor that you'll use it three weeks on, when you've
forgotten the limit. And then there's the problem of the cat on the
keyboard...
In my favorite hosted environment, sometimes
this happens: The console window, in which my program outputs its text,
opens, but closes immediately, before I have a chance to read my program
output.

In this case, adding something like the following:

{
char a[9];
gets(a);
}

cuts to the chase (i.e., console stays around waiting for your input)
faster than any other technique.

Does it? I find it unacceptably verbose. Here, have a scream and leap:

getchar();
After I've perused the output and possibly copied it to a text file, I
can just hit Enter and be done. Or I can hold down the 'u' key for
minutes and then press Enter and see what undefined behavior is like. I
always do the latter, because I know that my implementations and OSes
that I use would never do anything like format my hard drive or smoke my
video card.

You must be using VMS, then. Or possibly an AS/400. Under anything from
either Micro$oft or the Open Sores community, I wouldn't be so sure.
Mr. MacIntyre is entirely correct when he says that you have to be an
expert to be able to legitimately use gets(). When esteemed regulars say
that gets() is dangerous, they mean it. And when they say to never use
gets(), they mean it. Kinda Sorta.

Nothing Kinda Sorta about it. Never use gets(). Full stop.

Richard
 
J

jaysome

Richard said:
Mark McIntyre wrote: [snip]
In my favorite hosted environment, sometimes
this happens: The console window, in which my program outputs its text,
opens, but closes immediately, before I have a chance to read my program
output.

In this case, adding something like the following:

{
char a[9];
gets(a);
}

cuts to the chase (i.e., console stays around waiting for your input)
faster than any other technique.


Does it? I find it unacceptably verbose. Here, have a scream and leap:

getchar();

That's arguably better. But I inevitably forget what it's prototype is
compared to getc(). Overall, it's faster for me to use gets() than to
wait for Help on getc() or getchar() to arrive. And overall, it does not
matter to me.

All I can say is YMMV. And others' as well.
 
J

jaysome

Richard Bos wrote:
[snip]
You must be using VMS, then. Or possibly an AS/400. Under anything from
either Micro$oft or the Open Sores community, I wouldn't be so sure.

I used to use VMS, but now I use Windows XP.

I've also used Microsoft Windows 95/98/ME/NT/2K, and use of gets() would
not cause any problems in any of these OSes, let alone XP. For anyone
who disagrees, please provide the year, make and model of your
antiquated compiler.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top