0

So I've got a file that looks something like:

 LOAD  A1,DATA1   # load address DATA1 into A1


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

Basically this x 1000

What I want to do is turn each of these lines into a small array like {LOAD, A1, DATA1}

I'm not really looking for code examples, because I'd like to try to figure it out myself, but I was wondering what my best bet is in terms of an input method.

Jeff H
  • 386
  • 1
  • 3
  • 16
  • Looks like you're parsing assembly code. Perhaps writing a grammar with something like lex & yacc? – Schwern Nov 05 '15 at 04:06
  • Read lines in a loop? A [good reference on the input/output functions in C](http://en.cppreference.com/w/c/io) might be helpful. – Some programmer dude Nov 05 '15 at 04:06
  • 1
    In C, it's probably going to be more convenient to process each line as you read it, than to read all the lines and then process all the lines. – user253751 Nov 05 '15 at 04:08

3 Answers3

1

Since you wanted just a hint, the functions you're looking for are fopen, fgets, and fclose. Then you're going to want to string manipulate to break up the strings, I imagine.

See Going through a text file line by line in C for more on reading line by line.

personjerry
  • 1,045
  • 8
  • 28
  • Are these standard input? I need to be able to input through file redirection. – Jeff H Nov 05 '15 at 04:11
  • The `f` in `fopen` stands for file IIRC, so you'd be specifying a filename. If you want standard input output, you can just use `stdin` as param to `fgets`. – personjerry Nov 05 '15 at 04:14
  • Ah, thank you. I was considering fgets earlier but I didn't think it would work because I can't specify a file name. – Jeff H Nov 05 '15 at 04:15
0

You want to open/close/read from your file, so see fopen/fclose/fgets. fgets reads a text line. After that, look at strtok. It can help you split/tokenize the line.

You wanted just a hint, so stop here.

But, if you get stuck, see my recent answer here about proper strtok usage: C parsing input text file into words

Community
  • 1
  • 1
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
0

you can do it by basis c file operations(fopen/fclose/fgets) and after that using string operations to break and cut the read string(strtok is the best example). Following source code is not complete as per your requirement. But for sure it will provide you some basic idea. To execute this code put create file named temp.config and put your file contain inside that.

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

OUTPUT:

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

Note: I have just printed the output, you can store it.

Sigcont
  • 717
  • 2
  • 8
  • 16