如何用 fgets/read/fread 识别双 \n\n
How to identify a double \n\n with fgets/read/fread
我有一个点文件,我需要使用 C 将其解析为图形结构。
我的点文件包含 2 个用双\n 分隔的块:
一个是verticle
另一个是边
我尝试过使用 fgets,因为它一直读取直到遇到 \n。计算 Verticles 的数量而不是它后面的内容是有帮助的。我不知道我该怎么做
现在我有这样的功能(不完整)
int graphe_charger_dot(graphe *g, char *nom_fichier) {
FILE * f;
int n;
if ((f = fopen(nom_fichier, "r")) == 0) {
puts(nom_fichier);
puts("Erreur d'ouverture du fichier");
return -1;
}
char buffer[500];
fgets(buffer, sizeof(buffer),f);
;
if (strcmp(buffer, "graph {\n") != 0) {
puts("Erreur de compatibilité de fichier");
puts(nom_fichier);
return -1;
}
puts("Fichier ouvert et compatible");
puts("Lecture du buffer 1");
// reads the first verticles. the atoi is just here to test if i can convert an char to integer even with a non integer right after it.
fgets(buffer, sizeof(buffer),f);
n = atoi(buffer);
g->n += 1;
return 0;
}
我的点文件
graph {
0;
1;
2;
3;
4;
5;
6;
7;
8;
0 -- 1;
0 -- 4;
0 -- 5;
0 -- 6;
0 -- 8;
1 -- 2;
1 -- 3;
1 -- 4;
1 -- 5;
2 -- 3;
2 -- 4;
2 -- 5;
2 -- 8;
3 -- 7;
5 -- 7;
5 -- 8;
6 -- 8;
7 -- 8;
}
和图的结构
struct s_graphe {
int n; /* number of verticles */
int m; /* number of edges */
int adj[GRAPHE_ORDRE_MAX][GRAPHE_ORDRE_MAX];
/* Adjacency matrix of the graph */
};
How to identify a double \n\n ...
前一行以 '\n'
结尾。使用 fgets()
,测试是否 buffer[0] == '\n'
以查看一行是否以 '\n'
开头和结尾,以检测连续的 '\n'
.
...
puts("Lecture du buffer 1");
//fgets(buffer, sizeof(buffer),f);
//n = atoi(buffer);
//g->n += 1;
while (fgets(buffer, sizeof buffer, f) && buffer[0] != '\n') {
// parse `buffer` for a vertex
}
while (fgets(buffer, sizeof buffer, f)) {
// parse `buffer` for an edge
}
My dot file contains 2 blocs separated with a double \n:
下面看起来像连续 3 '\n'
。 3 中的第一个结束了 "8;\n"
行。
7;\n
8;\n
\n
\n
0 -- 1;\n
0 -- 4;\n
在那种情况下,需要做更多的工作。也许
while (fgets(buffer, sizeof buffer, f) && buffer[0] != '\n') {
// parse `buffer` for a vertex
}
// Here code consumes 0 or more lines that are only `"\n"`
// Could use a counter to limit
int ch;
while ((c = fgetc(f)) == '\n');
ungetc(ch, f);
while (fgets(buffer, sizeof buffer, f)) {
// parse `buffer` for an edge
}
我有一个点文件,我需要使用 C 将其解析为图形结构。
我的点文件包含 2 个用双\n 分隔的块:
一个是verticle
另一个是边
我尝试过使用 fgets,因为它一直读取直到遇到 \n。计算 Verticles 的数量而不是它后面的内容是有帮助的。我不知道我该怎么做
现在我有这样的功能(不完整)
int graphe_charger_dot(graphe *g, char *nom_fichier) {
FILE * f;
int n;
if ((f = fopen(nom_fichier, "r")) == 0) {
puts(nom_fichier);
puts("Erreur d'ouverture du fichier");
return -1;
}
char buffer[500];
fgets(buffer, sizeof(buffer),f);
;
if (strcmp(buffer, "graph {\n") != 0) {
puts("Erreur de compatibilité de fichier");
puts(nom_fichier);
return -1;
}
puts("Fichier ouvert et compatible");
puts("Lecture du buffer 1");
// reads the first verticles. the atoi is just here to test if i can convert an char to integer even with a non integer right after it.
fgets(buffer, sizeof(buffer),f);
n = atoi(buffer);
g->n += 1;
return 0;
}
我的点文件
graph {
0;
1;
2;
3;
4;
5;
6;
7;
8;
0 -- 1;
0 -- 4;
0 -- 5;
0 -- 6;
0 -- 8;
1 -- 2;
1 -- 3;
1 -- 4;
1 -- 5;
2 -- 3;
2 -- 4;
2 -- 5;
2 -- 8;
3 -- 7;
5 -- 7;
5 -- 8;
6 -- 8;
7 -- 8;
}
和图的结构
struct s_graphe {
int n; /* number of verticles */
int m; /* number of edges */
int adj[GRAPHE_ORDRE_MAX][GRAPHE_ORDRE_MAX];
/* Adjacency matrix of the graph */
};
How to identify a double \n\n ...
前一行以 '\n'
结尾。使用 fgets()
,测试是否 buffer[0] == '\n'
以查看一行是否以 '\n'
开头和结尾,以检测连续的 '\n'
.
...
puts("Lecture du buffer 1");
//fgets(buffer, sizeof(buffer),f);
//n = atoi(buffer);
//g->n += 1;
while (fgets(buffer, sizeof buffer, f) && buffer[0] != '\n') {
// parse `buffer` for a vertex
}
while (fgets(buffer, sizeof buffer, f)) {
// parse `buffer` for an edge
}
My dot file contains 2 blocs separated with a double \n:
下面看起来像连续 3 '\n'
。 3 中的第一个结束了 "8;\n"
行。
7;\n
8;\n
\n
\n
0 -- 1;\n
0 -- 4;\n
在那种情况下,需要做更多的工作。也许
while (fgets(buffer, sizeof buffer, f) && buffer[0] != '\n') {
// parse `buffer` for a vertex
}
// Here code consumes 0 or more lines that are only `"\n"`
// Could use a counter to limit
int ch;
while ((c = fgetc(f)) == '\n');
ungetc(ch, f);
while (fgets(buffer, sizeof buffer, f)) {
// parse `buffer` for an edge
}