Reputation: 339
How can I remove the "@" from "@2" is a .asm file? My output is currently incorrect when read from the file, but when using just "2" it produces the proper binary result.
FILE *fp;
char buffer[256];
fp = fopen("Add.asm", "r");
if(fp == NULL){
printf("Error opening file\n");
}
else{
while(fgets(buffer, 256, fp) != NULL){
buffer[strcspn(buffer, "\r\n")] = 0;
printf("Buffer:");
printf("%s\n",buffer);
if(aOrC(buffer) == true){
int changer = stringToInt(buffer);
printf("%s\n",intToBinary(changer));
} else if(aOrC(buffer) == false){
char* jump = jumpBits(buffer);
char* dest = destBits(buffer);
char* comp = compBits(buffer);
char* finalBits = finalBinaryC(comp, dest, jump);
printf("%s\n", finalBits);
}
}
fclose(fp);
}
The Add.asm file is below and from the nand2tetris project.
@2
D=A
@3
D=D+A
@0
M=D
Upvotes: 0
Views: 49
Reputation: 13580
Based on your output the @
comes always at the beginning of the strings. So
you can easily do this:
// str contains the string "@2"
puts(str + (str[0] == '@' ? 1 : 0));
If you want to remove a @
at some random position, then you should write a
function like this
char *remove_char(char *src, char c)
{
if(src == NULL)
return NULL;
char *p = strchr(src, c);
if(p == NULL)
return src; // c not found
// removing c
memmove(p, p+1, strlen(p));
return src;
}
Then you can call it like
char line[] = "abc@def";
puts(remove_char(line, '@'));
This would print abcdef
Upvotes: 2