#include "LVdefs.h"

struct konfigDatei
    char tableName[50];
    char columnName[128];
    char primary1[50];
    char primary2[50];
    char primary3[50];
    char primary4[50];
    char primary5[50];
    char primary6[50];

main( int argc, char * argv[] )
     int loop  = 0;
     int count = 0;
     struct konfigDatei kd[50];
     char line[500];
     char *ch;

     FILE *f = NULL;

     f = fopen ("/opt/sup0808/public/shah/work/konfigFile.txt", "r" );

     if (f == NULL )
         printf("can't open the file");

         /*while ( !feof(f) )
             fscanf ( f, "%s %s %s %s %s %s %s %s", kd[loop].tableName, kd[loop].columnName, kd[loop].primary1, kd[loop].primary2, kd[loop].primary3,
                      kd[loop].primary4, kd[loop].primary5, kd[loop].primary6 );
         while ( fgets ( line, 500, f ) != NULL )
             ch = strtok(line, " ");
             while ( ch != NULL )
                 sscanf ( ch, "%s", kd[loop].tableName );
                 sscanf ( ch, "%s", kd[loop].columnName );
                 ch = strtok(NULL, " ");

     fclose ( f );

     for ( count = 0; count < loop; count++ )
         printf ( "\n tableName= %s \t "    ,   kd[count].tableName);
         printf ( "column name= %s \t "     ,   kd[count].columnName);
         printf ( "primary 1= %s \t "       ,   kd[count].primary1);
         printf ( "primary 2= %s \t \n "    ,   kd[count].primary2);
         printf ( "primary 3= %s \t \n "    ,   kd[count].primary3);
         printf ( "primary 4= %s \t \n "    ,   kd[count].primary4);
         printf ( "primary 5= %s \t \n "    ,   kd[count].primary5);
         printf ( "primary 6= %s \t \n "    ,   kd[count].primary6);


I am new to C and need your help. I want to parse a text file with variable length lines and store the data into a structure. I written a program but I am not able to parse the file. Please see the attached program.

The contents of file are: First line is just the headings. Data is after the blank line.

TableName ColumnName Primarykey1 Primaykey2 Primarykey3 Primarykey4 Primarykey5 Primarykey6

diagramm diagramm_titel funktionsname diagramnname
diagramm rubrikachse_titel funktionsname diagramnname
diagramm groessenachse_titel funktionsname diagramnname
table1 column1 primary1 primary2 primary3 primary4 primary5
table2 column2 primary1 primary2 primary3
table2 column3 primary1 primary2 primary3
table3 column4 primary1 primary2 primary3 primary4 primary5 primary6

If each loop is separated with a space then the loop that uses strtok() isn't going to work they way you have it coded. It might be better to do it like the method you commented out.

If you expect anyone to actually change the code for you then you need to post a few lines of actual data for us to test.

actually the reason for commenting out the code was that the elements separated by spaces will not be same in each line.... it is possible that in the first line there are 4 elements and in the second 7. i do not have the actual data yet but it will be like this....

If all the lines except the first have the same number of fields on a line then you could parse the first line all by itself and then parse all the remaining lines in a loop.

i have attached a sample data file

I think this is how to parse that file

  1. If the first word is "diagramm" then it's a title line and does not go into the structure

  2. Otherwise the line is a data line so put each field into the structure then put the structure into either an array of structures or a linked list of structures (a linked list would probably be the best if you don't know how many lines of data there will be.)

Is this a school class assignment or one for work?

Hi Dragon,

thanks a lot for your reply. this is a work assignment :)

there is no title line in the file. what am i doing wrong with strtok? Can you please correct it?


This is one way to do it. It's a lot simpler to make primary? fields an array to that a loop can be used to fill it.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996)

struct konfigDatei
    char tableName[50];
    char columnName[128];
    char primary1[6][50];

struct node
    struct node* next;
    struct konfigDatei* k;

struct node* head = NULL;
struct node* tail = NULL;

void insert(char* word, char* line)
    int i;
    struct node* newnode = malloc( sizeof(struct node));
    newnode->k = malloc(sizeof(struct konfigDatei));
    strcpy(newnode->k->tableName, word);
    word = strtok(NULL," ");
    strcpy(newnode->k->columnName, word);
    i = 0;
    while( (word = strtok(NULL," '\n")) != NULL)
    newnode->next = NULL;
    if( head == NULL)
        head = tail = newnode;
        tail->next = newnode;
        tail = newnode;


int main()
    char line[255];
    struct node* curr;
    char* word;
    FILE* fp = fopen("data.txt","r");
    if( fp == NULL)
        printf("Can'[t open data file\n");
        return 1;

    while( fgets(line,sizeof(line), fp) != NULL)

        word = strtok(line," ");
        if( stricmp(word,"diagramm") == 0)
            continue; // ignore this line
        insert(word, line);
    // Now print the linked list to make sure it's correct
    curr = head;
    while( curr)
        printf("%s\n", curr->k->tableName);
        curr = curr->next;