0
iam trying to use only one function to get the value of add and sub. if there is a label i want to ignore it. 

file.txt
------------------------
label: add $1,$2,$2
       sub $1,$2,$2
-----------------------




int main()
{

   char line2[20];        
   char tem_line2[20];

   char *tptr;

   while(fget...line2)   //read one line and store in char line2[20];
   {
     strcpy(tem_line2, line2);  //store same value in different array
     if(strstr(line2,":") == 0)  //if there is a label, ignore 1st word
     {
        tptr = strtok(line2, " \t\n");
        if(tptr != NULL)
        {
            funct1(line2);
        }
     } 
     else                       //if there is no label than dont ignore 1st word
     {
       funt1(line2);
     }
    }
}


funct1(char line2[])
{
  char *tem_tptr;

  tem_tptr = strtok(line2, " \t\n");    //here i should have value add, or sub
  if(tem_tptr != NULL)
  {

  }
}
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by histrungalot
0

Try something like:

#include <string.h>
#include <stdio.h>

void funct1(char line2[]) {
  char *tem_tptr;
  tem_tptr = strtok(line2, " \t\n");    //here i should have value add, or sub
  if(tem_tptr != NULL) {
     printf("\tInside funct1: %s\n",tem_tptr);
  }
}

int main()
{
   char line2[20];        
   char tem_line2[20];
   char *tptr;

   while(fgets(line2,20,stdin))   //read one line and store in char line2[20];
   {
     strcpy(tem_line2, line2);  //store same value in different array
     if(tptr=strstr(line2,":"))  //if there is a label, ignore 1st word
     { 
        // If we are not at the end of the input string, process it
        // First +1 is for number of bytes, the second is because
        // I want to move to the byte after where the ':' was found
        // Have to check if the string is long enough
        if ( (tptr-line2+1+1) < strlen(line2)){
           tptr++ ;
           funct1(tptr);
        } 
        else {
           printf("Bad data\n");
        }
     } 
     else                       //if there is no label than dont ignore 1st word
     {
       funct1(line2);
     }
    }
}

Output:

$ ./a.out
label: add $1,$2,$2
    Inside funct1: add
sub $1,$2,$2
    Inside funct1: sub
label:
Bad data
^C
$

Edited by histrungalot: Fixing

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.