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)
{
}
}
hwoarang69 11 Newbie Poster
Dani AI
Generated
Good setup from and a useful pointer-based idea from — the key problems to avoid are (1) calling strtok in the caller and again in the callee (it keeps internal static state), and (2) using very small fixed buffers. A simple, robust pattern is: read the line with fgets, strip the trailing newline, find a : if any, move the pointer past it, skip leading whitespace, then extract the opcode with a single, portable call (for example sscanf) or a reentrant tokenizer. That keeps the original parsing simple and avoids strtok state surprises.
Example of the extraction core (adapt and expand to your function names and buffer sizes):
/* assume line[] filled by fgets and headers <string.h> <ctype.h> included */
line[strcspn(line, "\r\n")] = '\0'; /* remove newline */
char *colon = strchr(line, ':');
char *instr = colon ? colon + 1 : line; /* point to instruction area */
while (*instr && isspace((unsigned char)*instr)) instr++;
if (*instr == '\0') continue; /* label-only or blank line */
char opcode[32];
if (sscanf(instr, "%31s", opcode) == 1) {
/* opcode has "add" or "sub"; instr still points at operands */
funct1(opcode, instr); /* call your processing function */
} Why this helps: sscanf is portable and does not rely on internal tokenizer state; passing both opcode and instr to funct1 gives you the raw operand text to parse next. If you prefer tokenizing inside funct1, use strtok_r (POSIX) or strtok_s (Windows) instead of strtok. Also increase your line buffer (256–1024 bytes typical), always check return values, and handle edge cases: lines that contain only a label (label:), lines with leading whitespace, and comments or empty lines.
If operands include punctuation (like $1,$2,$2), parse them explicitly (split on commas or scan numbers) instead of relying on %s, since %s will include commas. This approach follows ’s pointer idea but makes the extraction safer and more portable.
histrungalot 32 Junior Poster
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 because: Fixing
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.