【问题标题】:C - How do I get the token before the one I'm currently in?C - 如何在我当前所在的令牌之前获得令牌?
【发布时间】:2021-03-11 12:02:56
【问题描述】:

我正在尝试将我当前所在文件之前的令牌返回到 C 中的控制台。标记由分隔符分隔。我需要实现某种计数器吗?

这是我的代码。

#include <stdio.h>
#include <string.h>

int main (int argc, char **argv)
{
    char line [1000];
    char *delimeters = ".,; \t\n";
    FILE *input = fopen (argv[2], "r");

    while(fgets(line,1000,input) != NULL)
    {
        char *word = strtok(line, delimeters);

        while(word != NULL)
        {
            if(word != NULL && strcmp(word,"(") == 0)
            {
                printf("Enters IF 3\n");
                char *method_name = strtok(NULL, delimeters); //NEED BEFORE WORD
                printf ("Method %s\n", method_name);
            }

【问题讨论】:

  • 你不能把前一个存储在char *prev吗?
  • 我该如何写下来?因为在我输入 if 语句时,我已经在下一个语句中。就像这样“某事()”我打印出“)”,我想要“某事”。

标签: c token


【解决方案1】:

好吧,我想通了,与其在之后寻找前一个标记,我还得在 if 语句中寻找之前的东西。无论如何,这是一个棘手的问题。

示例:Method Return_type Something ();

而不是寻找(,我必须寻找Method,然后用strtok跳转到Something

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 2021-09-05
    • 2020-04-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2018-06-10
    相关资源
    最近更新 更多