scanf/gets problems

scanf/gets problems

Post by Chri » Thu, 08 Nov 2001 04:02:06



Hi,
I've been trying to split an inputed string into each individual word. I
want to put each of those words into an array.
ex: hello world my name
becomes
var1: hello
var2: world
var3: my
and so on.
But I seem to be having troubles because scanf stops at the space char so I
used gets instead. But for some reason it wont allow me to access each char
of the string. It core dumps.
Here is my code up to now...
Any help would be great!
Cheers,
Chris
------------------------
gets(command_entry); /* used gets to fix scanf problem */
   printf ("\n%s", command_entry); /* Debug perposes */

   /* Seperate the words from the input string */
for (x = 2; x < strlen(command_entry); x ++) {
     printf ("\nThis is the letter: %c\n",command_entry[x]);  /* Debug */
      if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
           while (y < x) {
               command_entry_sep[total_commands][y] =  command_entry[x][y];
                y += 1;
           }
         total_commands += 1;
         y = x; /* May have to add one here to avoid space */
      }
 }

 
 
 

scanf/gets problems

Post by Barry Margoli » Thu, 08 Nov 2001 04:31:18




>Hi,
>I've been trying to split an inputed string into each individual word. I
>want to put each of those words into an array.
>ex: hello world my name
>becomes
>var1: hello
>var2: world
>var3: my
>and so on.
>But I seem to be having troubles because scanf stops at the space char so I
>used gets instead. But for some reason it wont allow me to access each char

Don't use gets(), use fgets(); gets() doesn't let you tell how large the
input buffer is, so you'll have an overflow if the input line is larger
than the buffer.

Quote:>of the string. It core dumps.

You didn't show the declaration and initialization of command_entry, so
it's hard to tell.  My guess is that command_entry is an uninitialized
pointer.  You have to declare it as an array or initialize it with
malloc().

Quote:>------------------------
>gets(command_entry); /* used gets to fix scanf problem */
>   printf ("\n%s", command_entry); /* Debug perposes */

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

scanf/gets problems

Post by Chri » Thu, 08 Nov 2001 04:36:53





> >Hi,
> >I've been trying to split an inputed string into each individual word. I
> >want to put each of those words into an array.
> >ex: hello world my name
> >becomes
> >var1: hello
> >var2: world
> >var3: my
> >and so on.
> >But I seem to be having troubles because scanf stops at the space char so
I
> >used gets instead. But for some reason it wont allow me to access each
char

> Don't use gets(), use fgets(); gets() doesn't let you tell how large the
> input buffer is, so you'll have an overflow if the input line is larger
> than the buffer.

> >of the string. It core dumps.

> You didn't show the declaration and initialization of command_entry, so
> it's hard to tell.  My guess is that command_entry is an uninitialized
> pointer.  You have to declare it as an array or initialize it with
> malloc().

> >------------------------
> >gets(command_entry); /* used gets to fix scanf problem */
> >   printf ("\n%s", command_entry); /* Debug perposes */

> --

> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.

Here is the initialyzation aswell.....

   char *command_entry[100]; <-- I think this here was my major problem....
I think its making an array of strings...not the length of the string....
right???
   char *command_entry_sep[100];

   gets(command_entry); /* used gets to fix scanf problem */
   printf ("\n %s", command_entry); <-- THIS at least works
   printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no mater
what is inputed.
   printf ("\n 3: %c", command_entry[3]); <-- NOTHING
  printf ("\n 5: %c", command_entry[5]); <--- nothing.

   /* Seperate the words from the input string */
   for (x = 0; x < strlen(command_entry); x ++){
    printf ("\nINSIDE THE FOR LOOP!");
      printf ("\nThis is the letter: %c\n",command_entry[x]);
     if (command_entry[x] == " "){ /* Found a space */
        while (y < x){
          command_entry_sep[total_commands][y] = *command_entry[y];
          ++y;
       }
         total_commands += 1;
       y = x + 1; /* May have to add one here to avoid space */
    }
 }

 
 
 

scanf/gets problems

Post by Barry Margoli » Thu, 08 Nov 2001 04:53:50




>Here is the initialyzation aswell.....

>   char *command_entry[100]; <-- I think this here was my major problem....
>I think its making an array of strings...not the length of the string....
>right???

Right.  If you want a 100-byte string, it should be:

#define COMMAND_ENTRY_SIZE 100
char command_entry[COMMAND_ENTRY_SIZE];

Quote:>   char *command_entry_sep[100];

>   gets(command_entry); /* used gets to fix scanf problem */

Use:

fgets(command_entry, COMMAND_ENTRY_SIZE, stdin);

Quote:>   printf ("\n %s", command_entry); <-- THIS at least works
>   printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no mater
>what is inputed.

