Problems with sscanf()?

This is a discussion on Problems with sscanf()? within the C Programming forums, part of the General Programming Boards category; I'm getting a segmentation fault with this piece of code. I pretty sure it has something to do with my ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Problems with sscanf()?

    I'm getting a segmentation fault with this piece of code. I pretty sure it has something to do with my use of sscanf(). Any tips would be greatly appreciated.

    Code:
    char c;
    .
    .
    .
    .
     if(argc==ARGNUM)
        {
        input = atoi(argv[2]);
        tempmol1 = molecule[input];
        input = atoi(argv[3]);
        tempmol2 = molecule[input];
        input = atoi(argv[4]);
        cellshift.X = input;
        input = atoi(argv[5]);
        cellshift.Y = input;
        input = atoi(argv[6]);
        cellshift.Z = input;
        check = 1;
        while(sscanf(argv[1][i],"%[^.]",&c)&&check!=0)
          {
    	if(c=='\0') check = 0;
    	i++;
          }
        for(j=0;j<3&&check==1;j++,i++)
          {
    	if(argv[1][j]!=extension[j]||argv[1][j]=='\0') check = 0;
          }
        if(!check)
          {
    	message("Warning! file could not be opened. Default file will be loaded");
    	//file = "default.cif";
          }
        fin = fopen(argv[1],"r");
        if(!fin) error("cannot read %s",argv[1]);
        }
    let me know if you can catch the problem here.

    THANKS
    Last edited by killiansman; 07-19-2007 at 11:27 AM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by killiansman View Post
    Code:
        while(sscanf(argv[1][i],"%[^.]",&c)&&check!=0)
    I'm not sure what you're trying to do there: load the next char of argv[1] into c? There's no need to use sscanf() for that:

    Code:
    while( (c = argv[1][i]) != 0 && check != 0)
    And I have no idea what that %[^.] thing is.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    I forgot to say that i declared "c" as a type char. What I'm trying to do is determine if the file extension matches the expected one. Therefore, the "[^.]" simply reads up to where it encounters a period. I don't know if this can be done using sscanf() though; I know it works fine with fscanf().

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by killiansman View Post
    I forgot to say that i declared "c" as a type char. What I'm trying to do is determine if the file extension matches the expected one. Therefore, the "[^.]" simply reads up to where it encounters a period. I don't know if this can be done using sscanf() though; I know it works fine with fscanf().
    If you are checking the file extension, maybe try something like this:

    Code:
    char *ext;
    
    ext = strrchr(argv[1], '.');
    if(!ext)
    {
        /* File has no extension */
    }
    else
    {
        if(strcmp(++ext, "foo") == 0)
        {
            /* File has the proper extension */
        }
        else
        {
            /* Extension isn't right */
        }
    }
    The call to strrchr() locates the last "." in the string. If one is found, it goes to the next character, i.e., the beginning of the extension. It then compares this with the extension you are looking for ("foo" in my example).

    This works because the extension is already at the end of the string, so it is null terminated.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to check if the string contains cpecific cahr - you can use strchr

    sscanf as a first parameter awaits pointer to char
    argv[1][i] is char - this causes the crash

    "&#37;[^.]" - reads several characters upto '.' so they will be written to the address provided as a 3rd parameter... you give a address of the char - so all other characters are written over the stack - causing stack corruption
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Thanks a lot guys (or girls)... I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf matching problems
    By KBriggs in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 02:20 PM
  2. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 03:43 PM
  3. Simple sscanf mystery
    By registering in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 11:47 PM
  4. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 PM

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