What is wrong in below simple code snippet?

  • Thread starter doublemaster007
  • Start date
D

doublemaster007

Hi All

I want to read the keybord till user presses enter (Enter also shud be
there in the strptr). Below code works incase of user has pressed
chars upto 1024. Incase of more chars this code doesnt work in HP-UX
machine....it ignores first 1024 chars and rest of them will be there
in strptr.

Does this code depnds on architecture? Why this code doesnt work in HP-
UX???

strptr = (char *)malloc(4096)
tempsstr=strptr
//strptr shub be memset to 0
while((*strptr++ = getchar()) != '\n')
{
// This loop shud read the chars until enter is pressed
}
strptr = mpsstr
 
D

doublemaster007

(e-mail address removed) ha scritto:


std::string line;
std::getline(std::cin, line);


line += '\n';


Each and every line of this code snippet looks like C, not C++ :) Are
you sure you did not mean to post in comp.lang.c instead?

--
Christian Hackl
(e-mail address removed)

Milano 2008/2009 -- L'Italia chiam , s !

I thought every c pgm is also a C++ pgm.!
Anyways thanks for ur sugg..I may post it there and see. Hope some one
may not ask me to post it again in Unix Forum..

One more thing..I had tried C++ way cin.getline(...) Still the output
was same..
 
R

Richard Herring

Christian Hackl said:
(e-mail address removed) ha scritto:


std::string line;
std::getline(std::cin, line);


line += '\n';

And what happens if the user presses the X key 4096 times?
Each and every line of this code snippet looks like C, not C++ :)

I thought C required statements to be terminated by semicolons?
;-/

IOW, this is not compilable code. "doublemaster007" should read the FAQ
at http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Until he posts the actual code that's causing the problem, not am
inaccurate paraphrase of it, there is no way of knowing where the
problem is.
 
D

doublemaster007

In message <[email protected]>, Christian Hackl








And what happens if the user presses the X key 4096 times?



I thought C required statements to be terminated by semicolons?
  ;-/

IOW, this is not compilable code. "doublemaster007" should read the FAQ
athttp://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Until he posts the actual code that's causing the problem, not am
inaccurate paraphrase of it, there is no way of knowing where the
problem is.

