如何从C文件中读取两个连续字符



我有下面的代码,我想在其中为一个简单的计算器语言创建一个简单的扫描仪。我使用fget从文件中获取字符。但是,在某些地方,我还需要检查后面的下一个字符。出于这个原因,我一直在使用++操作符,但它似乎不能正常工作。谁能帮我解决这个问题?

例如,当我的文本文件中有:=时,它打印冒号,然后显示一条错误消息:";不能跟:(冒号).",而应该打印"assign"

下面是我的完整代码:
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, char** argv)
{
    FILE *fp;
    int c, check, reti_d, reti_l;
    regex_t digit, letter;
    reti_d = regcomp(&digit, "[0-9]", 0);
    reti_l = regcomp(&letter, "[a-zA-Z]", 0);
    if (reti_d || reti_l)
    {
        fprintf(stderr, "Couldn't compile the regular expression(s).n");
        exit(1);
    }
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s filename.txtn", argv[0]);
        exit(1);
    }
    if (!(fp = fopen(argv[1], "r")))
    {
        perror("Error opening file!n");
        exit(1);
    }
    while ((c = fgetc(fp)) != EOF)
    {
        char regdtest[1];
        char regltest[1];
        regdtest[0] = (char)c;
        reti_d = regexec(&digit, regdtest, 0, NULL, 0);
        reti_l = regexec(&letter, regltest, 0, NULL, 0);
        switch (c)
        {
            case '(': printf("lperen "); break;
            case ')': printf("rparen "); break;
            case '+': printf("plus "); break;
            case '-': printf("minus "); break;
            case '*': printf("times "); break;
        }
        if (c == ':')
        {
            if (c++ == '=')
                printf("assign ");
            else printf("n%c cannot follow : (colon).n", (char)c);
        }
        if (c == '/')
        {
            if (++c == '/' || ++c == '*')
            {
                while (c != 'n' || (c == '*' && ++c == '/'))
                    c++;
            }
            else printf("div ");
        }
        if (c == '.')
        {
            regdtest[0] = (char)++c;
            reti_d = regexec(&digit, regdtest, 0, NULL, 0);
            if (!reti_d)
            {
                printf("number ");
            }
            else printf("n. (dot) should be followed by digits.n");
        }
        if (!reti_d)
        {
            while (!reti_d)
            {
                regdtest[0] = (char)c;
                reti_d = regexec(&digit, regdtest, 0, NULL, 0);
            }
            printf("number ");
        }
        if (!reti_l)
        {
            int i, j = 0;
            char read[5], write[6];
            read[i] = write[j] = (char)c;
            while(!reti_d || !reti_l)
            {
                c++;
                i++;
                j++;
                if (i >= 5)
                    i = 0;
                if (j >= 6)
                    j = 0;
                read[i] = write[j] = (char)c;
                if (strcmp(read, "read") == 0)
                    printf("read ");
                else printf("id ");
                if (strcmp(write, "write") == 0)
                    printf("write ");
                else printf("id ");
                regdtest[0] = (char)c;
                regltest[0] = (char)c;
                reti_d = regexec(&digit, regdtest, 0, NULL, 0);
                reti_l = regexec(&letter, regltest, 0, NULL, 0);
            }
            printf("letter ");
        }
    }
    fclose(fp);
    return 0;
}

您需要再次调用fgetc()来获得第二个字符

按照@pmg的建议,将++c替换为fgetc()

下面是一个重做的if (c == '/')部分。各种语法错误路径没有完全编码,下面可能需要额外的代码来处理意外的路径。记住,任何fgetc()都可能导致EOF情况。在fgetc()调用之间最多可以执行1个ungetc()来撤消。

if (c == '/') {
  c = fgetc(fp);
  if (c == '/') {
    c = fgetc(fp);
    if (c == '*') {
      while (c = fgetc(fp) != 'n' && c != EOF) {
        if (c == '*') {
          c = fgetc(fp);
          if (c == '/') break;
          ungetc(c, fp);
        }
      }
      printf("div ");
    }
    else TBD();
  } else ...

相关内容

  • 没有找到相关文章