Asked  7 Months ago    Answers:  5   Viewed   80 times

If I don't know how long the word is, I cannot write char m[6];,
The length of the word is maybe ten or twenty long. How can I use scanf to get input from the keyboard?

#include <stdio.h>
int main(void)
{
    char  m[6];
    printf("please input a string with length=5n");
    scanf("%s",&m);
    printf("this is the string: %sn", m);
    return 0;
}

please input a string with lenght=5
hello
this is the string: hello

 Answers

44

Enter while securing an area dynamically

E.G.

#include <stdio.h>
#include <stdlib.h>

char *inputString(FILE* fp, size_t size){
//The size is extended by the input with the value of the provisional
    char *str;
    int ch;
    size_t len = 0;
    str = realloc(NULL, sizeof(*str)*size);//size is start size
    if(!str)return str;
    while(EOF!=(ch=fgetc(fp)) && ch != 'n'){
        str[len++]=ch;
        if(len==size){
            str = realloc(str, sizeof(*str)*(size+=16));
            if(!str)return str;
        }
    }
    str[len++]='';

    return realloc(str, sizeof(*str)*len);
}

int main(void){
    char *m;

    printf("input string : ");
    m = inputString(stdin, 10);
    printf("%sn", m);

    free(m);
    return 0;
}
Tuesday, June 1, 2021
 
Markol
answered 7 Months ago
82

From http://beej.us/guide/bgc/output/html/multipage/scanf.html:

The scanf() family of functions reads data from the console or from a FILE stream, parses it, and stores the results away in variables you provide in the argument list.

The format string is very similar to that in printf() in that you can tell it to read a "%d", for instance for an int. But it also has additional capabilities, most notably that it can eat up other characters in the input that you specify in the format string.

What's happening is scanf is pattern matching the format string (kind of like a regular expression). scanf keeps consumes text from standard input (e.g. the console) until the entire pattern is matched.

In your second example, scanf reads in a number and stores it in x. But it has not yet reached the end of the format string -- there is still a space character left. So scanf reads additional whitespace character(s) from standard input in order to (try to) match it.

Thursday, July 29, 2021
 
EnTee
answered 5 Months ago
73

Use scanf("%[^n]",buffer);. It will accept white space.

Sample program-

int main()
{
    char buffer[2048];
    printf("Enter the stringn");
    scanf("%[^n]",buffer);

    printf("%sn", buffer);
    return 0;
}

output-

root@sathish1:~/My Docs/Programs# ./a.out 
Enter the string
abc def ghi ijk
abc def ghi ijk
root@sathish1:~/My Docs/Programs# 
Saturday, August 7, 2021
 
Bentleylust
answered 4 Months ago
82

What you need to do is read the line in smaller increments, and resize your buffer as you go.

As an example (not tested and not meaning to be particularly elegant, just an example):

char *readline(FILE *f)
{
   char *buf = NULL;
   size_t bufsz = 0, len = 0;
   int keep_going = 1;

   while (keep_going)
   {
      int c = fgetc(f);
      if (c == EOF || c == 'n')
      {
         c = 0;             // we'll add zero terminator
         keep_going = 0;    // and terminate the loop afterwards
      }

      if (bufsz == len)
      {
         // time to resize the buffer.
         //
         void *newbuf = NULL;
         if (!buf)
         {
            bufsz = 512; // some arbitrary starting size.
            newbuf = malloc(bufsz);
         }
         else
         {
            bufsz *= 2; // issue - ideally you'd check for overflow here.
            newbuf = realloc(buf, bufsz);
         }

         if (!newbuf)
         {
            // Allocation failure.  Free old buffer (if any) and bail.
            //
            free(buf);
            buf = NULL;
            break;
         }

         buf = newbuf;
      }

      buf[len++] = c;
   }

   return buf;
}
Thursday, August 26, 2021
 
viky
answered 4 Months ago
62

It's in the Row. I fixed your xml a bit :)

var xml = "<item attr="some attribute"><description>anything</description></item>";
var ds = new DataSet();

ds.ReadXml( new StringReader( xml ), XmlReadMode.Auto );

var ia = ds.Tables[0].Rows[0].ItemArray;
var att = ia[1]; // att == "some attribute"

If you don't have a schema, you might have to check the column to determine what it is.

Per comment: You will see I am letting it infer the schema (XmlReadMode.Auto). It takes elements under the root node as Rows then adds the attributes in order and then the value in the element. So for example the following XML ...

var xml = "<items>
             <item attr1='attr1' attr2='attr2'>
                 <description>desc1</description>
             </item>
             <item attr1='attr3' attr2='attr4'>
                 <description>desc2</description></item>
           </items>";

I will get two rows (one for each item) with Columns for attr1, attr2 and description. You can change the way it interprets the XML using a schema.

Saturday, August 28, 2021
 
tika
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share