How to interpret this input problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishwater00
    New Member
    • Jun 2008
    • 17

    How to interpret this input problem?

    I need to read parameters from a file called "params", the following is part of codes which can read parameters from that file. But I am not totally understand. Hope anyone can give detailed answers. Thanks.

    In a file called "params.c"
    [code=c]
    #include "systems.h" /* it includes all .h codes I need */

    int mystrcmp(const char *tag, char *buff)
    {
    int l = strlen(tag) ;

    return( strcmp(tag, buff, l) ) ; /* why he uses 3 parameters? I think it should be 2 parameters in "strcmp" ? */

    int params(char *file, int *nx, int *ny)
    {
    FILE *fd ;

    fd = fopen(file,"r") ;
    if ( fd == NULL )
    {
    fprintf(stderr, "cannot open %-s\n",file) ;
    return(1) ;
    }

    while ( fgets(buff,256, fd ) )
    {

    if ( mystrcmp("nx", buff) == 0 )
    sscanf(buff, "%*s %d", nx ); /* what is "%*s" mean? */

    if ( mystrcmp("ny", buff) == 0 )
    sscanf(buff, "%*s %d", ny );

    fclose(fd) ;
    return(0) ;
    }

    }
    [/code]

    After I compile it ( I do have main funtion, but the probem is here), it shows:
    In function ‘mystrcmp’:
    "error: too many arguments to function ‘strcmp’ "
    warning: passing argument 1 of ‘fgets’ from incompatible pointer type
    warning: passing argument 2 of ‘mystrcmp’ from incompatible pointer type
    warning: passing argument 1 of ‘sscanf’ from incompatible pointer type

    Thank you in advance.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    strcmp does indeed take two arguments, however, strncmp takes three. sscanf is complaining because your format string is essentially of the form <string> <int>, and the only pointer you're passing in for it to write to is an int*. The * means that the function will scan the data, but not read it in. As to the incompatibility warnings, I'm not completely sure...

    Comment

    • fishwater00
      New Member
      • Jun 2008
      • 17

      #3
      Originally posted by Laharl
      strcmp does indeed take two arguments, however, strncmp takes three.
      Thanks. it hsould be strncmp.

      Do I need to define "buff", if I donot define buff, error message shows "‘buff’ undeclared (first use in this function)". if I define "*buff" as int or float, it will shows the message like:

      warning: passing argument 1 of ‘fgets’ from incompatible pointer type
      warning: passing argument 2 of ‘mystrcmp’ from incompatible pointer type
      warning: passing argument 1 of ‘sscanf’ from incompatible pointer type

      How can I define buff coreecty or fgets can give it memory?

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Do I need to define "buff"
        Of course you do. Why otherwise?
        if I define "*buff" as int or float
        If you look up the documentation for fgets, you see that buff needs to be a char*. Actually, that translates to buff either pointed to a dynamically allocated char array, or a char array itself. (remember that when you pass a char array[], it decays to a char*)

        Comment

        • fishwater00
          New Member
          • Jun 2008
          • 17

          #5
          Originally posted by oler1s
          If you look up the documentation for fgets, you see that buff needs to be a char*. Actually, that translates to buff either pointed to a dynamically allocated char array, or a char array itself. (remember that when you pass a char array[], it decays to a char*)
          Thank you. I fix it.
          Code:
          char *buff

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by fishwater00
            Thank you. I fix it.
            Code:
            char *buff
            If you are doing that I hope you are allocating some memory for the pointer before writing data to the place it points to.

            Comment

            Working...