Problem here is..i canot copy paste the code :( Due to restriction
from my organisation..Anyways i will type simple program here...

#include <stdio.h>

int main ()
{
char str [80];
int i;

printf ("Enter your family name: ");
scanf ("%s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Mr. %s , %d years old.\n",str,i);
return 0;
}


This code doesnt work in HP-UX machine..Code works perfectly in SUSE.
May be this is OS Specific, But i do not know where to post it..If i
am posting this on a wrong place..pls guide me..

Output of above code is: If user enters more than 1024 chars...first
1024 chars will be truncated..

Since scanf did not work, i tried all the ways like
cin.getline
gets,fgets,fscanf...finally the first posted code, which reads char by
char...

Can any one help me?? This is annoying..make me feel like i donno to
write a basic pgm!!!
 
R

Richard Herring

In message
In message <[email protected]>, Christian Hackl











And what happens if the user presses the X key 4096 times?



I thought C required statements to be terminated by semicolons?
  ;-/

IOW, this is not compilable code. "doublemaster007" should read the FAQ
athttp://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Until he posts the actual code that's causing the problem, not am
inaccurate paraphrase of it, there is no way of knowing where the
problem is.

Problem here is..i canot copy paste the code :( Due to restriction
from my organisation..Anyways i will type simple program here...

#include <stdio.h>

int main ()
{
char str [80];
int i;

printf ("Enter your family name: ");
scanf ("%s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Mr. %s , %d years old.\n",str,i);
return 0;
}


This code doesnt work in HP-UX machine..Code works perfectly in SUSE.
May be this is OS Specific, But i do not know where to post it..If i
am posting this on a wrong place..pls guide me..

Output of above code is: If user enters more than 1024 chars...first
1024 chars will be truncated..

No. In the code above, if the user enters more than 79 characters, you
have undefined behaviour.

Your statement above is ambiguous. Do you mean that in your actual code,
if the user enters more than 1024 characters, only the first 1024 are
returned ("it is truncated to the first 1024 characters") or the first
1024 are removed and the rest is returned?
Since scanf did not work, i tried all the ways like
cin.getline
gets,fgets,fscanf...finally the first posted code, which reads char by
char...

Can any one help me?? This is annoying..make me feel like i donno to
write a basic pgm!!!

Me too. What was wrong with Christian Hackl's suggestion for dropping
the C idioms and using C++ techniques that avoid buffer-overrun
problems?
 
V

Victor Bazarov

[..]
Problem here is..i canot copy paste the code :( Due to restriction
from my organisation..Anyways i will type simple program here...

Bad idea. Post from outside of your organisation or work on loosening
the restrictions.
#include <stdio.h>

int main ()
{
char str [80];
int i;

printf ("Enter your family name: ");
scanf ("%s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Mr. %s , %d years old.\n",str,i);
return 0;
}


This code doesnt work in HP-UX machine..

What do you mean by "doesnt work"? In what *sense* doesn't it work?
>Code works perfectly in SUSE.

"Perfectly"? The code is *very* dangerous (since 'str' is declared to
have a very limited size - 80 bytes). This code is a buffer overrun
waiting to happen.
May be this is OS Specific, But i do not know where to post it..If i
am posting this on a wrong place..pls guide me..

You're posting C code in a C++ newsgroup. Consider 'comp.lang.c'.

Since you mentioned the different behaviour between HP-UX and Linux, it
*can* be OS-specific, but because you didn't say *what* didn't work on
HP-UX (and did work on Linux), it is rather impossible to conclude reliably.
Output of above code is: If user enters more than 1024 chars...

If the user enters 1024 chars when the buffer is only declared to be 80
bytes long, you're in the *undefined behaviour* land. Anything is
allowed to happen. Nasal demons. Look 'em up.
>first
1024 chars will be truncated..

Since scanf did not work, i tried all the ways like
cin.getline
gets,fgets,fscanf...finally the first posted code, which reads char by
char...

Look up 'std::getline', which reads a 'std::string'.
Can any one help me?? This is annoying..make me feel like i donno to
write a basic pgm!!!

Frustrating as it might be, sometimes it's easier to scrap what you have
and start from the beginning, which should start by *stating your
requirements*, not writing code.

Good luck!

V
 
D

doublemaster007

[..]
Problem here is..i canot copy paste the code :( Due to restriction
from my organisation..Anyways i will type simple program here...

Bad idea.  Post from outside of your organisation or work on loosening
the restrictions.






#include <stdio.h>
int main ()
{
  char str [80];
  int i;
  printf ("Enter your family name: ");
  scanf ("%s",str);
  printf ("Enter your age: ");
  scanf ("%d",&i);
  printf ("Mr. %s , %d years old.\n",str,i);
  return 0;
}
This code doesnt work in HP-UX machine..

What do you mean by "doesnt work"?  In what *sense* doesn't it work?

 >Code works perfectly in SUSE.

"Perfectly"?  The code is *very* dangerous (since 'str' is declared to
have a very limited size - 80 bytes).  This code is a buffer overrun
waiting to happen.
May be this is OS Specific, But i do not know where to post it..If i
am posting this on a wrong place..pls guide me..

You're posting C code in a C++ newsgroup.  Consider 'comp.lang.c'.

Since you mentioned the different behaviour between HP-UX and Linux, it
*can* be OS-specific, but because you didn't say *what* didn't work on
HP-UX (and did work on Linux), it is rather impossible to conclude reliably.
Output of above code is: If user enters more than 1024 chars...

If the user enters 1024 chars when the buffer is only declared to be 80
bytes long, you're in the *undefined behaviour* land.  Anything is
allowed to happen.  Nasal demons.  Look 'em up.

 >first
1024 chars will be truncated..
Since scanf did not work, i tried all the ways like
cin.getline
gets,fgets,fscanf...finally the first posted code, which reads char by
char...

Look up 'std::getline', which reads a 'std::string'.
Can any one help me?? This is annoying..make me feel like i donno to
write a basic pgm!!!

Frustrating as it might be, sometimes it's easier to scrap what you have
and start from the beginning, which should start by *stating your
requirements*, not writing code.

Good luck!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

char str [80]; I had changed to char str [2000]; Still the output is
truncated
 
V

Victor Bazarov

If the user enters 1024 chars when the buffer is only declared to be 80
bytes long, you're in the *undefined behaviour* land. Anything is
allowed to happen. Nasal demons. Look 'em up.
[..]
char str [80]; I had changed to char str [2000]; Still the output is
truncated

That is up to the compiler. Apparently it has a limitation on how big
the input can be for a %s format in 'scanf'. You should find something
in the compiler documentation about that. Lesson: don't use 'scanf'.

V
 
J

James Kanze

(e-mail address removed) wrote:
[..]
Output of above code is: If user enters more than 1024 chars...
If the user enters 1024 chars when the buffer is only
declared to be 80 bytes long, you're in the *undefined
behaviour* land. Anything is allowed to happen. Nasal
demons. Look 'em up.
first
1024 chars will be truncated..
[..]
char str [80]; I had changed to char str [2000]; Still the output is
truncated
That is up to the compiler. Apparently it has a limitation on
how big the input can be for a %s format in 'scanf'. You
should find something in the compiler documentation about
that. Lesson: don't use 'scanf'.

The problem is more likely in the system: it's not rare for
various Unixes (and possibly other systems) to have a finite
input buffer for the console, and to ignore characters which
don't fit. (Normally, I would expect that the character
wouldn't echo if it didn't fit, but who knows.)

Anyway, I'd second the recommendation that he use std::getline
on an std::string. If he still can't read more than 1000 some
characters, then he should ask in an appropriate forum for his
system, since that's probably where the problem lies.
 
D

doublemaster007

(e-mail address removed) ha scritto:


C and C++ share a common subset, but it's not true that every valid C
program is also a valid C++ program.


But my suggestion was using the free-standing function std::getline, not
the member function of std::istream!

That makes a huge difference, because the free-standing version uses
std::string, not char*. In C++, you use the std::string class for
strings, not char* like in C, unless you have a truly compelling reason
for doing so.

#include <iostream>
#include <string>

int main()
{
   std::string line;
   std::getline(std::cin, line);
   line += '\n';
   std::cout << "you entered the following line: " << line;

}

--
Christian Hackl
(e-mail address removed)

Milano 2008/2009 -- L'Italia chiam , s !

Thank u sooooooooo much for the suggetion. I do not know if i can use
std::string, Will there be any portable issue if i use string??
because trough out the project stl string is used...
ddoes STL string has methods to read line??
 
D

doublemaster007

(e-mail address removed) wrote:
[..]
Output of above code is: If user enters more than 1024 chars...
If the user enters 1024 chars when the buffer is only
declared to be 80 bytes long, you're in the *undefined
behaviour* land.  Anything is allowed to happen.  Nasal
demons.  Look 'em up.
 >first
1024 chars will be truncated..
[..]
char str [80]; I had changed to char str [2000]; Still the output is
truncated
That is up to the compiler.  Apparently it has a limitation on
how big the input can be for a %s format in 'scanf'.  You
should find something in the compiler documentation about
that.  Lesson: don't use 'scanf'.

The problem is more likely in the system: it's not rare for
various Unixes (and possibly other systems) to have a finite
input buffer for the console, and to ignore characters which
don't fit.  (Normally, I would expect that the character
wouldn't echo if it didn't fit, but who knows.)

Anyway, I'd second the recommendation that he use std::getline
on an std::string.  If he still can't read more than 1000 some
characters, then he should ask in an appropriate forum for his
system, since that's probably where the problem lies.

Hmmm..Even i suspect problem (or may be its because of some valid
reason) in the system..If there are any UNIX experts here or if any
one having access to HP-UX machine could help me out?
 
V

Victor Bazarov

[..]
Hmmm..Even i suspect problem (or may be its because of some valid
reason) in the system..If there are any UNIX experts here or if any
one having access to HP-UX machine could help me out?

Isn't there a Unix newsgroup (or two or three) out there? This is,
after all, a language newsgroup. If your problem is with the platform,
please post to the platform forum. Or contact HP tech support.

V
 
I

Ian Collins

Hmmm..Even i suspect problem (or may be its because of some valid
reason) in the system..If there are any UNIX experts here or if any
one having access to HP-UX machine could help me out?

Why don't you just use C++?
 
J

James Kanze

On Dec 4, 11:10 pm, James Kanze <[email protected]> wrote:

[...]
Hmmm..Even i suspect problem (or may be its because of some
valid reason) in the system..If there are any UNIX experts
here or if any one having access to HP-UX machine could help
me out?

Not here. For any restrictions in the system, you'd have to ask
in a system specific newsgroup. (On the other hand, regardless
of the system, there must be some restrictions. All of the
usual systems support editing the line you're currently
inputting, which in turn means some sort of internal buffering
when reading. And that internal buffer can't be infinite---even
if it dynamically grows, there must be a limit.)
 
J

James Kanze

(e-mail address removed) wrote:
Why don't you just use C++?

C++ can't provide services that the system doesn't support.
Neither Windows nor any of the Unix support infinitely long
lines of input from the keyboard, for example. (Under Unix, he
can find out the maximum length by using fpathconf with an
argument of MAX_CANON. Off hand, I don't know how to get it
from Windows---maybe GetConsoleScreenBufferInfo, but I'm not
sure of the relationship between the screen buffer and the
editing buffer.)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top