Thread: reading a file line by line

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    reading a file line by line

    hallo,
    i need to read a file which is have this format

    $system = "Na3N";
    $SG = "139";
    $sg = "I4/MMM";
    $os = "Al3Ti";
    $NoA = 4;
    $afrom = 4.24269319472366;
    $bfrom = 4.24269319472366;
    $cfrom = 5.99985148882454;
    $alpha = 90;
    $beta = 90;
    $gamma = 90;
    $the_best_energy = -5.399666516753E+02;
    now when i want to read line by line and want to print like
    system SG sg os NoA .............................
    Na3N 139 ........
    how can i read and print like this
    bye
    sudhir

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Cool problem, here's a quick and dirty solution I came up with to see how it might be done
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define DELIM " =;"
    
    enum { NAME_MARKER = '$', STR_MARKER = '\"', DBL_MARKER = '.' };
    enum { STRING, DOUBLE, INT };
    
    struct file_fields {
        struct file_fields *next;
        char *name;
        int field;
        union field_value {
            double d;
            char s[BUFSIZ];
            int i;
        } value;
    };
    
    void cleanup(struct file_fields *list);
    
    int main(void)
    {
        FILE *in;
        char buf[1000];
        struct file_fields *head;
        struct file_fields *it;
        
        in = fopen("input.txt", "r");
        if (in == NULL)
        {
            perror("File couldn't be opened");
            exit(EXIT_FAILURE);
        }
        
        head = malloc(sizeof (*head));
        if (head == NULL)
        {
            fprintf(stderr, "Memory exhausted\n");
            cleanup(head);
        }
        
        it = head;
        
        while (fgets(buf, sizeof (buf), in))
        {
            char *t;
            char *newline;
            
            it->next = malloc(sizeof (*head));
            if (it->next == NULL)
            {
                fprintf(stderr, "Memory exhausted\n");
                cleanup(head);
            }
            
            it = it->next;
    
            newline = strrchr(buf, '\n');
            if (newline)
                *newline = '\0';
            
            for (t = strtok(buf, DELIM); t; t = strtok(NULL, DELIM))
            {
                if (*t == NAME_MARKER)
                {
                    it->name = malloc(strlen(t) + 1);
                    if (it->name == NULL)
                    {
                        fprintf(stderr, "Memory exhausted\n");
                        cleanup(head);
                    }
                    
                    strcpy(it->name, t);
                }
                else
                {
                    /* Not a name, must be a value */
                    if (*t == STR_MARKER)
                    {
                        it->field = STRING;
                        strncpy(it->value.s, t, BUFSIZ);
                    }
                    else if (strchr(t, DBL_MARKER))
                    {
                        it->field = DOUBLE;
                        it->value.d = strtod(t, NULL);
                    }
                    else
                    {
                        it->field = INT;
                        it->value.i = strtol(t, NULL, 0);
                    }
                }
            }
        }
        
        for (it = head->next; it; )
        {
            int i;
            struct file_fields *save;
    
            save = it;
    
            for (i = 1; it && i % 6; i++)
            {
                printf("%-15s", it->name);
                fflush(stdout);
    
                it = it->next;
            }
    
            printf("\n");
    
            it = save;
    
            for (i = 1; it && i % 6; i++)
            {
                switch (it->field)
                {
                case STRING:
                    printf("%-15s", it->value.s);
                    break;
                case DOUBLE:
                    printf("%-15g", it->value.d);
                    break;
                case INT:
                    printf("%-15d", it->value.i);
                    break;
                }
    
                it = it->next;
            }
    
            printf("\n\n");
        }
    
        cleanup(head);
    
        return 0;
    }
    
    void cleanup(struct file_fields *list)
    {
        struct file_fields *forward;
    
        while (list)
        {
            forward = list->next;
            
            free(list->name);
            free(list);
            
            list = forward;
        }
    
        exit(0);
    }
    And yes, I know main is way too long.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM