次のようなファイルからすべての行を読みたい:
readEveryLine
{
"Bart [m]" -> "Marge [f]";
"Lisa [f]" -> "Homer [m]";
...
}
使いたい:
- ファイルを 1 行ずつ読み取る fgets()
- strncmp() を使用して、すべての行を指定された文字列と比較するか、適切な形式であることを確認します
私が持っているもの:
while(fgets(*file_string, MAX_INPUT_STDIN, file) != NULL)
{
changeLastC(*file_string); // function to change \n into \0
if (strncmp(*file_string, "readEveryLine\0", 14) == 0)
{
if (strncmp(*file_string, "{\0", 2) == 0)
{
// check the first -> relation
}
}
else
{
printf("Error Parsing\n");
}
}
問題は、エラー解析が表示されるだけで、ここで何が間違っていたのかわかりません。
助けてくれてどうもありがとう!
ここで、いくつかのことを行いました (最初の 2 行の解析が機能するようになりました)。どうもありがとう。
if ((fp = fopen("df.dot","r")) == NULL)
{
printf("Error: File Open\n");
return 1;
}
int row = 0; // check row 1
while (fgets(buffer, MAX_PARSING, fp))
{
if ((row == 0) && strncmp(buffer, "readEveryLine\n", 14) == 0)
{
printf("%s", buffer);
}
else
{
printf("Parsing Error 1\n");
}
}
int row1 = 1; // check row 2
while (fgets(buffer, MAX_PARSING, fp))
{
if ((row1 == 1) && strncmp(buffer, "{\n", 2) == 0)
{
printf("%s", buffer);
}
else
{
printf("Parsing Error 2\n");
}
}
int row2 = 2; // check other rows (dynamic, could be even more or less)
while (fgets(buffer, MAX_PARSING, fp))
{
if ((row2 == 2) && strncmp(buffer, " ", 2) == 0)
{
const char *p1 = strstr(fp, "\"")+1;
const char *p2 = strstr(p1, " [m]\"");
const char *p3 = strstr(p1, " [f]\"");
// extract male persons
if (p1 && p2)
{
size_t len1 = p2 - p1;
char* res1 = (char*)malloc(sizeof(char)*(len1 + 1));
strncpy(res1, p1, len1);
res1[len1] = '\0';
// give res1 for functionMale() to work on that string
}
// extract female persons
else if (p1 && p3)
{
size_t len2 = p3 - p1;
char* res2 = (char*)malloc(sizeof(char)*(len2 + 1));
strncpy(res2, p1, len2);
res2[len2] = '\0';
// give res2 for functionFemale() to work on that string
}
else if (strcmp(buffer, " -> ") == 0)
{
// work in progress (quite complicated to do this i think)
// it has to be a realtion between two people
}
else if (strcmp(buffer, ";") == 0)
{
// work in progress
// this sign can either exist like this:
// "Bart [m]" -> "Marge [f]";
// or like this:
// "Marge [f]";
}
break;
}
else
{
printf("Parsing Error 3\n");
}
row2++;
}
// そして最後の記号は }\n でなければなりません