473,388 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Reversing Input

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?

Mar 29 '06 #1
13 1272
jj**********@gmail.com 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.

Mar 29 '06 #2
jj**********@gmail.com wrote:
[...]
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()

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 29 '06 #3
Then why gets() is there?



Pedro Graca wrote:
jj**********@gmail.com wrote:
[...]
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()

--
If you're posting through Google read <http://cfaj.freeshell.org/google>


Mar 29 '06 #4

sonu wrote:

Don't top post. Corrected here...
Pedro Graca wrote:
jj**********@gmail.com wrote:
[...]
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.

--
BR, Vladimir

Mar 29 '06 #5
On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
<sa****************@gmail.com> wrote:
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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 29 '06 #6
sonu wrote:

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/>
Mar 29 '06 #7
CBFalconer wrote:
sonu wrote:
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.


That sonu asked this also shows why gets should be removed from the
standard.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Mar 29 '06 #8
Mark McIntyre <ma**********@spamcop.net> writes:
On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
<sa****************@gmail.com> wrote:
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.


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.

--
Keith Thompson (The_Other_Keith) 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.
Mar 29 '06 #9
On Wed, 29 Mar 2006 21:07:17 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
<sa****************@gmail.com> wrote:
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.


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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 29 '06 #10
Mark McIntyre wrote:
On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
<sa****************@gmail.com> wrote:

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


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.

--
jay

Mar 30 '06 #11
jaysome <ja*****@spamcop.com> wrote:
Mark McIntyre wrote:
On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
<sa****************@gmail.com> wrote:
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.


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
Mar 30 '06 #12
Richard Bos wrote:
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.

--
jay
Mar 30 '06 #13
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.

--
jay
Everyone loves the '$' sign. Everyone loves Raymond, too.
Mar 30 '06 #14

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

Similar topics

4
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart...
3
by: deanfamily11 | last post by:
I need to write a function that takes an integer as a parameter and then returns it reversed (i.e. input: 45678, returned: 87654). Can anyone help?
11
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in...
46
by: Albert | last post by:
Why doesn't: #include <stdio.h> void reverse(char, int); main() { char s;
6
by: neo | last post by:
Hello everyone, I want to write a function that takes a simple text file as input and outputs a file such that the last line in the input file becomes the first line in the output file and the...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
3
by: kellysgirl | last post by:
Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.Click ' creates and displays a new password Dim origPassword As String ...
2
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code...
15
by: ab12 | last post by:
I am writing in C. I want to reverse the input integer using recursion so if the input is 456, the output should be 654. it only needs to work with positive numbers. I've already written this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...

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.