Thread: Problem with strcat

  1. #1
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56

    Problem with strcat

    Hi, I have written a function called read_input and it is for writing a table of data from a file and categorizing and arranging them into an array of structs.. A sample of 1 line of the data is shown below:

    1.1.1.1.2 = 1.2372e-05 23 4

    The struct is written as:
    struct database_input {
    int category_A;
    int category_B;
    double category_C;
    int category_D;
    };
    What I am intended to put into one of the index of the array is :
    category_A = 11112
    category_B = 23
    category_C = 0.000012372
    category_D = 4
    My full written code is shown below:
    Code:
    void read_input( FILE *ifp, struct database_input *data)
    {
    int i=0;
    char number = 0;
    char *goto_A = NULL;
    
    char  *category_C_f = NULL;
    double goto_C;
    
    char *goto_B;
    
    while( i<99999 && feof(ifp) == 0)
    	{
    	while( number != ' ')
    	{
    		fscanf( ifp, &number);
    		if (number != '.')
    		strcat(goto_A, &number); //all of the strcat are making the segment faultation
    	}
    
    	data[i].category_A = (int)*goto_A;
    	ifp = ifp+2;
    	number = 0;
    
    	while( number != 'e')
    	{
    		fscanf( ifp,&number);
    		strcat(category_C_f, &number);
    	}
    	
    	goto_C = (double)*category_C_f;
    	data[i].category_C = goto_C/10000;
    	ifp = ifp+5;
    	number = 0;
    	
    	while( number != ' ')
    	{
    		fscanf( ifp, &number);
    		if (number != '.')
    		strcat(goto_B, &number);
    	}
    
    	
    	data[i].category_B = (int)*goto_B;
    	ifp = ifp+1;
    	number = 0;
    	fscanf( ifp, &number);
    	data[i].category_D = (int)number;
    	number = 0;
    	
    	while( number != '\n')
    	{
    		ifp++;
    	}
    	
    	i++;
    	}
    }
    Like what I had commented inside the code, I had a segmentation fault error when the code was run. This error was found (and has been later confirmed by a debugger) to be the fault of the strcat line, which I have been commented above. Can anybody please tell me how to fix this problem and tell me about other problems found in this program?

    Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Walk through the code and ask yourself this question:

    What is the value of goto_A at
    Code:
    strcat(goto_A, &number);
    That should tell you why it is failing.

    If you still aren't sure, try outputting goto_A's value right before that bit of code.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    Too slow !

  4. #4
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Do you mean that I can't concatenate to a string when the string itself is Null?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, where would you be writing the second string to? You can't write to NULL. It's a place where there's guarenteed to be no valid memory to write to.

  6. #6
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    I have change the goto_A line to
    char goto_A[] = "0"
    and now I can strcat the first value, but whenever I have '.'(period) or ' '(space), the program crashes into a segmentation fault again.

    How can I fix this problem?

    Update: Instead of (fscanf( ifp, "&#37;c", &number);), I used (number = fgetc(ifp);) and I also commented out (ifp++;).
    I have tried different types of data, and it wouldn't stop when the space is read, and it keeps on reading until a segmentation fault occurs when it reads the equal sign.
    Can somebody please help me on this problem?
    Last edited by firyace; 05-15-2007 at 09:36 AM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char goto_A[] = "0"
    allocates buffer of 2 chars

    you should allocate buffer that will be big enough to store all the resulting string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    struct database_input {
        int category_A;
        int category_B;
        double category_C;
        int category_D;
    };
    
    int main ( ) {
        char    input[] = "1.1.1.1.2 = 1.2372e-05 23 4\n";  // just like fgets() returns
        struct database_input foo;
        char    temp[6] = { 0 };
        if ( sscanf( input, "&#37;c.%c.%c.%c.%c = %lf %d %d",
                    temp+0, temp+1, temp+2, temp+3, temp+4,
                    &foo.category_C, &foo.category_B, &foo.category_D ) == 8 ) {
            foo.category_A = strtol( temp, NULL, 0 );
            printf( "Input=%s", input );
            printf( "A=%d, B=%d, C=%g, D=%d\n",
                foo.category_A, foo.category_B,
                foo.category_C, foo.category_D );
        }
        return 0;    
    }
    
    $ gcc foo.c
    $ ./a.exe
    Input=1.1.1.1.2 = 1.2372e-05 23 4
    A=11112, B=23, C=1.2372e-05, D=4
    Having done that, you can then do this
    Code:
    char input[BUFSIZ];
    while ( fgets ( input, sizeof input, ifp ) != NULL ) {
      // now do the sscanf.
      // If the result isn't 8 (in this case), then report an error
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    hi guys, thanks for the reply, you guys are a great help, I have just rewritten the whole program by capturing the data into several strings, almost the same as you had there, but instead I did:
    fscanf(ifp, "%s", a);

    fscanf( ifp, "%s %s %d %d", equal, b, &c, &d);

    and now it works perfectly. Again, thanks for you guys help and support.
    Last edited by firyace; 05-16-2007 at 12:57 PM.
    Firyace
    Undergraduate Research
    Electrical and Biomedical Engineering Department
    University of Calgary

    My Comp:
    |Core 2 Duo 6420 4mb cache| Corsair 2*1Gb memory pc5400|
    |500Gb and 80Gb Sata2| HIS 1950pro Turbo OC 256mb ViVo|
    |X-Cube2 red micro atx case| 3in1 Tiger Game port|
    |ASUS P5B-LD2 Rev2.0-VM| WindowsXP Pro SP2| Fedora 8|
    |Windows XP Pro 64|

    My Store
    Real estate 43

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well until fscanf doesn't convert the number of expected values anyway...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STRCAT problem
    By Frusciante in forum C Programming
    Replies: 9
    Last Post: 07-04-2007, 06:15 AM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Problem with strcat() function
    By sureshkumarct in forum C Programming
    Replies: 6
    Last Post: 01-03-2007, 08:05 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM