【问题标题】:How to read each line from a file if I intend to split it up into strings? C如果我打算将文件拆分为字符串,如何从文件中读取每一行? C
【发布时间】:2016-02-05 19:29:28
【问题描述】:

所以我有一个看起来像这样的文件:

 LOAD  A1,DATA1   # load address DATA1 into A1


LOADI R1,A1      # load contents of address in A1 into R1

基本上这 x 1000

我想做的是将这些行中的每一行变成一个小数组,如 {LOAD, A1, DATA1}

我并不是真的在寻找代码示例,因为我想自己弄清楚,但我想知道在输入法方面我最好的选择是什么。

【问题讨论】:

  • 看起来您正在解析汇编代码。也许用 lex & yacc 之类的东西写一个语法?
  • 循环读取行? good reference on the input/output functions in C 可能会有所帮助。
  • 在 C 中,在阅读时处理每一行可能比读取所有行然后处理所有行更方便。

标签: c parsing input


【解决方案1】:

您可以通过基础 c 文件操作(fopen/fclose/fgets)来完成,然后使用字符串操作来中断和剪切读取的字符串(strtok 是最好的例子)。 根据您的要求,以下源代码不完整。但可以肯定的是,它会为您提供一些基本的想法。要执行此代码,请创建名为 temp.config 的文件并将您的文件包含在其中。

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<stdint.h>
#define MAXLEN 1024
#define CONFIG_FILE "temp.config"
/*
 * remove trailing and leading whitespace
 */
static inline char *
trim (char * s)
{
  /* Initialize start, end pointers */
  char *s1 = s, *s2 = &s[strlen (s) - 1];

  /* Trim and delimit right side */
  while ( (isspace (*s2)) && (s2 >= s1) )
    s2--;
  *(s2+1) = '\0';

  /* Trim left side */
  while ( (isspace (*s1)) && (s1 < s2) )
    s1++;

  /* Copy finished string */
  strcpy (s, s1);
  return s;
}

inline bool
parse_config ( ){
#ifdef DEBUG
    fprintf(stdout,"__parse_config__\n");
#endif
    char *s, buff[MAXLEN];
    char *temp1,*temp2;

    FILE *fp = fopen (CONFIG_FILE, "r");
    if (fp == NULL){
        fprintf(stderr,"Not able to open file\n");
        return false;

    }
     /* Read next line */
     while ( ( (s = fgets (buff, sizeof buff, fp)) != NULL)  ){
          /* Skip blank lines and comments */
         if (buff[0] == '\n' || buff[0] == '#')
             continue;
          /* Parse name/value pair from line */
         char name[MAXLEN], value[MAXLEN];
         s = strtok (buff, " ");
         if (s==NULL)
             continue;
         else
             strncpy (name, s, MAXLEN);
         s = strtok (NULL, " ");
         if (s==NULL)
             continue;
         else
             strncpy (value, s, MAXLEN);
         trim (value);
         /* you can use a switch case*/
         if (strcmp(name, "LOAD")==0){
             fprintf(stdout,"%s\n",name);
             temp1 = strtok(value,",");
             fprintf(stdout,"%s\n",value);
             //this is the logic.. rest you have to implement.
         }  if (strcmp(name, "LOADI")==0){
                     fprintf(stdout,"%s\n",name);
                     temp1 = strtok(value,",");
                     fprintf(stdout,"%s\n",value);
             }

     }
     fclose(fp);
     return true;
}
int main(){
    parse_config();
    return 0;
}

输出:

root@suman-OptiPlex-380:/home/suman/poc# ./a.out 
LOAD
A1
LOADI
R1

注意:我刚刚打印了输出,你可以存储它。

【讨论】:

    【解决方案2】:

    您想打开/关闭/读取文件,请参阅fopen/fclose/fgetsfgets 读取文本行。之后,查看strtok。它可以帮助您拆分/标记线路。

    你只是想要一个提示,所以停在这里。

    但是,如果您遇到问题,请在此处查看我最近关于正确使用 strtok 的回答:C parsing input text file into words

    【讨论】:

      【解决方案3】:

      由于您只需要提示,因此您要查找的函数是 fopenfgetsfclose。然后你会想要字符串操作来分解字符串,我想。

      有关逐行阅读的更多信息,请参阅Going through a text file line by line in C

      【讨论】:

      • 这些是标准输入吗?我需要能够通过文件重定向输入。
      • fopen 中的 f 代表文件 IIRC,因此您需要指定文件名。如果你想要标准输入输出,你可以使用stdin作为fgets的参数。
      • 啊,谢谢。我之前正在考虑使用 fgets,但我认为它不会起作用,因为我无法指定文件名。
      猜你喜欢
      • 2012-11-19
      • 2023-04-08
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2016-04-30
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多