【发布时间】:2016-12-03 06:25:24
【问题描述】:
所以我正在为我的班级编写一个程序,我从文本文件中读取了一行如下:
while (fgets(line, sizeof(line), file) != NULL)
这是从文本文件中获取的,我一直在使用匹配字符的 line[index] 从该文本文件中获取特定行。
文本文件行如下所示;
dbr:Edmonton rdf:type yago:WikicatTownsInAlberta ,
我一直在使用 sscanf 从每一行获取特定数据。
sscanf(line,"%s %s %s %s",triple[t][0],triple[t][1],triple[t][2],triple[t][3]);
现在我期望从中得到的是:
triple[t][0] == dbr:Edmonton
triple[t][1] == rdf:type
triple[t][2] == yago:WikicatTownsInAlberta
triple[t][3] == ,
我的输出是什么:
Object:dbr:rdf:yago,
Predicate:rdf:yago,
Subject:yago,
Extraneous: ,
然而,当我 printf(%s,line) 得到的行与文件中所表示的完全一样。
这可能完全是愚蠢的,但我真的希望你能帮助我。
干杯,
整个代码如下:
#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);
}
【问题讨论】:
-
triple是如何定义的?始终显示有问题的完整代码,而不仅仅是其中的某些行。 -
很难调试不完整的代码。请提供minimal reproducible example。
-
还有;一个最小、完整和可验证的示例不意味着您的整个源文件。
-
您是否希望
"dbr:Edmonton"适合 4 个字符triple[t][0]?