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

时间:2014-03-16 21:30:23

标签: c file fgetc getc

我有以下代码,我想为简单的计算器语言创建一个简单的扫描程序。我正在使用fgetc从文件中获取字符。虽然,在某些地方我还需要检查所遵循的下一个字符。出于这个原因,我一直在使用++运算符,但它似乎无法正常工作。有人可以帮我解决我的问题。

例如,当我在我的文本文件中有:=时,它会打印冒号,然后显示一条错误消息:“;不能跟随:(冒号)。”,而它应该打印“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.txt\n", 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;
}

2 个答案:

答案 0 :(得分:1)

您需要再次致电fgetc()来获得第二个角色。

答案 1 :(得分:0)

正如@pmg建议的那样,将++ c替换为fgetc()

以下是经过重新设计的if (c == '/')部分。各种语法错误路径未完全编码,下面可能需要额外的cod来处理意外路径。请记住,任何fgetc()都可能导致EOF条件。在ungetc()撤消调用之间最多可执行1 fgetc()

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 ...