Thread: Problems while reading arguments

  1. #1
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82

    Question Problems while reading arguments

    I´m now working on a new project
    I´m having a problem,though:
    The program receives commands similar to assembly commands
    PUSH,POP,MUL and so on.The problem is ,sometimes there´s the command PUSH <number>,where number is an int,but other times i have PUSH <var>,where var is a char *.These commands are written in a file. I´m using fscanf to read the commands.An input file would be like this:
    PUSH 1
    PUSH 2
    MUL
    POP abc
    PUSH abc
    ...
    But when i´m reading the commands,i dont know if the argument will be an int or a char*.And,according to the type of argument,i have to call different functions.How can i solve this?

  2. #2
    Unreg1
    Guest
    If you can convert it to a number, then it's a number, else its a string. Do that conversion test, and then branch accordingly.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    27
    this may not be the smartest way, but it will work
    I would guess that a variable will never start with a number,
    so just test to see if the first part of the variable is a number and if it is convert it to a int, using atoi().

    basicly
    Code:
    if((strncmp(var,"1",1) == 0) || (strncmp(var,"2",1) == 0) ||... (strncmp(var,"9",1) == 0)){
    number = atoi(var);
    }
    well you get the idea

  4. #4
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    ok,but how do i read and declare it?
    char argument[20];
    fscanf(file,"%s",argument)?
    and then use that atoi thing?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    27
    you could declare it like
    char argument[20]
    then you could break up the argument string into two strings using strtok() or index(), check the man pages for more info on those. I try not to use scanf or fscanf, are you reading from a file?
    If so try using fgets to read from stdin.
    Last edited by samps005; 05-05-2003 at 04:25 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're overcomplicating this.
    According to what you have here, you will never have just a number on a line. As such, read until you hit a space. That is your command. If it is a command that takes more than one argument, read until you hit some more whitespace. That's your second command.

    Simplifying even more, read an entire line at once. Then apply the above logic.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Ok,here´s what i have done so far:
    Code:
    int main(int argc,char *argv[])
    {
    	FILE *fp;
    	char comando[20];
    	char argumento[20];
    	int arg_numerico;
    	fp=fopen(argv[1],"r");
    	if(fp==NULL)
    	{
    		printf("Impossivel abrir o ficheiro %s\n", argv[1]);
    	}
    	else
    	{
    	while((fscanf(fp,"%s %s",comando,argumento))!=EOF)
    	{
    		if(analisa(argumento))
    		{
    			if(comando=="POP")
    			pop(&l,argumento);
    			else push(&l,valor_da_var(&v, argumento));
    		}
    		else
    		{
    			arg_numerico=atoi(argumento);
    			executa_comando(comando,arg_numerico);
    		}
    		
    	}
    	}
    	return 0;
    }
    So,the function analisa tells me if its a number of not,and does the atoi thing.The function executa comando calls all the other functions,push,pop,mul and so on.The problem is when i have a line like this:
    PUSH 34
    It gets 2 arguments,comando and argument.
    But if i have
    MUL
    ADD
    it also gets 2 arguments,and it should only catch mul,whihc is the command(in this case,it would consider add as an argument for mul)The problem is that i´m "fetching" 2 strings,but sometimes,when the commands dont have any arguments,i want to catch only one.Can someone help me with this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  3. Problems reading windows.h
    By yaya in forum Windows Programming
    Replies: 7
    Last Post: 05-22-2009, 02:33 PM
  4. More Reading in problems
    By swayp in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2005, 04:27 PM
  5. Reading from file problems
    By highlands in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2004, 12:50 PM