Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Reading inp...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 15 of 25 Topic 26078 of 27343
Post > Topic >>

Re: Reading input problem

by pete <pfiland@[EMAIL PROTECTED] > May 8, 2008 at 05:24 AM

arnuld wrote:
> I have a function named getword that read every single input from std.
> input.
> 
> WHAT I WANTED:  I want it read the word if it has less than or equal to
30
> characters. Anything else beyond that should be discarded and it should
> ask the user for new input.

Try this:

/* BEGIN fscanf_input.c */
/*
** There are only three different values
** that can be assigned to the int variable rc,
** from the fscanf calls in this program;
** They are:
**      EOF
**      0
**      1
** If rc equals EOF, then the end of file was reached,
** or there is some other input problem;
** ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line
** (a line consisting only of one newline character)
** was entered, and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** The newline character in the text line is replaced
** by a null character in the array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

#define LENGTH          30
#define str(x)          # x
#define xstr(x)         str(x)

int main(void)
{
     int rc;
     char array[LENGTH + 1];

     puts("The LENGTH macro is " xstr(LENGTH) ".");
     do {
         fputs("Enter any line of text to continue,\n"
             "or just hit the Enter key to quit:", stdout);
         fflush(stdout);
         rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
         if (!feof(stdin)) {
             getc(stdin);
         }
         if (rc == 0) {
             array[0] = '\0';
         }
         if (rc == EOF) {
             puts("rc equals EOF");
         } else {
             printf("rc is %d. Your string is:%s\n\n", rc, array);
         }
     } while (rc == 1);
     return 0;
}

/* END fscanf_input.c */

-- 
pete
 




 25 Posts in Topic:
Reading input problem
arnuld <sunrise@[EMAIL  2008-05-07 11:30:57 
Re: Reading input problem
Ben Bacarisse <ben.use  2008-05-07 10:58:41 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-09 02:37:42 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-08 10:41:14 
Re: Reading input problem
CBFalconer <cbfalconer  2008-05-07 12:35:35 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-09 02:35:14 
Re: Reading input problem
CBFalconer <cbfalconer  2008-05-08 13:57:23 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-09 02:36:41 
Re: Reading input problem
Ian Collins <ian-news@  2008-05-08 17:00:03 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-08 10:39:36 
Re: Reading input problem
Ian Collins <ian-news@  2008-05-08 18:15:26 
Re: Reading input problem
Richard Heathfield <rj  2008-05-08 07:14:08 
Re: Reading input problem
Ian Collins <ian-news@  2008-05-08 17:03:05 
Re: Reading input problem
CBFalconer <cbfalconer  2008-05-08 13:59:58 
Re: Reading input problem
pete <pfiland@[EMAIL P  2008-05-08 05:24:35 
Re: Reading input problem
Ben Bacarisse <ben.use  2008-05-08 11:45:24 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-09 20:36:41 
Re: Reading input problem
jacob navia <jacob@[EM  2008-05-09 10:20:33 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-10 07:11:22 
Re: Reading input problem
jacob navia <jacob@[EM  2008-05-09 13:33:45 
Re: Reading input problem
Ben Bacarisse <ben.use  2008-05-08 11:54:31 
Re: Reading input problem
arnuld <sunrise@[EMAIL  2008-05-10 00:08:14 
Re: Reading input problem
Barry Schwarz <schwarz  2008-05-09 10:55:56 
Re: Reading input problem
Antoninus Twink <nospa  2008-05-09 13:50:58 
Re: Reading input problem
Eligiusz Narutowicz<el  2008-05-09 14:04:44 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 21:33:25 CDT 2008.