<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Help with printf, gets</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/24777/help-with-printf-gets</link><description> 
Can someone help me get this to work? I want to be able to receive
an input on the serial window and then display that input. The
problem is the first text repeats forever with a while loop, and
without a loop the program displays text but doesn&amp;#39;t respond</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/146307?ContentTypeID=1</link><pubDate>Fri, 02 Jan 2009 03:13:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d2c98dcd-d87a-4ec4-b993-76d87eb68593</guid><dc:creator>Jacob F</dc:creator><description>&lt;p&gt;&lt;p&gt;
Ah, I was close. Thank you.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/144532?ContentTypeID=1</link><pubDate>Fri, 02 Jan 2009 03:05:52 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:25abc16d-17a3-46ec-9521-ede8a6ac7ad8</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;&lt;p&gt;
Try this.&lt;/p&gt;

&lt;pre&gt;
while(1)
  {
  char msg[60];

  printf(&amp;quot;Type a sentence and press ENTER\n&amp;quot;);
  gets (msg, sizeof(msg)-1);
  printf(&amp;quot;%s\n&amp;quot;,msg);
  }
&lt;/pre&gt;

&lt;p&gt;
Jon&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/142195?ContentTypeID=1</link><pubDate>Fri, 02 Jan 2009 00:54:00 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8908d5f8-4a56-443c-a872-9cd0af15eebc</guid><dc:creator>Jacob F</dc:creator><description>&lt;p&gt;&lt;p&gt;
Not sure what you mean. I&amp;#39;m pretty new at programming.&lt;/p&gt;

&lt;p&gt;
I understand gets() will read a line and put a terminating \0 into
the string array. Maybe I&amp;#39;m in over my head. I&amp;#39;ve been trying to get
this to work all week.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/139022?ContentTypeID=1</link><pubDate>Fri, 02 Jan 2009 00:26:05 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0e52d982-7046-4743-98b2-ee549a46e212</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;&lt;p&gt;
Where is msg[0] set to &amp;#39;\0&amp;#39;?&lt;/p&gt;

&lt;p&gt;
Jon&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/130416?ContentTypeID=1</link><pubDate>Fri, 02 Jan 2009 00:11:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5ba47e53-a90a-47eb-a675-34dc4c6f1ae9</guid><dc:creator>Jacob F</dc:creator><description>&lt;p&gt;&lt;p&gt;
Nifty. On the man page for gets the example code is practically
the inverse of what I need.&lt;/p&gt;

&lt;pre&gt;
void tst_gets (void) {
  char buf [21];

  do {
    gets (buf, sizeof(buf)-1);
    printf (&amp;quot;Input string \&amp;quot;%s\&amp;quot;&amp;quot;, buf);
  } while (buf [0] != &amp;#39;\0&amp;#39;);
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
So I try&lt;/p&gt;

&lt;pre&gt;
while(1){

char msg[60];

printf(&amp;quot;Type a sentence and press ENTER\n&amp;quot;);

while (msg [0] != &amp;#39;\0&amp;#39;)
{gets (msg, sizeof(msg)-1);

//puts(msg);
printf(&amp;quot;%s\n&amp;quot;,msg);
}
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Which again just loops the &amp;quot;type a sentence&amp;quot; text. Frustrating.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/125160?ContentTypeID=1</link><pubDate>Thu, 01 Jan 2009 23:54:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:021178d1-27b9-4f79-ae43-3b4de205383f</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
No. What isn&amp;#39;t available can be created.&lt;/p&gt;

&lt;p&gt;
But I took a look at the manual. The gets() in the Keil RTL is not
the gets() from the C language standard since it does take a buffer
size parameter. That makes it safe to use.&lt;/p&gt;

&lt;p&gt;
If it hadn&amp;#39;t, it would have been trivial two write a new function
that had picked up characters one at a time, while keeping track of
the buffer space.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/114567?ContentTypeID=1</link><pubDate>Thu, 01 Jan 2009 23:26:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d45c4b47-634b-4b34-9da0-22600f694283</guid><dc:creator>Jacob F</dc:creator><description>&lt;p&gt;&lt;p&gt;
Unfortunately fgets is not available in the standard C51 library.
See below:&lt;/p&gt;

&lt;p&gt;
&amp;quot;The ANSI C Standard Library includes a vast number of routines,
most of which are included in the Cx51 Library. Many, however, are
not applicable to an embedded application and are excluded from the
Cx51 Library.&amp;quot;&lt;br /&gt;
&lt;a href="http://www.keil.com/support/man/docs/c51/c51_xa_librarydif.htm"&gt;http://www.keil.com/support/man/docs/c51/c51_xa_librarydif.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
So it seems I must use gets()&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/101503?ContentTypeID=1</link><pubDate>Thu, 01 Jan 2009 15:50:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c7c614e5-8852-416c-a073-ff46dd8fc37c</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
A novice should not use gets(). And neither should anyone else. It
is the #1 buffer overflow function call known to man.&lt;/p&gt;

&lt;p&gt;
You would normally use fgets() instead, since it takes a buffer
size as parameter.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/77024?ContentTypeID=1</link><pubDate>Thu, 01 Jan 2009 13:56:58 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:19362780-61b6-424d-8407-42a7f4beec09</guid><dc:creator>Jacob F</dc:creator><description>&lt;p&gt;&lt;p&gt;
That still just loops the &amp;quot;Insert sentence&amp;quot; text.&lt;/p&gt;

&lt;p&gt;
I got it to work this way but its not what I want:&lt;/p&gt;

&lt;pre&gt;
while(1){

char msg[60];
char ch;
int i=0;

printf(&amp;quot;Type a sentence and press ENTER\n&amp;quot;);

while ((ch=getchar()) != &amp;#39;\n&amp;#39;)
        msg[i++]=ch;

msg[i]=&amp;#39;\0&amp;#39;;

i=0;
while(msg[i] != &amp;#39;\0&amp;#39;)
        putchar(msg[i++]);
printf(&amp;quot;\n&amp;quot;);
}
&lt;/pre&gt;

&lt;p&gt;
Can someone help a novice translate this into gets and puts form?
I want to be able to manipulate strings later.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Help with printf, gets</title><link>https://community.arm.com/thread/53188?ContentTypeID=1</link><pubDate>Tue, 30 Dec 2008 07:33:48 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:67eff690-585f-49f2-b898-2a57acab7801</guid><dc:creator>Drew Blank</dc:creator><description>&lt;p&gt;&lt;p&gt;
You would want to stay at the gets(string) until you actually have
valid data. Also you shouldn&amp;#39;t have a return 0 in your while loop.
Embedded programs should never end. Something like:&lt;/p&gt;

&lt;pre&gt;
   while (1) {
     printf (&amp;quot;Insert sentence: &amp;quot;);
     while(gets (string) == NULL)
     {/*do nothing*/}
     printf (&amp;quot;Your sentence is: %s&amp;quot;,string);

  }
&lt;/pre&gt;

&lt;p&gt;
I&amp;#39;m not sure if gets will return NULL if nothing is there, but you
get the idea.&lt;/p&gt;

&lt;p&gt;
Once you get comfortable with this method (busy polling) you
should start looking into using the serial interrupts :). That would
allow your program to do other things and only handle rs232 io when
it happens.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>