Reputation: 11
So I'm writing a program for my class and I have a line read in from a text file as follows:
while (fgets(line, sizeof(line), file) != NULL)
That gets from a text file and I've been using line[index] matching a character to get specific lines from that text file.
The text file line looks as follows;
dbr:Edmonton rdf:type yago:WikicatTownsInAlberta ,
I've been using sscanf to get specific data from each line.
sscanf(line,"%s %s %s %s",triple[t][0],triple[t][1],triple[t][2],triple[t][3]);
Now what I expected to get from this was:
triple[t][0] == dbr:Edmonton
triple[t][1] == rdf:type
triple[t][2] == yago:WikicatTownsInAlberta
triple[t][3] == ,
Instead what my output was:
Object:dbr:rdf:yago,
Predicate:rdf:yago,
Subject:yago,
Extraneous: ,
Yet, when I printf(%s,line) I get the line exactly as represented in the file.
This could be totally stupid, but I really hope you can help me.
Cheers,
The entirety of the code is as follows:
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
sqlite3 * db; // THE DATABASE
sqlite3_stmt * stmt; // THE SQL STATEMENT
int main(int argc, char * argv[])
{
// Do Argument Stuff, Open Database
int rc;
char * class;
char * data;
if (argc!=3)
{
printf("Usage: <programfile.c database data>\n");
printf("You specified %d arguments. You need to specify 3.\n",argc);
return 1;
}
else
{
printf("Correct Number of Inputs!\n");
//printf("Argument %d: %s",argc,argv[0]);
//printf("Argument %d: %s",argc,argv[1]);
//printf("Argument %d: %s",argc,argv[2]);
data = argv[2];
printf("Data: %s\n",data);
rc = sqlite3_open(argv[1], &db);
}
// Open File
FILE * file = fopen(data,"r");
if (file == NULL)
{
printf("ERROR: File open error.\n");
return 1;
}
/* Read the file */
char line[256];
char prefix[256][200];
char prefixURI[256][200];
char triple[256][200][4];
// object, predicate, subject
while (fgets(line, sizeof(line), file) != NULL)
{
//printf("%s\n",line);
int p = 0, t=0;
// Handle COMMENTS and Blank Lines
if (line[0] == '#' || line[0] == '\n')
{
continue;
}
// Handle "@prefix"
else if (line[0] == '@')
{
// Load prefix (rdf: into prefix) and the URI into prefixURI.
sscanf(line,"@prefix %s %s",prefix[p],prefixURI[p]);
printf("Prefix Encoded: %s URI: %s\n",prefix[p],prefixURI[p]);
p++;
}
// handle data triples
else if (line[3] == ':')
{
printf("%s",line);
sscanf(line,"%s %s %s %s",triple[t][0],triple[t][1],triple[t] [2],triple[t][3]);
printf("Object:%s \nPredicate:%s \nSubject:%s\nExtraneous: %s\n",triple[t][0],triple[t][1],triple[t][2],triple[t][3]);
}
} // end getWhile Loop
fclose(file);
}
Upvotes: 0
Views: 180
Reputation: 1836
If str is a character array with more than one row, sscanf reads the characters in column order. you have to replace you else if(line[3]==':') condition
the correct way is :
// handle data triples
else if (line[3] == ':')
{
printf("%s",line);
sscanf(line,"%s %s %s %s",triple[0][t],triple[1][t],triple[2] [t],triple[3][t]);
printf("Object:%s \nPredicate:%s \nSubject:%s\nExtraneous: %s\n",triple[0][t],triple[1][t],triple[2][t],triple[3][t]);
}
Upvotes: -1