command_entry[1] is a pointer, not a character.  Why are you trying to
print it using the %c format?

Quote:>   printf ("\n 3: %c", command_entry[3]); <-- NOTHING
>  printf ("\n 5: %c", command_entry[5]); <--- nothing.

>   /* Seperate the words from the input string */
>   for (x = 0; x < strlen(command_entry); x ++){
>    printf ("\nINSIDE THE FOR LOOP!");
>      printf ("\nThis is the letter: %c\n",command_entry[x]);
>     if (command_entry[x] == " "){ /* Found a space */

This should be:

      if (command_entry[x] == ' ') ...

BTW, you seem to have two different threads where you're trying to get help
with the same code.  Pick one and stick with it.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

scanf/gets problems

Post by Chri » Thu, 08 Nov 2001 05:47:40





> >Here is the initialyzation aswell.....

> >   char *command_entry[100]; <-- I think this here was my major
problem....
> >I think its making an array of strings...not the length of the string....
> >right???

> Right.  If you want a 100-byte string, it should be:

> #define COMMAND_ENTRY_SIZE 100
> char command_entry[COMMAND_ENTRY_SIZE];

> >   char *command_entry_sep[100];

> >   gets(command_entry); /* used gets to fix scanf problem */

> Use:

> fgets(command_entry, COMMAND_ENTRY_SIZE, stdin);

> >   printf ("\n %s", command_entry); <-- THIS at least works
> >   printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no
mater
> >what is inputed.

> command_entry[1] is a pointer, not a character.  Why are you trying to
> print it using the %c format?

> >   printf ("\n 3: %c", command_entry[3]); <-- NOTHING
> >  printf ("\n 5: %c", command_entry[5]); <--- nothing.

> >   /* Seperate the words from the input string */
> >   for (x = 0; x < strlen(command_entry); x ++){
> >    printf ("\nINSIDE THE FOR LOOP!");
> >      printf ("\nThis is the letter: %c\n",command_entry[x]);
> >     if (command_entry[x] == " "){ /* Found a space */

> This should be:

>       if (command_entry[x] == ' ') ...

> BTW, you seem to have two different threads where you're trying to get
help
> with the same code.  Pick one and stick with it.

> --

> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.

I've fixed it. Thanks.
Cheers,
Chris

 
 
 

scanf/gets problems

Post by Toli » Thu, 08 Nov 2001 15:53:02




Quote:> Hi,
> I've been trying to split an inputed string into each individual word. I
> want to put each of those words into an array.
> ex: hello world my name
> becomes
> var1: hello
> var2: world
> var3: my
> and so on.
> But I seem to be having troubles because scanf stops at the space char so

I

Please try to use scanf  that way:
scanf ( "%[^:]", var ) - it will read values ( including spaces ) untill
read ':'. Example code to get ':' splitted  data:
scanf ( "%[^:]:%[^\n]", var1, var2 );

Quote:> used gets instead. But for some reason it wont allow me to access each
char
> of the string. It core dumps.
> Here is my code up to now...
> Any help would be great!
> Cheers,
> Chris
> ------------------------
> gets(command_entry); /* used gets to fix scanf problem */
>    printf ("\n%s", command_entry); /* Debug perposes */

>    /* Seperate the words from the input string */
> for (x = 2; x < strlen(command_entry); x ++) {
>      printf ("\nThis is the letter: %c\n",command_entry[x]);  /* Debug */
>       if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
>            while (y < x) {
>                command_entry_sep[total_commands][y] =

command_entry[x][y];

- Show quoted text -

Quote:>                 y += 1;
>            }
>          total_commands += 1;
>          y = x; /* May have to add one here to avoid space */
>       }
>  }

 
 
 

1. no more than 10 chars at scanf() or gets() ??

Hi all,

How can I limit the number of the user's key-ins at a scanf() or gets
()?
Whenever the scanf() prompts the user to key in a string, the user can
key in a few lines of characters that overwrites important information
that was printed on the screen.
How do I limit the user from typing in more than , lets say, 10
characters?

I was about to create a function that loops a getchar() and stores the
the chars in a string until the counter reached 10. But I can't imagine
that this problem wasn't fixed before. I don't want to re-invent the
wheel.

Greetings;
-=* Mark *=-

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Configuring Summagraphics tablet in X

3. sendmail 8.7.1 scanf problem

4. Is FreeBSD ok for a home user?

5. a problem with scanf

6. System bootup in progress - please wait ???

7. problems with scanf between Ultrix 4.3 and Solaris 2.4

8. HELP ON KSH:select

9. DHCP Problem, Getting a Reply but its not getting configured? ? ? ? ??

10. Looking for Implementation of scanf() ...

11. Help with scanf()

12. Shell Script for Scanf

13. changing from user scanf input to a loop