简体   繁体   中英

How to read each line from a file if I intend to split it up into strings? C

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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