I need to read parameters from a file called "params.c", the following is the 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" I have:

#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) ;
   }

}

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.

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

That line is definately incorrect -- strcmp() only takes two parameters, not thread. Maybe it should have been strncmp() which has 3 parameters ?


mystrcmp() is missing the closing function brace -- }


line 20: where is buff declared?

> /* what is "%*s" mean? */
The * suppresses assignment. So this scans a string, but doesn't store it anywhere.

Like you have
hello 123
in the file, and you want to skip hello and store 123

You indentation is OK but not perfect. You present your code in a good way, .you expect can expect more help. Take me an example I totally ignored your post when I just looked at your at start. I had a look at it again. This applied to all newbies. But i have sorted it out for now.

#include "systems.h"   
 
int mystrcmp(const char *tag, char *buff) 
{
  int l = strlen(tag) ;

  return( strcmp(tag, buff, l) ) ; 
}

int params( const char *file, int *nx,  int *ny ) 
{
  FILE *fd ;
  char buff[256];

  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 );   
      
      if ( mystrcmp("ny", buff) == 0 ) 
	     sscanf(buff, "%*s %d", ny );
   }
   
   fclose(fd) ; // This part of the code should be outside the loop.
   return 0 ;
}

I have changed few thing in your code. So that the clarify of your code improves. Perhaps the code which is highlighted in blue was must ;).

ssharish

This article has been dead for over six months. Start a new discussion instead.