简体   繁体   English

读取文本文件并忽略C中的注释行

[英]Reading a text file and ignoring commented lines in C

I'm currently working on making a tiny computer in C for a programming assignment and I'm stuck on one part. 我目前正在用C语言制作一台微型计算机以进行编程任务,但我只停留在一部分上。

I'm stuck on how to correctly ignore comments in a text file that I'm reading in. (Example of file input below). 我一直在研究如何正确忽略正在阅读的文本文件中的注释。(下面的文件输入示例)。

Input File
5 5     //IN 5
6 7     //OUT 7
3 0     //STORE 0
5 5     //IN 5
6 7     //OUT 7
3 1     //STORE 1
1 0     //LOAD 0
4 1     //SUB 1
3 0     //STORE 0
6 7     //OUT 7
1 1     //LOAD 1
6 7     //OUT 7
7 0     //END

The first input on each new line is an operation, the 2nd input being an address. 每条新行的第一个输入是一个操作,第二个输入是一个地址。 I am planning to have a switch statement for each op and then calling the appropriate function. 我计划为每个操作准备一个switch语句,然后调用适当的函数。 This is my current layout for reading in the file: 这是我当前读取文件的布局:

//file handling
int c;
FILE * file;
file = fopen(name, "r");
if (file){
    printf("Run.\n");
    while ((c = getc(file)) != EOF){
        op = c;
        switch (op){
            case 5:
                print("Inside case 5\n");
        }
    }
    fclose(file);
}

How can I ignore the // on each line and skip to the next line in the file? 如何忽略每行// //并跳至文件的下一行?

Call fgets to get a full line: 致电fgets以获取完整行:

fgets(buffer, 100, file);

and then extract the two numbers from the line: 然后从该行中提取两个数字:

sscanf(buffer, "%d%d", &instruction, &address);

how to correctly ignore comments in a text file that I'm reading in 如何正确忽略正在阅读的文本文件中的注释
How can I ignore the // on each line and skip to the next line in the file? 如何忽略每行// //并跳至文件的下一行?

Read the line using fgets() 使用fgets()阅读该

char buf[80];
if (fgets(buf, sizeof buf, file)) {

Look for the // with strstr() @Steve Summit and lop off the string at that point. 使用strstr() @Steve Summit查找// ,然后在该位置断开字符串。

  char *slash_slash = strstr(buf, "//");
  if (slash_slash) {
    *slash_slash = '\0';
  }

Continue processing the line as desired. 根据需要继续处理生产线。

  ...
}

By using fgets and strtok you can read line by line and split the string acording to the // delimiter. 通过使用fgetsstrtok您可以逐行阅读并根据//分隔符分割字符串。 Here's an example (it is not fully checked, but it's the main idea): 这是一个示例(尚未完全检查,但这是主要思想):

FILE *f = fopen("file.txt", "r");
    if (f== NULL)
    {
        printf("opening file failed\n");
        return 1;
    }
    char buf[256] = { 0 };
    while (fgets(buf, 256, f))
    {
        char *s = strtok(buf, "//");
        if (s == NULL)
        {
            printf("s == NULL\n");
        }
        else
        {
            printf("%s\n", s);
        }
        memset(buf, 0, 256);
    }
    fclose(f);

EDIT: I just realized that this is not exactly what you were looking for. 编辑:我只是意识到这不是您想要的。 However, you can still use it in order to first ignore the comment, and then break the given string into operation and address, or whatever that is... 但是,您仍然可以使用它来首先忽略注释,然后将给定的字符串分成操作和地址,或其他任何东西。

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

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