Token Problem

This is a discussion on Token Problem within the C Programming forums, part of the General Programming Boards category; lo I have created a function that reads characters from the keyboard into a buffer. It terminates on reading a ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Token Problem

    lo
    I have created a function that reads characters from the keyboard into a buffer. It terminates on reading a return or on recieving 128 characters.

    I now want to create a function that will take a token from this buffer and place it in another array.
    eg
    if i got:
    test test2
    I would want test holding in the array, with the space being the separator.

    Ne suggestions would be very welcome, code is show below.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    	int i;
    	char inbuff[128];
    	FILE *fp;
    
    main() 
    {
    	void query();
    	char *gettoken(char sep);
    
    	char inbuff[128];
    
    	query();
    
    return 0;
     
    }
    
    
    /*Reads characters from input stream into a buffer*/
    void query()
    { 	char c;
    
    	fp=stdin;
    	i=0;
    
    	while ( (i<128) && (c!='\n') )
    	{
    		c=fgetc(fp);
    		inbuff[i]=c;
    		i++;
    	}
    
    	inbuff[i]='\0';
    
    printf( "Currently in the buffer: %s\n", inbuff );
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Suggestions:

    - don't use global variables unless you really need to. I see no reason to in your program.

    - Your input loop could fill all 128 bytes of the array, meaning the \0 byte will be written to position 129, which is outside the array bounds.

    - char c; The variable should be declared as an int to allow you to trap EOF.

    - Read up about fgets() and reading input

    Once you've done the reading, loop through the array, copying the bits you want to another array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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