简体   繁体   English

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

[英]How to read two consecutive characters from a file in C?

I have the following code, where I want to create a simply scanner for a simple calculator language. 我有以下代码,在这里我想为一种简单的计算器语言创建一个简单的扫描仪。 I am using fgetc to get the character from the file. 我正在使用fgetc从文件中获取字符。 Though, at some places I also need to check the next character that is followed. 但是,在某些地方,我还需要检查下一个字符。 For this reason I have been using the ++ operator, but it seems like it is not working properly. 因此,我一直在使用++运算符,但似乎无法正常工作。 Can someone help me fix my problem. 有人可以帮我解决我的问题吗?

For example, when I have := in my text file, it prints colon, and then shows an error message saying: "; cannot follow : (colon).", whereas it should print "assign". 例如,当我的文本文件中包含:=时,它将显示冒号,然后显示一条错误消息:“;无法跟随:(冒号)。”,而应显示“分配”。

Here is my full code: 这是我的完整代码:

#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;
}

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

As @pmg suggested, replace ++c with fgetc() . 正如@pmg所建议的,用fgetc()替换++ c。

Below is a reworked if (c == '/') section. 以下是重做的if (c == '/')部分。 The various syntax errors path were not completely coded and the below may need additional cod to deal with unexpected paths. 各种语法错误路径未完全编码,因此以下内容可能需要其他编码才能处理意外路径。 Remember than any fgetc() may result in the EOF condition. 请记住,任何fgetc()可能导致EOF条件。 Also up to 1 ungetc() may be executed between fgetc() calls to undo. 同样在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 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM