c - 例如,如何使用 strtok(string, "\n") 删除字符串末尾的 \n?
问题描述
我正在为我的班级编写一个汇编程序(基本上将汇编代码转换为十六进制代码)。它从文件中获取代码,拆分行(strtok)并搜索助记符。
但是当我使用一些不使用参数的助记符RET
and时(例如,例如),strtok 将 the 保留在最后,根本找不到助记符。如果我使用它会给出错误并且不起作用。HLT
MOV A, [X]
'\n'
strtok(string, "\n")
那么,我怎样才能用它从字符串的末尾strtok()
删除它呢?'\n'
这是程序的第一个函数,用于统计每个汇编函数的字节数:
void contabyte (char arquivo[50], char arquivo_byte[50])
{
FILE *arq, *arq_dest;
int byte = 0;
char linhatok[80], linha[80];
char *mnemonico;
arq = fopen(arquivo, "r");
if (!arq)
printf("Erro na abertura do arquivo\n");
arq_dest = fopen(arquivo_byte, "w+");
if (!arq_dest)
printf("Erro na criação do arquivo destino\n");
while (fgets(linhatok, 80, arq) != NULL) {
if (feof(arq)) break;
strcpy(linha, linhatok);
strlwr(linhatok);
mnemonico = strtok(linhatok, " [];");
if (strcmp(mnemonico, "add") == 0) {
mnemonico = strtok(NULL, " [];");
mnemonico = strtok(NULL, " [];");
if (strcmp(mnemonico, "b") == 0) byte++;
else byte += 2;
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "sub") == 0) {
mnemonico = strtok(NULL, " [];");
mnemonico = strtok(NULL, " [];");
if (strcmp(mnemonico, "b") == 0) byte++;
else byte += 2;
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "cmp") == 0) {
mnemonico = strtok(NULL, " [];");
mnemonico = strtok(NULL, " [];");
if (strcmp(mnemonico, "b") == 0) byte++;
else byte += 2;
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "inc") == 0) {
byte++;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "dec") == 0) {
byte++;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "jc") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "jnc") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "jz") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "jnz") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "jbe") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "ja") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "mov") == 0) {
mnemonico = strtok(NULL, " [];");
if (strcmp(mnemonico, "b") == 0) byte++;
mnemonico = strtok(NULL, " [];");
if (strcmp(mnemonico, "a") == 0) byte++;
else byte += 2;
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "jmp") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "call") == 0) {
byte += 2;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "ret") == 0) {
byte++;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else if (strcmp(mnemonico, "hlt") == 0) {
byte++;
mnemonico = strtok(NULL, " [];");
// printf("%d\t%s\n", byte, linha);
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
else {
printf("%d\t%s\t%d\n", byte, mnemonico, strlen(mnemonico));
fprintf(arq_dest, "%d\t%s\n", byte, linha);
}
}
fclose(arq_dest);
fclose(arq);
}
解决方案
推荐阅读
- javascript - 使用 Node Js 发送和检索表单数据
- wcf - 在没有 XML 配置的情况下将依赖项绑定到 WCF 服务
- html - VBA循环浏览网页中的下拉元素并下载到excel表
- git - 为什么在 Visual Studio Code 中尝试使用“git checkout”时不能切换分支
- php - 如何正确地将 CSS 应用于 PHP 回显?
- mysql - 如何删除 MySQL 中字符串的第一部分,包括第一个逗号?
- php - 如何在 WordPress 中注销之前执行一些代码
- java - 如何直接使 OpenJFX 可用?
- matplotlib - 从几何坐标中获取投影坐标
- angular - Angular7 - 表单未提交到数据库