首页 > 解决方案 > 例如,如何使用 strtok(string, "\n") 删除字符串末尾的 \n?

问题描述

我正在为我的班级编写一个汇编程序(基本上将汇编代码转换为十六进制代码)。它从文件中获取代码,拆分行(strtok)并搜索助记符。

但是当我使用一些不使用参数的助记符RETand时(例如,例如),strtok 将 the 保留在最后,根本找不到助记符。如果我使用它会给出错误并且不起作用。HLTMOV 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);
}

标签: cfileassemblystrtok

解决方案


推荐阅读