strtok

This is a discussion on strtok within the C Programming forums, part of the General Programming Boards category; help!? I think i'm retarded because I can't figure this out. I'm reading in the input at a prompt and ...

  1. #1
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429

    strtok

    help!? I think i'm retarded because I can't figure this out. I'm reading in the input at a prompt and I need to break it up.

    I'm using:

    while((scanf("%s", inputstr))

    to read in the string. Then I want to use strtok to break up inputstr by looking for spaces. I need the first one to go into a variable and the rest to go into an array. Can someone guide me? I've looked at examples of strtok and they don't make sense. If someone knows of a different way, I'd be up for that too.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Try using fgets to read a whole line

    scanf("%s"
    breaks the input into space delimited words to start with, so trying to strtok the result of that is a waste of time

    > while((scanf("%s", inputstr))
    And this is quite the most dangerous use of scanf used I've seen for some time.

  3. #3
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    yeah, but I'm not reading from a file. How do I get fgets to read from stdin??

    if scanf seperates the input already, how do i say for instance:

    var1 = first word scanned
    array[0] = second word scanned
    array[1] = third word scanned
    etc....

    and there is more to the while statement, so it's not entirely scary.

  4. #4
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    i guess what i'm trying to say is, I have to read the string all at once. I can't keep reading it. I need to read it in, seperate it and then deal with it. I'm working on my own shell.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ober5861
    How do I get fgets to read from stdin??
    Code:
    #include <stdio.h>
    int main(void)
    {
    	char name[10];
    	
    	if (fgets(name, sizeof(name),stdin))
    		printf("%s", name);
    	return(0);
    }
    Comments?
    Last edited by Hammer; 05-03-2002 at 05:12 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > and there is more to the while statement, so it's not entirely scary.
    Except for the fact that it will never exit

    If it succeeds in assigning a string, it will return 1 (and the while loop will evaluate this as true)

    If it fails, it will return EOF, and the while loop will also regard this as being true

  7. #7
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    i have a conditional statement later in the loop that checks for a string "exit" and if that is typed in, the loop exits, and conseqently so does the program.

    Incidently, this isn't working at the moment, but I think i just have to cut the input down to what is actually typed in and it should be fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21