#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");
}
else
{
/*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 );
loop++;
}*/
while ( fgets ( line, 500, f ) != NULL )
{
ch = strtok(line, " ");
while ( ch != NULL )
{
sscanf ( ch, "%s", kd[loop].tableName );
sscanf ( ch, "%s", kd[loop].columnName );
loop++;
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);
}
}
Hi,
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