Sponsored Content
Top Forums Programming Regardign strtok() output directing to 2-D string array Post 302189827 by andryk on Monday 28th of April 2008 04:03:38 AM
Old 04-28-2008
Quote:
Originally Posted by SankarV
Hi,


I just wrote a program in C to split a comma seperated string in to group of strings using strtok() function. The code is:

Code:
int main()
{
    char *temp;//not used here but basically we extract one string after another using strtok() and assign to a string pointer defined like this.
    char *str="aa,bb,cc,dd";
    char *ss;
    int count=0;
    for(int i=0;i<strlen(str);i++)
    {
      if(str[i]==',')
      count++;
    }

    char *ss[count];//2-d array where i want to store splitted results
    int j=0;
    *(ss+j)=strtok(str,",");

    while(*(ss+j)!=NULL)
    {
      *(ss+j)=strtok(NULL,",");
      j++;
    }
    
    //print  output
    for(int i=0;i<strlen(*ss);i++)
    printf("%s\t",*(ss+i));

    return 0;

}



The program compiles without error(please bear any syntax error, since i typed in hand without the code with me but the orignial program compiles successfully) but i am getting runtime error like 'Segmentation Fault'. I hope there needs to be proper memory handling here which i am not sure of.


Please help me in getting this splitted set of strings to a new 2-D array.


Thanks in advance.
Hi,
Just a little comment after a quick look to your code: i would suggest you to allocate memory properly (using malloc) and not use a shortcut like this
Code:
for(int i=0;i<strlen(str);i++)
    {
      if(str[i]==',')
      count++;
    }

    //char *ss[count];//2-d array where i want to store splitted results
    ss = malloc(count*sizeof(char)); // try this one!

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

directing output

How do I direct the output of an at command at now < backupprogram so that I see something happening. It says the job has been executed but I am not getting the tar file that my backup program on the computer anywhere at all Please help me - I really am a struggling begginer. (1 Reply)
Discussion started by: Cynergetix
1 Replies

2. Programming

better way than strtok?

Hi all, Right now I'm using this but it seems to be a hack: if (prefix(arg, "mark=")) { for (markid = strtok(args,"="); markid; markid=strtok((char *)NULL, "=")) { basically the user passes "mark=ny" to the command. I want to be able to extract "ny" from that... (7 Replies)
Discussion started by: annie
7 Replies

3. Shell Programming and Scripting

Directing program output to a file

When I do time tar cvf /dev/st0 /mnt/junk >> /root/benchlog, I want it to put the output of the time command into the benchlog file, but it put /mnt/junk. How do I get it to put the output of the tar command? (1 Reply)
Discussion started by: jeriryan87
1 Replies

4. Shell Programming and Scripting

directing output to multiple files

I have a script in which some outputs are directed to one file echo "Load Started" >>${LOGFILE1} If I have another file LOGFILE2 and i want to redirect the output of the above echo command to LOGFILE2 as well with the same command line... how can i do that? Thanks (2 Replies)
Discussion started by: cobroraj
2 Replies

5. Shell Programming and Scripting

Directing awk output to a folder

Dear All I have a simple bash script that creates a folder ( I called it TEMP) in the current directory. The question is: how do I direct the output of my awk script into folder TEMP? Below is my attempt: #!/bin/bash mkdir TEMP echo Enter input file: read infile awk... (4 Replies)
Discussion started by: Ghetz
4 Replies

6. Shell Programming and Scripting

Directing only part of a script's output to piped application

Is there a way to keep the output of a script displayed on the terminal when it's run by itself, but suspend part of that output and only have a specific part delivered when it's piped to another script or program? I'm thinking something like the following pseudocode: #!/bin/bash ... (1 Reply)
Discussion started by: trigg
1 Replies

7. Filesystems, Disks and Memory

iostat output vs TPC output (array layer)

Hi Guys, I've been having some arguments with my colleagues about one thing. Always my thought was that as as far as disk performance is concern by looking at the output of the iostat command (AIX) you would be able to identify if you have a hot disk and then by moving some files out that disk... (3 Replies)
Discussion started by: arizah
3 Replies

8. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

9. UNIX Desktop Questions & Answers

Re-directing output

ps –xyz >/tmp/proc 2>&1 Can anyone explain what does '2' and '&1' will do here? Thanks to explain (1 Reply)
Discussion started by: kkalyan
1 Replies

10. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies
strtok(3)						     Library Functions Manual							 strtok(3)

NAME
strtok, strtok_r - Split string into tokens LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <string.h> char *strtok( char *s1, const char *s2); char *strtok_r( char *s1, const char *s2, char **savept); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: strtok_r(): POSIX.1c strtok(): XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Contains a pointer to the string to be searched. Contains a pointer to the string of byte token delimiters. [POSIX] Identifies the loca- tion of the byte where the search for tokens should be started in the next call to strtok_r(). The savept parameter contains a pointer to a variable that contains a pointer to the byte in the string. DESCRIPTION
The strtok() function splits the string pointed to by the s1 parameter into a sequence of tokens, each of which is delimited by a byte equal to one of the bytes in the s2 parameter. Usually, the strtok() function is called repeatedly to extract the tokens in a string. The first time the application program calls the strtok() function, it sets the s1 parameter to point to the input string. The function returns a pointer to the first token. Then the application program calls the function again with the s1 parameter set to the null pointer. This call returns a pointer to the next token in the string. The application program repeats the call to strtok() with the s1 parameter set to the null pointer until all the tokens in the string have been returned. Note If the input string contains no instances of bytes from the delimiter string, the first call to strtok() results in the return of a pointer to the original string. On subsequent calls, strtok() returns NULL. In the initial call to strtok(), the function first searches the string pointed to by the s1 parameter to locate the first byte that does not occur in the delimiter string pointed to by the s2 parameter. If such a byte is found, it is the start of the first token. The str- tok() function then searches from there for a byte that does occur in the delimiter string. If such a delimiter is found, strtok() over- writes it with a null byte, which terminates the current token. The strtok() function saves a pointer to the byte following the null byte and returns a pointer to the start of the token. In the subsequent calls to strtok(), in which the s1 parameter is set to the null pointer, the function starts at its saved pointer and searches for the next byte that does not occur in the delimiter string pointed to by the s2 parameter. If such a byte is found, it is the start of the new token. The strtok() function then searches from there for a byte that does occur in the delimiter string. If such a delimiter is found, strtok() overwrites it with a null byte, which terminates the new token. The strtok() function saves a pointer to the byte following the null byte and returns a pointer to the start of the new token. If a call to the strtok() function cannot find a byte that does not occur in the delimiter string, it returns the null pointer. If a call to the strtok() function cannot find the terminating byte that does occur in the delimiter string, the current token extends to the end of the string and subsequent calls to strtok() will return the null pointer. If the delimiters used in the string change from one set of characters to another within the string, the application program can set the second parameter, s2, to different strings from call to call. The implementation behaves as though no function calls the strtok() function. The strtok_r() function is the reentrant version of strtok(). Upon successful completion, the strtok_r() function stores the saved pointer in *savept. If the s1 parameter is a null pointer, the strtok_r() function uses the saved pointer in *savept to start searching for the next token. In the initial call to strtok_r(), the *savept must be the null pointer. NOTES
[POSIX] The strtok() function is not supported for multithreaded applications. Instead, its reentrant equivalent, strtok_r(), should be used with multiple threads. EXAMPLES
The following example demonstrates how to split a string into tokens. #include <string.h> #include <locale.h> #include <stdio.h> #define LENGTH 40 main() { char string1[LENGTH], delimiters[LENGTH]; char *pstr ; int counter; (void)setlocale(LC_ALL, ""); printf("Enter the string to be searched: "); if (fgets(string1, LENGTH, stdin) != NULL) { printf("Enter the delimiter(s): "); if (fgets(delimiters, LENGTH, stdin) != NULL) { if ((pstr = strtok(string1, delimiters )) != NULL) { /* pstr points to the first token */ printf("Token 1 is %s ", pstr); counter = 2; while ((pstr = strtok((char *)NULL, delimiters )) != NULL) { printf("Token %d is %s ", counter, pstr); counter++; } } } } } RETURN VALUES
Upon successful completion, the strtok() and strtok_r() functions return a pointer to the first byte of the parsed token in the string. When there is no token in the string, a null pointer is returned. RELATED INFORMATION
Functions: string(3), wcstok(3), wcstok_r(3) Standards: standards(5) delim off strtok(3)
All times are GMT -4. The time now is 05:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy